Repository navigation
Expand file tree
/
Copy pathQueueRoutes.php
More file actions
179 lines (152 loc) · 4.41 KB
/
Copy pathQueueRoutes.php
File metadata and controls
179 lines (152 loc) · 4.41 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
<?php
namespace Illuminate\Queue;
use Illuminate\Queue\Attributes\Queue as QueueAttribute;
use Illuminate\Support\Traits\ReadsClassAttributes;
use UnitEnum;
use function Illuminate\Support\enum_value;
class QueueRoutes
{
use ReadsClassAttributes;
/**
* The mapping of class names to their default routes.
*
* @var array<class-string, array|string>
*/
protected $routes = [];
/**
* The queues that have been forwarded to another queue and/or connection.
*
* @var array<string, array>
*/
protected $forwards = [];
/**
* Get the queue connection that a given queueable instance should be routed to.
*
* @param object $queueable
* @return string|null
*/
public function getConnection($queueable)
{
$route = $this->getRoute($queueable);
if (! is_null($route)) {
return is_string($route) ? null : $route[0];
}
if (empty($this->forwards)) {
return null;
}
return $this->forwardedConnection(
$this->getAttributeValue($queueable, QueueAttribute::class, 'queue')
);
}
/**
* Get the connection the given queue has been forwarded to.
*
* @param \UnitEnum|string|null $queue
* @return string|null
*/
protected function forwardedConnection($queue)
{
if (is_null($queue)) {
return null;
}
return $this->forwards[enum_value($queue)][0] ?? null;
}
/**
* Get the queue that a given queueable instance should be routed to.
*
* @param object $queueable
* @return string|null
*/
public function getQueue($queueable)
{
$route = $this->getRoute($queueable);
if (is_null($route)) {
return;
}
return is_string($route)
? $route
: $route[1];
}
/**
* Get the queue the given queue has been forwarded to.
*
* @param string $queue
* @param string|null $connection
* @return string
*/
public function forwardedQueue($queue, $connection = null)
{
if (! isset($this->forwards[$queue])) {
return $queue;
}
[$forwardConnection, $forwardQueue] = $this->forwards[$queue];
return is_null($forwardConnection) || $forwardConnection === $connection
? $forwardQueue ?? $queue
: $queue;
}
/**
* Get the route for a given queueable instance.
*
* @param object $queueable
* @return array|string|null
*/
public function getRoute($queueable)
{
if (empty($this->routes)) {
return null;
}
$classes = array_merge(
[get_class($queueable)],
class_parents($queueable) ?: [],
class_implements($queueable) ?: [],
class_uses_recursive($queueable)
);
foreach ($classes as $class) {
if (isset($this->routes[$class])) {
return $this->routes[$class];
}
}
return null;
}
/**
* Register the queue route for the given class.
*
* @param array|class-string $class
* @param \UnitEnum|string|null $queue
* @param \UnitEnum|string|null $connection
* @return void
*/
public function set(array|string $class, $queue = null, $connection = null)
{
$routes = is_array($class) ? $class : [$class => [$connection, $queue]];
foreach ($routes as $from => $to) {
$this->routes[$from] = is_array($to)
? array_map(enum_value(...), $to)
: enum_value($to);
}
}
/**
* Register a forward for the given queue.
*
* @param \UnitEnum|array<string, \UnitEnum|string>|string $queue
* @param \UnitEnum|string|null $to
* @param \UnitEnum|string|null $connection
* @return void
*/
public function forward(UnitEnum|array|string $queue, $to = null, $connection = null)
{
$forwards = is_array($queue) ? $queue : [enum_value($queue) => $to];
foreach ($forwards as $from => $destination) {
$this->forwards[enum_value($from)] = [enum_value($connection), enum_value($destination)];
}
}
/**
* Get all registered queue routes.
*
* @return array<class-string, array|string>
*/
public function all()
{
return $this->routes;
}
}