[laravel源码]Dispatcher/dispath&fire解析
说明#
本章说明事件分配 dispatch
的流程。
源码#
vendor\laravel\framework\src\Illuminate\Events\Dispatcher.php
public function fire($event, $payload = [], $halt = false)
{
return $this->dispatch($event, $payload, $halt);
}
public function dispatch($event, $payload = [], $halt = false)
{
// 1. 数据整理
list($event, $payload) = $this->parseEventAndPayload(
$event, $payload
);
// 2. 判断载荷是否有广播事件
if ($this->shouldBroadcast($payload)) {
$this->broadcastEvent($payload[0]);
}
$responses = [];
// 3. 获取监听器并调用
foreach ($this->getListeners($event) as $listener) {
$response = $listener($event, $payload);
if ($halt && ! is_null($response)) {
return $response;
}
if ($response === false) {
break;
}
$responses[] = $response;
}
return $halt ? null : $responses;
}
分析#
本作品采用《CC 协议》,转载必须注明作者和本文链接
推荐文章: