1
|
<?php
|
2
|
|
3
|
namespace Illuminate\Queue;
|
4
|
|
5
|
use Closure;
|
6
|
use DateTime;
|
7
|
use Exception;
|
8
|
use Illuminate\Support\Arr;
|
9
|
use SuperClosure\Serializer;
|
10
|
use Illuminate\Container\Container;
|
11
|
use Illuminate\Contracts\Encryption\Encrypter;
|
12
|
use Illuminate\Contracts\Queue\QueueableEntity;
|
13
|
|
14
|
abstract class Queue
|
15
|
{
|
16
|
/**
|
17
|
* The IoC container instance.
|
18
|
*
|
19
|
* @var \Illuminate\Container\Container
|
20
|
*/
|
21
|
protected $container;
|
22
|
|
23
|
/**
|
24
|
* The encrypter implementation.
|
25
|
*
|
26
|
* @var \Illuminate\Contracts\Encryption\Encrypter
|
27
|
*/
|
28
|
protected $encrypter;
|
29
|
|
30
|
/**
|
31
|
* Push a new job onto the queue.
|
32
|
*
|
33
|
* @param string $queue
|
34
|
* @param string $job
|
35
|
* @param mixed $data
|
36
|
* @return mixed
|
37
|
*/
|
38
|
public function pushOn($queue, $job, $data = '')
|
39
|
{
|
40
|
return $this->push($job, $data, $queue);
|
41
|
}
|
42
|
|
43
|
/**
|
44
|
* Push a new job onto the queue after a delay.
|
45
|
*
|
46
|
* @param string $queue
|
47
|
* @param \DateTime|int $delay
|
48
|
* @param string $job
|
49
|
* @param mixed $data
|
50
|
* @return mixed
|
51
|
*/
|
52
|
public function laterOn($queue, $delay, $job, $data = '')
|
53
|
{
|
54
|
return $this->later($delay, $job, $data, $queue);
|
55
|
}
|
56
|
|
57
|
/**
|
58
|
* Push an array of jobs onto the queue.
|
59
|
*
|
60
|
* @param array $jobs
|
61
|
* @param mixed $data
|
62
|
* @param string $queue
|
63
|
* @return mixed
|
64
|
*/
|
65
|
public function bulk($jobs, $data = '', $queue = null)
|
66
|
{
|
67
|
foreach ((array) $jobs as $job) {
|
68
|
$this->push($job, $data, $queue);
|
69
|
}
|
70
|
}
|
71
|
|
72
|
/**
|
73
|
* Create a payload string from the given job and data.
|
74
|
*
|
75
|
* @param string $job
|
76
|
* @param mixed $data
|
77
|
* @param string $queue
|
78
|
* @return string
|
79
|
*/
|
80
|
protected function createPayload($job, $data = '', $queue = null)
|
81
|
{
|
82
|
if ($job instanceof Closure) {
|
83
|
return json_encode($this->createClosurePayload($job, $data));
|
84
|
}
|
85
|
|
86
|
if (is_object($job)) {
|
87
|
return json_encode([
|
88
|
'job' => 'Illuminate\Queue\CallQueuedHandler@call',
|
89
|
'data' => ['commandName' => get_class($job), 'command' => serialize(clone $job)],
|
90
|
]);
|
91
|
}
|
92
|
|
93
|
return json_encode($this->createPlainPayload($job, $data));
|
94
|
}
|
95
|
|
96
|
/**
|
97
|
* Create a typical, "plain" queue payload array.
|
98
|
*
|
99
|
* @param string $job
|
100
|
* @param mixed $data
|
101
|
* @return array
|
102
|
*/
|
103
|
protected function createPlainPayload($job, $data)
|
104
|
{
|
105
|
return ['job' => $job, 'data' => $this->prepareQueueableEntities($data)];
|
106
|
}
|
107
|
|
108
|
/**
|
109
|
* Prepare any queueable entities for storage in the queue.
|
110
|
*
|
111
|
* @param mixed $data
|
112
|
* @return mixed
|
113
|
*/
|
114
|
protected function prepareQueueableEntities($data)
|
115
|
{
|
116
|
if ($data instanceof QueueableEntity) {
|
117
|
return $this->prepareQueueableEntity($data);
|
118
|
}
|
119
|
|
120
|
if (is_array($data)) {
|
121
|
$data = array_map(function ($d) {
|
122
|
if (is_array($d)) {
|
123
|
return $this->prepareQueueableEntities($d);
|
124
|
}
|
125
|
|
126
|
return $this->prepareQueueableEntity($d);
|
127
|
}, $data);
|
128
|
}
|
129
|
|
130
|
return $data;
|
131
|
}
|
132
|
|
133
|
/**
|
134
|
* Prepare a single queueable entity for storage on the queue.
|
135
|
*
|
136
|
* @param mixed $value
|
137
|
* @return mixed
|
138
|
*/
|
139
|
protected function prepareQueueableEntity($value)
|
140
|
{
|
141
|
if ($value instanceof QueueableEntity) {
|
142
|
return '::entity::|'.get_class($value).'|'.$value->getQueueableId();
|
143
|
}
|
144
|
|
145
|
return $value;
|
146
|
}
|
147
|
|
148
|
/**
|
149
|
* Create a payload string for the given Closure job.
|
150
|
*
|
151
|
* @param \Closure $job
|
152
|
* @param mixed $data
|
153
|
* @return array
|
154
|
*/
|
155
|
protected function createClosurePayload($job, $data)
|
156
|
{
|
157
|
$closure = $this->getEncrypter()->encrypt((new Serializer)->serialize($job));
|
158
|
|
159
|
return ['job' => 'IlluminateQueueClosure', 'data' => compact('closure')];
|
160
|
}
|
161
|
|
162
|
/**
|
163
|
* Set additional meta on a payload string.
|
164
|
*
|
165
|
* @param string $payload
|
166
|
* @param string $key
|
167
|
* @param string $value
|
168
|
* @return string
|
169
|
*/
|
170
|
protected function setMeta($payload, $key, $value)
|
171
|
{
|
172
|
$payload = json_decode($payload, true);
|
173
|
|
174
|
return json_encode(Arr::set($payload, $key, $value));
|
175
|
}
|
176
|
|
177
|
/**
|
178
|
* Calculate the number of seconds with the given delay.
|
179
|
*
|
180
|
* @param \DateTime|int $delay
|
181
|
* @return int
|
182
|
*/
|
183
|
protected function getSeconds($delay)
|
184
|
{
|
185
|
if ($delay instanceof DateTime) {
|
186
|
return max(0, $delay->getTimestamp() - $this->getTime());
|
187
|
}
|
188
|
|
189
|
return (int) $delay;
|
190
|
}
|
191
|
|
192
|
/**
|
193
|
* Get the current UNIX timestamp.
|
194
|
*
|
195
|
* @return int
|
196
|
*/
|
197
|
protected function getTime()
|
198
|
{
|
199
|
return time();
|
200
|
}
|
201
|
|
202
|
/**
|
203
|
* Set the IoC container instance.
|
204
|
*
|
205
|
* @param \Illuminate\Container\Container $container
|
206
|
* @return void
|
207
|
*/
|
208
|
public function setContainer(Container $container)
|
209
|
{
|
210
|
$this->container = $container;
|
211
|
}
|
212
|
|
213
|
/**
|
214
|
* Get the encrypter implementation.
|
215
|
*
|
216
|
* @return \Illuminate\Contracts\Encryption\Encrypter
|
217
|
*
|
218
|
* @throws \Exception
|
219
|
*/
|
220
|
protected function getEncrypter()
|
221
|
{
|
222
|
if (is_null($this->encrypter)) {
|
223
|
throw new Exception('No encrypter has been set on the Queue.');
|
224
|
}
|
225
|
|
226
|
return $this->encrypter;
|
227
|
}
|
228
|
|
229
|
/**
|
230
|
* Set the encrypter implementation.
|
231
|
*
|
232
|
* @param \Illuminate\Contracts\Encryption\Encrypter $encrypter
|
233
|
* @return void
|
234
|
*/
|
235
|
public function setEncrypter(Encrypter $encrypter)
|
236
|
{
|
237
|
$this->encrypter = $encrypter;
|
238
|
}
|
239
|
}
|