|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Illuminate\Events; |
| 4 | + |
| 5 | +use Illuminate\Container\Container; |
| 6 | +use Illuminate\Contracts\Queue\Job; |
| 7 | +use Illuminate\Queue\InteractsWithQueue; |
| 8 | +use Illuminate\Contracts\Queue\ShouldQueue; |
| 9 | + |
| 10 | +class CallQueuedListener implements ShouldQueue |
| 11 | +{ |
| 12 | + use InteractsWithQueue; |
| 13 | + |
| 14 | + /** |
| 15 | + * The listener class name. |
| 16 | + * |
| 17 | + * @var string |
| 18 | + */ |
| 19 | + public $class; |
| 20 | + |
| 21 | + /** |
| 22 | + * The listener method. |
| 23 | + * |
| 24 | + * @var string |
| 25 | + */ |
| 26 | + public $method; |
| 27 | + |
| 28 | + /** |
| 29 | + * The data to be passed to the listener. |
| 30 | + * |
| 31 | + * @var string |
| 32 | + */ |
| 33 | + public $data; |
| 34 | + |
| 35 | + /** |
| 36 | + * Create a new job instance. |
| 37 | + * |
| 38 | + * @param string $class |
| 39 | + * @param string $method |
| 40 | + * @param string $data |
| 41 | + * @return void |
| 42 | + */ |
| 43 | + public function __construct($class, $method, $data) |
| 44 | + { |
| 45 | + $this->data = $data; |
| 46 | + $this->class = $class; |
| 47 | + $this->method = $method; |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * Handle the queued job. |
| 52 | + * |
| 53 | + * @param \Illuminate\Container\Container $container |
| 54 | + * @return void |
| 55 | + */ |
| 56 | + public function handle(Container $container) |
| 57 | + { |
| 58 | + $handler = $this->setJobInstanceIfNecessary( |
| 59 | + $this->job, $container->make($this->class) |
| 60 | + ); |
| 61 | + |
| 62 | + call_user_func_array( |
| 63 | + [$handler, $this->method], unserialize($this->data) |
| 64 | + ); |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * Set the job instance of the given class if necessary. |
| 69 | + * |
| 70 | + * @param \Illuminate\Contracts\Queue\Job $job |
| 71 | + * @param mixed $instance |
| 72 | + * @return mixed |
| 73 | + */ |
| 74 | + protected function setJobInstanceIfNecessary(Job $job, $instance) |
| 75 | + { |
| 76 | + if (in_array(InteractsWithQueue::class, class_uses_recursive(get_class($instance)))) { |
| 77 | + $instance->setJob($job); |
| 78 | + } |
| 79 | + |
| 80 | + return $instance; |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * Call the failed method on the job instance. |
| 85 | + * |
| 86 | + * The event instance and the exception will be passed. |
| 87 | + * |
| 88 | + * @param \Exception $e |
| 89 | + * @return void |
| 90 | + */ |
| 91 | + public function failed($e) |
| 92 | + { |
| 93 | + $handler = Container::getInstance()->make($this->class); |
| 94 | + |
| 95 | + $parameters = array_merge(unserialize($this->data), [$e]); |
| 96 | + |
| 97 | + if (method_exists($handler, 'failed')) { |
| 98 | + call_user_func_array([$handler, 'failed'], $parameters); |
| 99 | + } |
| 100 | + } |
| 101 | +} |
0 commit comments