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\Bus\Events\BatchDispatched;
7
use Illuminate\Contracts\Container\Container;
8
use Illuminate\Contracts\Events\Dispatcher as EventDispatcher;
9
use Illuminate\Support\Arr;
10
use Illuminate\Support\Collection;
11
use Laravel\SerializableClosure\SerializableClosure;
12
use Throwable;
13
 
14
class PendingBatch
15
{
16
    /**
17
     * The IoC container instance.
18
     *
19
     * @var \Illuminate\Contracts\Container\Container
20
     */
21
    protected $container;
22
 
23
    /**
24
     * The batch name.
25
     *
26
     * @var string
27
     */
28
    public $name = '';
29
 
30
    /**
31
     * The jobs that belong to the batch.
32
     *
33
     * @var \Illuminate\Support\Collection
34
     */
35
    public $jobs;
36
 
37
    /**
38
     * The batch options.
39
     *
40
     * @var array
41
     */
42
    public $options = [];
43
 
44
    /**
45
     * Create a new pending batch instance.
46
     *
47
     * @param  \Illuminate\Contracts\Container\Container  $container
48
     * @param  \Illuminate\Support\Collection  $jobs
49
     * @return void
50
     */
51
    public function __construct(Container $container, Collection $jobs)
52
    {
53
        $this->container = $container;
54
        $this->jobs = $jobs;
55
    }
56
 
57
    /**
58
     * Add jobs to the batch.
59
     *
60
     * @param  iterable|object|array  $jobs
61
     * @return $this
62
     */
63
    public function add($jobs)
64
    {
65
        $jobs = is_iterable($jobs) ? $jobs : Arr::wrap($jobs);
66
 
67
        foreach ($jobs as $job) {
68
            $this->jobs->push($job);
69
        }
70
 
71
        return $this;
72
    }
73
 
74
    /**
75
     * Add a callback to be executed after all jobs in the batch have executed successfully.
76
     *
77
     * @param  callable  $callback
78
     * @return $this
79
     */
80
    public function then($callback)
81
    {
82
        $this->options['then'][] = $callback instanceof Closure
83
                        ? new SerializableClosure($callback)
84
                        : $callback;
85
 
86
        return $this;
87
    }
88
 
89
    /**
90
     * Get the "then" callbacks that have been registered with the pending batch.
91
     *
92
     * @return array
93
     */
94
    public function thenCallbacks()
95
    {
96
        return $this->options['then'] ?? [];
97
    }
98
 
99
    /**
100
     * Add a callback to be executed after the first failing job in the batch.
101
     *
102
     * @param  callable  $callback
103
     * @return $this
104
     */
105
    public function catch($callback)
106
    {
107
        $this->options['catch'][] = $callback instanceof Closure
108
                    ? new SerializableClosure($callback)
109
                    : $callback;
110
 
111
        return $this;
112
    }
113
 
114
    /**
115
     * Get the "catch" callbacks that have been registered with the pending batch.
116
     *
117
     * @return array
118
     */
119
    public function catchCallbacks()
120
    {
121
        return $this->options['catch'] ?? [];
122
    }
123
 
124
    /**
125
     * Add a callback to be executed after the batch has finished executing.
126
     *
127
     * @param  callable  $callback
128
     * @return $this
129
     */
130
    public function finally($callback)
131
    {
132
        $this->options['finally'][] = $callback instanceof Closure
133
                    ? new SerializableClosure($callback)
134
                    : $callback;
135
 
136
        return $this;
137
    }
138
 
139
    /**
140
     * Get the "finally" callbacks that have been registered with the pending batch.
141
     *
142
     * @return array
143
     */
144
    public function finallyCallbacks()
145
    {
146
        return $this->options['finally'] ?? [];
147
    }
148
 
149
    /**
150
     * Indicate that the batch should not be cancelled when a job within the batch fails.
151
     *
152
     * @param  bool  $allowFailures
153
     * @return $this
154
     */
155
    public function allowFailures($allowFailures = true)
156
    {
157
        $this->options['allowFailures'] = $allowFailures;
158
 
159
        return $this;
160
    }
161
 
162
    /**
163
     * Determine if the pending batch allows jobs to fail without cancelling the batch.
164
     *
165
     * @return bool
166
     */
167
    public function allowsFailures()
168
    {
169
        return Arr::get($this->options, 'allowFailures', false) === true;
170
    }
171
 
172
    /**
173
     * Set the name for the batch.
174
     *
175
     * @param  string  $name
176
     * @return $this
177
     */
178
    public function name(string $name)
179
    {
180
        $this->name = $name;
181
 
182
        return $this;
183
    }
184
 
185
    /**
186
     * Specify the queue connection that the batched jobs should run on.
187
     *
188
     * @param  string  $connection
189
     * @return $this
190
     */
191
    public function onConnection(string $connection)
192
    {
193
        $this->options['connection'] = $connection;
194
 
195
        return $this;
196
    }
197
 
198
    /**
199
     * Get the connection used by the pending batch.
200
     *
201
     * @return string|null
202
     */
203
    public function connection()
204
    {
205
        return $this->options['connection'] ?? null;
206
    }
207
 
208
    /**
209
     * Specify the queue that the batched jobs should run on.
210
     *
211
     * @param  string  $queue
212
     * @return $this
213
     */
214
    public function onQueue(string $queue)
215
    {
216
        $this->options['queue'] = $queue;
217
 
218
        return $this;
219
    }
220
 
221
    /**
222
     * Get the queue used by the pending batch.
223
     *
224
     * @return string|null
225
     */
226
    public function queue()
227
    {
228
        return $this->options['queue'] ?? null;
229
    }
230
 
231
    /**
232
     * Add additional data into the batch's options array.
233
     *
234
     * @param  string  $key
235
     * @param  mixed  $value
236
     * @return $this
237
     */
238
    public function withOption(string $key, $value)
239
    {
240
        $this->options[$key] = $value;
241
 
242
        return $this;
243
    }
244
 
245
    /**
246
     * Dispatch the batch.
247
     *
248
     * @return \Illuminate\Bus\Batch
249
     *
250
     * @throws \Throwable
251
     */
252
    public function dispatch()
253
    {
254
        $repository = $this->container->make(BatchRepository::class);
255
 
256
        try {
257
            $batch = $repository->store($this);
258
 
259
            $batch = $batch->add($this->jobs);
260
        } catch (Throwable $e) {
261
            if (isset($batch)) {
262
                $repository->delete($batch->id);
263
            }
264
 
265
            throw $e;
266
        }
267
 
268
        $this->container->make(EventDispatcher::class)->dispatch(
269
            new BatchDispatched($batch)
270
        );
271
 
272
        return $batch;
273
    }
274
 
275
    /**
276
     * Dispatch the batch after the response is sent to the browser.
277
     *
278
     * @return \Illuminate\Bus\Batch
279
     */
280
    public function dispatchAfterResponse()
281
    {
282
        $repository = $this->container->make(BatchRepository::class);
283
 
284
        $batch = $repository->store($this);
285
 
286
        if ($batch) {
287
            $this->container->terminating(function () use ($batch) {
288
                $this->dispatchExistingBatch($batch);
289
            });
290
        }
291
 
292
        return $batch;
293
    }
294
 
295
    /**
296
     * Dispatch an existing batch.
297
     *
298
     * @param  \Illuminate\Bus\Batch  $batch
299
     * @return void
300
     *
301
     * @throws \Throwable
302
     */
303
    protected function dispatchExistingBatch($batch)
304
    {
305
        try {
306
            $batch = $batch->add($this->jobs);
307
        } catch (Throwable $e) {
308
            if (isset($batch)) {
309
                $batch->delete();
310
            }
311
 
312
            throw $e;
313
        }
314
 
315
        $this->container->make(EventDispatcher::class)->dispatch(
316
            new BatchDispatched($batch)
317
        );
318
    }
319
}