Laravel-Dispatcher 发表于 2020-04-03 | 更新于 2020-04-20 | 分类于 PHP 本文字数: 3.8k | 阅读时长 ≈ 3 分钟 思考并回答以下问题: Dispatcher Illuminate\Contracts\Events\Dispatcher.php 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182<?phpnamespace Illuminate\Contracts\Events;interface Dispatcher{ /** * Register an event listener with the dispatcher. * * @param string|array $events * @param mixed $listener * @return void */ public function listen($events, $listener); /** * Determine if a given event has listeners. * * @param string $eventName * @return bool */ public function hasListeners($eventName); /** * Register an event subscriber with the dispatcher. * * @param object|string $subscriber * @return void */ public function subscribe($subscriber); /** * Dispatch an event until the first non-null response is returned. * * @param string|object $event * @param mixed $payload * @return array|null */ public function until($event, $payload = []); /** * Dispatch an event and call the listeners. * * @param string|object $event * @param mixed $payload * @param bool $halt * @return array|null */ public function dispatch($event, $payload = [], $halt = false); /** * Register an event and payload to be fired later. * * @param string $event * @param array $payload * @return void */ public function push($event, $payload = []); /** * Flush a set of pushed events. * * @param string $event * @return void */ public function flush($event); /** * Remove a set of listeners from the dispatcher. * * @param string $event * @return void */ public function forget($event); /** * Forget all of the queued listeners. * * @return void */ public function forgetPushed();} Illuminate\Events\Dispatcher.php 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859<?phpnamespace Illuminate\Events;use Exception;use Illuminate\Container\Container;use Illuminate\Contracts\Broadcasting\Factory as BroadcastFactory;use Illuminate\Contracts\Broadcasting\ShouldBroadcast;use Illuminate\Contracts\Container\Container as ContainerContract;use Illuminate\Contracts\Events\Dispatcher as DispatcherContract;use Illuminate\Contracts\Queue\ShouldQueue;use Illuminate\Support\Arr;use Illuminate\Support\Str;use ReflectionClass;class Dispatcher implements DispatcherContract{ /** * Register an event listener with the dispatcher. * * @param string|array $events * @param mixed $listener * @return void */ public function listen($events, $listener) { foreach ((array)$events as $event) { if (Str::containers($event, '*')) { $this->setupWildcardListen($event, $listener); } else { $this->listeners[$event][] = $this->makeListener($listener); } } } /** * Create a callable for putting an event handler on the queue. * * @param string $class * @param string $method * @return \Closure */ protected function createQueueHandlerCallable($class, $method) { return function () use ($class, $method) { $arguments = array_map(function($a) { return is_object($a) ? clone $a : $a; }, func_get_arg()); if ($this->handlerWantsToBeQueued($class, $arguments )) { $this->queueHandler($class, $method, $arguments); } }; }}