-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDispatcher.php
More file actions
82 lines (71 loc) · 1.67 KB
/
Copy pathDispatcher.php
File metadata and controls
82 lines (71 loc) · 1.67 KB
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
<?php
/**
* Dispatcher class file.
*
* @package Mantle
*/
namespace Mantle\Queue;
use Mantle\Contracts\Application;
use Mantle\Contracts\Queue\Can_Queue;
use Mantle\Contracts\Queue\Queue_Manager;
use Mantle\Queue\Events\Job_Queued;
/**
* Queue Dispatcher
*
* Executes jobs from the queue.
*/
class Dispatcher {
/**
* Constructor.
*
* @param Application $container Container instance.
*/
public function __construct( protected Application $container ) {}
/**
* Dispatch the job to the queue.
*
* @param mixed $job Job instance.
*/
public function dispatch( mixed $job ): void {
if ( ! $this->should_command_be_queued( $job ) ) {
$this->dispatch_now( $job );
return;
}
/**
* Provider instance.
*
* @var \Mantle\Contracts\Queue\Provider
*/
$provider = $this->container->make( Queue_Manager::class )->get_provider();
// Send the job to the queue.
$provider->push( $job );
// Dispatch the job queued event.
$this->container['events']->dispatch(
new Job_Queued( $provider, $job ),
);
}
/**
* Dispatch the job after sending the given response.
*
* @param mixed $job Job instance.
*/
public function dispatch_after_response( mixed $job ): void {
$this->container->terminating( fn () => $this->dispatch_now( $job ) );
}
/**
* Dispatch a job in the current process.
*
* @param object|class-string $job Job instance.
*/
public function dispatch_now( mixed $job ): void {
$this->container->call( [ $job, 'handle' ] );
}
/**
* Check if the command should be queued.
*
* @param mixed $job Job instance.
*/
protected function should_command_be_queued( $job ): bool {
return $job instanceof Can_Queue;
}
}