Skip to content

Commit 8ec6bf7

Browse files
committed
feat: switch to documentation accurate API
1 parent 84b3c2a commit 8ec6bf7

File tree

3 files changed

+37
-18
lines changed

3 files changed

+37
-18
lines changed

src/Batch.php

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,35 @@ class Batch implements Dispatchable
2727
*/
2828
protected $queue = null;
2929

30+
/**
31+
* Configured queue connection
32+
*/
33+
protected string $connection = 'default';
34+
35+
/**
36+
* Return configured connection
37+
*/
38+
public function connection()
39+
{
40+
return $this->connection;
41+
}
42+
3043
/**
3144
* Load a job for running
3245
*/
33-
public function __construct($job, $config, $queue)
46+
public function fromQueue($job, $config, $queue)
3447
{
3548
$this->job = $job;
36-
$this->config = $config;
3749
$this->queue = $queue;
50+
51+
if (isset($config['data'])) {
52+
$this->data = $config['data'];
53+
unset($config['data']);
54+
}
55+
56+
$this->config = $config;
57+
58+
return $this;
3859
}
3960

4061
/**
@@ -125,11 +146,11 @@ public function release($delay = 0)
125146
/**
126147
* Pass data to the job
127148
*/
128-
public function with($data)
149+
public static function with($data)
129150
{
130-
$this->data = $data;
151+
static::$data = $data;
131152

132-
return $this;
153+
return new static;
133154
}
134155

135156
/**

src/Queue/Dispatchable.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public function connection();
1515
/**
1616
* Add data to the job
1717
*/
18-
public function with($data);
18+
public static function with($data);
1919

2020
/**
2121
* Return job stack if available

src/functions.php

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,30 +19,28 @@ function queue()
1919
/**
2020
* Dispatch a job, batch or a group of jobs
2121
*
22-
* @param array|mixed $dispatchable The job, batch or group of jobs to dispatch
22+
* @param array|\Leaf\Queue\Dispatchable|string $dispatchable The job, batch or group of jobs to dispatch
2323
* @param array $data The data to pass to the job
2424
*/
25-
function dispatch($dispatchable, $data = [])
25+
function dispatch($dispatchable)
2626
{
2727
if (is_array($dispatchable)) {
2828
foreach ($dispatchable as $item) {
29-
dispatch($item, $data);
29+
dispatch($item);
3030
}
3131

3232
return;
3333
}
3434

35-
/** @var \Leaf\Queue\Dispatchable */
36-
$jobOrBatch = new $dispatchable();
35+
if (is_string($dispatchable)) {
36+
$dispatchable = new $dispatchable();
37+
}
38+
3739
$queueModuleConfig = MvcConfig('queue') ?? [];
38-
$jobConnection = $jobOrBatch->connection();
40+
$jobConnection = $dispatchable->connection();
3941

4042
$defaultConnection = $queueModuleConfig['connections'][$queueModuleConfig['default'] ?? 'database'];
4143

42-
if (!empty($data)) {
43-
$jobOrBatch->with($data);
44-
}
45-
4644
// if (\Leaf\Config::getStatic('queue')) {
4745
// return queue()->push([
4846
// 'class' => $jobOrBatch,
@@ -56,8 +54,8 @@ function dispatch($dispatchable, $data = [])
5654
queue()->connect($queueModuleConfig['connections'][$jobConnection] ?? $defaultConnection);
5755

5856
return queue()->push([
59-
'class' => $jobOrBatch::class,
60-
'config' => json_encode($jobOrBatch->getConfig()),
57+
'class' => $dispatchable::class,
58+
'config' => json_encode($dispatchable->getConfig()),
6159
'status' => 'pending',
6260
'retry_count' => 0,
6361
]);

0 commit comments

Comments
 (0)