Laravel8:事件系统源代码分析

这篇文档是分析Laravel事件系统。

在生命周期中,注册event服务

在Laravel框架生命周期中,在最开始实例应用程序时

$app = new Illuminate\Foundation\Application(
    $_ENV['APP_BASE_PATH'] ?? dirname(__DIR__)
);

Application 类的构造函数里,会执行注册基础服务提供者,其中就有Event事件服务提供者。

protected function registerBaseServiceProviders()
    {
        $this->register(new EventServiceProvider($this));
    }

然后执行了EventServiceProvider类中的register方法。

public function register()
{
        $this->app->singleton('events', function ($app) {
            return (new Dispatcher($app))->setQueueResolver(function () use ($app) {
                return $app->make(QueueFactoryContract::class);
            });
        });
}

容器中注册绑定了events服务,对应于new Dispatcher()对象。

自此就有了$app['events']

项目中EventServiceProvider 服务提供者类

在App\Providers\目录中有一个EventServiceProvider ,在这个服务提供者里,可以通过$listen 属性,注册事件和对应的侦听器

class EventServiceProvider extends ServiceProvider
{
    protected $listen = [
        Registered::class => [
            SendEmailVerificationNotification::class,
        ],
        Kindle::class=>[JujuListener::class]
    ];

可以在boot()方法中,手动注册事件.

public function boot()
{
    Event::listen(function (PodcastProcessed $event) {
        //
    });
}

在Laravel框架生命周期运行流程里,会遍历所有的服务提供者类,然后执行其内部定义的register()方法。

EventServiceProvider 的父类是 Illuminate\Foundation\Support\Providers\EventServiceProvider ,它有一个register()方法。

public function register()
    {
        $this->booting(function () {
            $events = $this->getEvents();

            foreach ($events as $event => $listeners) {
                foreach (array_unique($listeners) as $listener) {
                    Event::listen($event, $listener);
                }
            }

            foreach ($this->subscribe as $subscriber) {
                Event::subscribe($subscriber);
            }
        });
    }

调用booting()方法,然后传递一个匿名函数。那么booting()方法定义于它的父类中。

它的父类是Illuminate\Support\ServiceProvider

    public function booting(Closure $callback)
    {
        $this->bootingCallbacks[] = $callback;
    }

把上面的匿名函数,注册到$this->bootingCallbacks 中。

事件注册流程分析

事件系统,可以通过外观类Event来调用。

 use Illuminate\Support\Facades\Event;

操作方法是在 App\Providers\EventServiceProvider类中,操作$listen属性,或者是在boot()方法中,使用Event::listen()方法注册事件。

namespace App\Providers;

use Illuminate\Auth\Events\Registered;
use Illuminate\Auth\Listeners\SendEmailVerificationNotification;
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;
use Illuminate\Support\Facades\Event;
use App\Events\Kindle;
use App\Listeners\JujuListener;
class EventServiceProvider extends ServiceProvider
{
    /**
     * The event listener mappings for the application.
     *
     * @var array
     */
    protected $listen = [
        Registered::class => [
            SendEmailVerificationNotification::class,
        ],
        Kindle::class=>[JujuListener::class]
    ];

    /**
     * Register any events for your application.
     *
     * @return void
     */
    public function boot()
    {
        Event::listen(function(){
            

        });
    }
}

注册完毕后,在控制器中引入事件类,然后使用event()全局函数,将事件对应作为参数传入其中。

use App\Events\Kindle;


public function indexAction()
{
    event(new Kindle());
}

event()函数定义在:Illuminate\Foundation\helpers.php文件中

function event(...$args)
{
        return app('events')->dispatch(...$args);
}

因此我们将从dispatcher(事件类) 中的 dispatch()方法,做为入口,进行流程分析。

1 dispatch()方法分析

作用:启动事件并呼叫听众

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容