Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
namespace Illuminate\Bus;
4
 
5
use Closure;
6
use Illuminate\Queue\CallQueuedClosure;
7
use Illuminate\Support\Arr;
8
use RuntimeException;
9
 
10
trait Queueable
11
{
12
    /**
13
     * The name of the connection the job should be sent to.
14
     *
15
     * @var string|null
16
     */
17
    public $connection;
18
 
19
    /**
20
     * The name of the queue the job should be sent to.
21
     *
22
     * @var string|null
23
     */
24
    public $queue;
25
 
26
    /**
27
     * The name of the connection the chain should be sent to.
28
     *
29
     * @var string|null
30
     */
31
    public $chainConnection;
32
 
33
    /**
34
     * The name of the queue the chain should be sent to.
35
     *
36
     * @var string|null
37
     */
38
    public $chainQueue;
39
 
40
    /**
41
     * The callbacks to be executed on chain failure.
42
     *
43
     * @var array|null
44
     */
45
    public $chainCatchCallbacks;
46
 
47
    /**
48
     * The number of seconds before the job should be made available.
49
     *
50
     * @var \DateTimeInterface|\DateInterval|array|int|null
51
     */
52
    public $delay;
53
 
54
    /**
55
     * Indicates whether the job should be dispatched after all database transactions have committed.
56
     *
57
     * @var bool|null
58
     */
59
    public $afterCommit;
60
 
61
    /**
62
     * The middleware the job should be dispatched through.
63
     *
64
     * @var array
65
     */
66
    public $middleware = [];
67
 
68
    /**
69
     * The jobs that should run if this job is successful.
70
     *
71
     * @var array
72
     */
73
    public $chained = [];
74
 
75
    /**
76
     * Set the desired connection for the job.
77
     *
78
     * @param  string|null  $connection
79
     * @return $this
80
     */
81
    public function onConnection($connection)
82
    {
83
        $this->connection = $connection;
84
 
85
        return $this;
86
    }
87
 
88
    /**
89
     * Set the desired queue for the job.
90
     *
91
     * @param  string|null  $queue
92
     * @return $this
93
     */
94
    public function onQueue($queue)
95
    {
96
        $this->queue = $queue;
97
 
98
        return $this;
99
    }
100
 
101
    /**
102
     * Set the desired connection for the chain.
103
     *
104
     * @param  string|null  $connection
105
     * @return $this
106
     */
107
    public function allOnConnection($connection)
108
    {
109
        $this->chainConnection = $connection;
110
        $this->connection = $connection;
111
 
112
        return $this;
113
    }
114
 
115
    /**
116
     * Set the desired queue for the chain.
117
     *
118
     * @param  string|null  $queue
119
     * @return $this
120
     */
121
    public function allOnQueue($queue)
122
    {
123
        $this->chainQueue = $queue;
124
        $this->queue = $queue;
125
 
126
        return $this;
127
    }
128
 
129
    /**
130
     * Set the desired delay in seconds for the job.
131
     *
132
     * @param  \DateTimeInterface|\DateInterval|array|int|null  $delay
133
     * @return $this
134
     */
135
    public function delay($delay)
136
    {
137
        $this->delay = $delay;
138
 
139
        return $this;
140
    }
141
 
142
    /**
143
     * Indicate that the job should be dispatched after all database transactions have committed.
144
     *
145
     * @return $this
146
     */
147
    public function afterCommit()
148
    {
149
        $this->afterCommit = true;
150
 
151
        return $this;
152
    }
153
 
154
    /**
155
     * Indicate that the job should not wait until database transactions have been committed before dispatching.
156
     *
157
     * @return $this
158
     */
159
    public function beforeCommit()
160
    {
161
        $this->afterCommit = false;
162
 
163
        return $this;
164
    }
165
 
166
    /**
167
     * Specify the middleware the job should be dispatched through.
168
     *
169
     * @param  array|object  $middleware
170
     * @return $this
171
     */
172
    public function through($middleware)
173
    {
174
        $this->middleware = Arr::wrap($middleware);
175
 
176
        return $this;
177
    }
178
 
179
    /**
180
     * Set the jobs that should run if this job is successful.
181
     *
182
     * @param  array  $chain
183
     * @return $this
184
     */
185
    public function chain($chain)
186
    {
187
        $this->chained = collect($chain)->map(function ($job) {
188
            return $this->serializeJob($job);
189
        })->all();
190
 
191
        return $this;
192
    }
193
 
194
    /**
195
     * Prepend a job to the current chain so that it is run after the currently running job.
196
     *
197
     * @param  mixed  $job
198
     * @return $this
199
     */
200
    public function prependToChain($job)
201
    {
202
        $this->chained = Arr::prepend($this->chained, $this->serializeJob($job));
203
 
204
        return $this;
205
    }
206
 
207
    /**
208
     * Append a job to the end of the current chain.
209
     *
210
     * @param  mixed  $job
211
     * @return $this
212
     */
213
    public function appendToChain($job)
214
    {
215
        $this->chained = array_merge($this->chained, [$this->serializeJob($job)]);
216
 
217
        return $this;
218
    }
219
 
220
    /**
221
     * Serialize a job for queuing.
222
     *
223
     * @param  mixed  $job
224
     * @return string
225
     *
226
     * @throws \RuntimeException
227
     */
228
    protected function serializeJob($job)
229
    {
230
        if ($job instanceof Closure) {
231
            if (! class_exists(CallQueuedClosure::class)) {
232
                throw new RuntimeException(
233
                    'To enable support for closure jobs, please install the illuminate/queue package.'
234
                );
235
            }
236
 
237
            $job = CallQueuedClosure::create($job);
238
        }
239
 
240
        return serialize($job);
241
    }
242
 
243
    /**
244
     * Dispatch the next job on the chain.
245
     *
246
     * @return void
247
     */
248
    public function dispatchNextJobInChain()
249
    {
250
        if (! empty($this->chained)) {
251
            dispatch(tap(unserialize(array_shift($this->chained)), function ($next) {
252
                $next->chained = $this->chained;
253
 
254
                $next->onConnection($next->connection ?: $this->chainConnection);
255
                $next->onQueue($next->queue ?: $this->chainQueue);
256
 
257
                $next->chainConnection = $this->chainConnection;
258
                $next->chainQueue = $this->chainQueue;
259
                $next->chainCatchCallbacks = $this->chainCatchCallbacks;
260
            }));
261
        }
262
    }
263
 
264
    /**
265
     * Invoke all of the chain's failed job callbacks.
266
     *
267
     * @param  \Throwable  $e
268
     * @return void
269
     */
270
    public function invokeChainCatchCallbacks($e)
271
    {
272
        collect($this->chainCatchCallbacks)->each(function ($callback) use ($e) {
273
            $callback($e);
274
        });
275
    }
276
}