Subversion-Projekte lars-tiefland.laravel_shop

Revision

Revision 148 | Details | Vergleich mit vorheriger | 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\Contracts\Bus\QueueingDispatcher;
7
use Illuminate\Contracts\Container\Container;
8
use Illuminate\Contracts\Queue\Queue;
9
use Illuminate\Contracts\Queue\ShouldQueue;
10
use Illuminate\Foundation\Bus\PendingChain;
11
use Illuminate\Pipeline\Pipeline;
12
use Illuminate\Queue\InteractsWithQueue;
13
use Illuminate\Queue\Jobs\SyncJob;
14
use Illuminate\Support\Collection;
15
use RuntimeException;
16
 
17
class Dispatcher implements QueueingDispatcher
18
{
19
    /**
20
     * The container implementation.
21
     *
22
     * @var \Illuminate\Contracts\Container\Container
23
     */
24
    protected $container;
25
 
26
    /**
27
     * The pipeline instance for the bus.
28
     *
29
     * @var \Illuminate\Pipeline\Pipeline
30
     */
31
    protected $pipeline;
32
 
33
    /**
34
     * The pipes to send commands through before dispatching.
35
     *
36
     * @var array
37
     */
38
    protected $pipes = [];
39
 
40
    /**
41
     * The command to handler mapping for non-self-handling events.
42
     *
43
     * @var array
44
     */
45
    protected $handlers = [];
46
 
47
    /**
48
     * The queue resolver callback.
49
     *
50
     * @var \Closure|null
51
     */
52
    protected $queueResolver;
53
 
54
    /**
55
     * Create a new command dispatcher instance.
56
     *
57
     * @param  \Illuminate\Contracts\Container\Container  $container
58
     * @param  \Closure|null  $queueResolver
59
     * @return void
60
     */
61
    public function __construct(Container $container, Closure $queueResolver = null)
62
    {
63
        $this->container = $container;
64
        $this->queueResolver = $queueResolver;
65
        $this->pipeline = new Pipeline($container);
66
    }
67
 
68
    /**
69
     * Dispatch a command to its appropriate handler.
70
     *
71
     * @param  mixed  $command
72
     * @return mixed
73
     */
74
    public function dispatch($command)
75
    {
76
        return $this->queueResolver && $this->commandShouldBeQueued($command)
77
                        ? $this->dispatchToQueue($command)
78
                        : $this->dispatchNow($command);
79
    }
80
 
81
    /**
82
     * Dispatch a command to its appropriate handler in the current process.
83
     *
84
     * Queueable jobs will be dispatched to the "sync" queue.
85
     *
86
     * @param  mixed  $command
87
     * @param  mixed  $handler
88
     * @return mixed
89
     */
90
    public function dispatchSync($command, $handler = null)
91
    {
92
        if ($this->queueResolver &&
93
            $this->commandShouldBeQueued($command) &&
94
            method_exists($command, 'onConnection')) {
95
            return $this->dispatchToQueue($command->onConnection('sync'));
96
        }
97
 
98
        return $this->dispatchNow($command, $handler);
99
    }
100
 
101
    /**
102
     * Dispatch a command to its appropriate handler in the current process without using the synchronous queue.
103
     *
104
     * @param  mixed  $command
105
     * @param  mixed  $handler
106
     * @return mixed
107
     */
108
    public function dispatchNow($command, $handler = null)
109
    {
110
        $uses = class_uses_recursive($command);
111
 
112
        if (in_array(InteractsWithQueue::class, $uses) &&
113
            in_array(Queueable::class, $uses) &&
114
            ! $command->job) {
115
            $command->setJob(new SyncJob($this->container, json_encode([]), 'sync', 'sync'));
116
        }
117
 
118
        if ($handler || $handler = $this->getCommandHandler($command)) {
119
            $callback = function ($command) use ($handler) {
120
                $method = method_exists($handler, 'handle') ? 'handle' : '__invoke';
121
 
122
                return $handler->{$method}($command);
123
            };
124
        } else {
125
            $callback = function ($command) {
126
                $method = method_exists($command, 'handle') ? 'handle' : '__invoke';
127
 
128
                return $this->container->call([$command, $method]);
129
            };
130
        }
131
 
132
        return $this->pipeline->send($command)->through($this->pipes)->then($callback);
133
    }
134
 
135
    /**
136
     * Attempt to find the batch with the given ID.
137
     *
138
     * @param  string  $batchId
139
     * @return \Illuminate\Bus\Batch|null
140
     */
141
    public function findBatch(string $batchId)
142
    {
143
        return $this->container->make(BatchRepository::class)->find($batchId);
144
    }
145
 
146
    /**
147
     * Create a new batch of queueable jobs.
148
     *
149
     * @param  \Illuminate\Support\Collection|array|mixed  $jobs
150
     * @return \Illuminate\Bus\PendingBatch
151
     */
152
    public function batch($jobs)
153
    {
154
        return new PendingBatch($this->container, Collection::wrap($jobs));
155
    }
156
 
157
    /**
158
     * Create a new chain of queueable jobs.
159
     *
160
     * @param  \Illuminate\Support\Collection|array  $jobs
161
     * @return \Illuminate\Foundation\Bus\PendingChain
162
     */
163
    public function chain($jobs)
164
    {
165
        $jobs = Collection::wrap($jobs);
166
 
167
        return new PendingChain($jobs->shift(), $jobs->toArray());
168
    }
169
 
170
    /**
171
     * Determine if the given command has a handler.
172
     *
173
     * @param  mixed  $command
174
     * @return bool
175
     */
176
    public function hasCommandHandler($command)
177
    {
178
        return array_key_exists(get_class($command), $this->handlers);
179
    }
180
 
181
    /**
182
     * Retrieve the handler for a command.
183
     *
184
     * @param  mixed  $command
185
     * @return bool|mixed
186
     */
187
    public function getCommandHandler($command)
188
    {
189
        if ($this->hasCommandHandler($command)) {
190
            return $this->container->make($this->handlers[get_class($command)]);
191
        }
192
 
193
        return false;
194
    }
195
 
196
    /**
197
     * Determine if the given command should be queued.
198
     *
199
     * @param  mixed  $command
200
     * @return bool
201
     */
202
    protected function commandShouldBeQueued($command)
203
    {
204
        return $command instanceof ShouldQueue;
205
    }
206
 
207
    /**
208
     * Dispatch a command to its appropriate handler behind a queue.
209
     *
210
     * @param  mixed  $command
211
     * @return mixed
212
     *
213
     * @throws \RuntimeException
214
     */
215
    public function dispatchToQueue($command)
216
    {
217
        $connection = $command->connection ?? null;
218
 
219
        $queue = call_user_func($this->queueResolver, $connection);
220
 
221
        if (! $queue instanceof Queue) {
222
            throw new RuntimeException('Queue resolver did not return a Queue implementation.');
223
        }
224
 
225
        if (method_exists($command, 'queue')) {
226
            return $command->queue($queue, $command);
227
        }
228
 
229
        return $this->pushCommandToQueue($queue, $command);
230
    }
231
 
232
    /**
233
     * Push the command onto the given queue instance.
234
     *
235
     * @param  \Illuminate\Contracts\Queue\Queue  $queue
236
     * @param  mixed  $command
237
     * @return mixed
238
     */
239
    protected function pushCommandToQueue($queue, $command)
240
    {
241
        if (isset($command->queue, $command->delay)) {
242
            return $queue->laterOn($command->queue, $command->delay, $command);
243
        }
244
 
245
        if (isset($command->queue)) {
246
            return $queue->pushOn($command->queue, $command);
247
        }
248
 
249
        if (isset($command->delay)) {
250
            return $queue->later($command->delay, $command);
251
        }
252
 
253
        return $queue->push($command);
254
    }
255
 
256
    /**
257
     * Dispatch a command to its appropriate handler after the current process.
258
     *
259
     * @param  mixed  $command
260
     * @param  mixed  $handler
261
     * @return void
262
     */
263
    public function dispatchAfterResponse($command, $handler = null)
264
    {
265
        $this->container->terminating(function () use ($command, $handler) {
1464 lars 266
            $this->dispatchSync($command, $handler);
148 lars 267
        });
268
    }
269
 
270
    /**
271
     * Set the pipes through which commands should be piped before dispatching.
272
     *
273
     * @param  array  $pipes
274
     * @return $this
275
     */
276
    public function pipeThrough(array $pipes)
277
    {
278
        $this->pipes = $pipes;
279
 
280
        return $this;
281
    }
282
 
283
    /**
284
     * Map a command to a handler.
285
     *
286
     * @param  array  $map
287
     * @return $this
288
     */
289
    public function map(array $map)
290
    {
291
        $this->handlers = array_merge($this->handlers, $map);
292
 
293
        return $this;
294
    }
295
}