-
Notifications
You must be signed in to change notification settings - Fork 0
/
Queueable.php
62 lines (51 loc) · 1.79 KB
/
Queueable.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
<?php
declare(strict_types=1);
namespace Forme\Framework\Jobs;
use Psr\Container\ContainerInterface;
use ReflectionFunction;
use RuntimeException;
trait Queueable
{
public function dispatch(array $args = []): void
{
$queue = $this->getQueue();
$queue->dispatch(['class' => $this::class, 'arguments' => $args]);
}
public function schedule(array $args = []): void
{
$queue = $this->getQueue();
$args['class'] = $this::class;
$queue->schedule($args);
}
public function start(array $args = []): void
{
$queue = $this->getQueue();
$args['class'] = $this::class;
$queue->start($args);
}
public function stop(): void
{
$queue = $this->getQueue();
$queue->stop(['class' => $this::class]);
}
protected function getQueue(): Queue
{
if (property_exists($this, 'queue') && is_a($this->queue, Queue::class)) {
return $this->queue;
}
if (property_exists($this, 'container') && is_a($this->container, ContainerInterface::class)) {
return $this->container->get(Queue::class);
}
if (function_exists('app')) {
$reflection = new ReflectionFunction('app');
$returnType = $reflection->getReturnType();
if ($returnType && is_a((string) $returnType, ContainerInterface::class, true)) {
return app()->get(Queue::class);
}
}
if (function_exists('Forme\getInstance') && defined('FORME_PRIVATE_ROOT')) {
return \Forme\getInstance(Queue::class);
}
throw new RuntimeException('Unable to instantiate queue, either provide a suitable container or inject the queue directly into the job class constructor as $queue');
}
}