| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178 |
- <?php
- namespace Illuminate\Events;
- use Closure;
- use Illuminate\Support\Collection;
- use Laravel\SerializableClosure\SerializableClosure;
- use function Illuminate\Support\enum_value;
- class QueuedClosure
- {
- /**
- * The underlying Closure.
- *
- * @var \Closure
- */
- public $closure;
- /**
- * The name of the connection the job should be sent to.
- *
- * @var string|null
- */
- public $connection;
- /**
- * The name of the queue the job should be sent to.
- *
- * @var string|null
- */
- public $queue;
- /**
- * The job "group" the job should be sent to.
- *
- * @var string|null
- */
- public $messageGroup;
- /**
- * The job deduplicator callback the job should use to generate the deduplication ID.
- *
- * @var \Laravel\SerializableClosure\SerializableClosure|null
- */
- public $deduplicator;
- /**
- * The number of seconds before the job should be made available.
- *
- * @var \DateTimeInterface|\DateInterval|int|null
- */
- public $delay;
- /**
- * All of the "catch" callbacks for the queued closure.
- *
- * @var array
- */
- public $catchCallbacks = [];
- /**
- * Create a new queued closure event listener resolver.
- *
- * @param \Closure $closure
- */
- public function __construct(Closure $closure)
- {
- $this->closure = $closure;
- }
- /**
- * Set the desired connection for the job.
- *
- * @param \UnitEnum|string|null $connection
- * @return $this
- */
- public function onConnection($connection)
- {
- $this->connection = enum_value($connection);
- return $this;
- }
- /**
- * Set the desired queue for the job.
- *
- * @param \UnitEnum|string|null $queue
- * @return $this
- */
- public function onQueue($queue)
- {
- $this->queue = enum_value($queue);
- return $this;
- }
- /**
- * Set the desired job "group".
- *
- * This feature is only supported by some queues, such as Amazon SQS.
- *
- * @param \UnitEnum|string $group
- * @return $this
- */
- public function onGroup($group)
- {
- $this->messageGroup = enum_value($group);
- return $this;
- }
- /**
- * Set the desired job deduplicator callback.
- *
- * This feature is only supported by some queues, such as Amazon SQS FIFO.
- *
- * @param callable|null $deduplicator
- * @return $this
- */
- public function withDeduplicator($deduplicator)
- {
- $this->deduplicator = $deduplicator instanceof Closure
- ? new SerializableClosure($deduplicator)
- : $deduplicator;
- return $this;
- }
- /**
- * Set the desired delay in seconds for the job.
- *
- * @param \DateTimeInterface|\DateInterval|int|null $delay
- * @return $this
- */
- public function delay($delay)
- {
- $this->delay = $delay;
- return $this;
- }
- /**
- * Specify a callback that should be invoked if the queued listener job fails.
- *
- * @param \Closure $closure
- * @return $this
- */
- public function catch(Closure $closure)
- {
- $this->catchCallbacks[] = $closure;
- return $this;
- }
- /**
- * Resolve the actual event listener callback.
- *
- * @return \Closure
- */
- public function resolve()
- {
- return function (...$arguments) {
- dispatch(new CallQueuedListener(InvokeQueuedClosure::class, 'handle', [
- 'closure' => new SerializableClosure($this->closure),
- 'arguments' => $arguments,
- 'catch' => (new Collection($this->catchCallbacks))
- ->map(fn ($callback) => new SerializableClosure($callback))
- ->all(),
- ]))
- ->onConnection($this->connection)
- ->onQueue($this->queue)
- ->delay($this->delay)
- ->onGroup($this->messageGroup)
- ->withDeduplicator($this->deduplicator);
- };
- }
- }
|