5.1 事件Event
Minor的 Event
类提供一个简单的观察者实现,允许您在应用程序里订阅与监听事件。
5.1.1 订阅事件
首先创建一个事件类:
<span style="color: #000000;">php namespace App\Event; </span><span style="color: #0000ff;">use</span><span style="color: #000000;"> Minor\Event\Event; </span><span style="color: #0000ff;">class</span> DemoEvent <span style="color: #0000ff;">extends</span><span style="color: #000000;"> Event { </span><span style="color: #0000ff;">private</span> <span style="color: #800080;">$name</span><span style="color: #000000;">; </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span> __construct(<span style="color: #800080;">$name</span><span style="color: #000000;">) { </span><span style="color: #800080;">$this</span>->name = <span style="color: #800080;">$name</span><span style="color: #000000;">; } </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span> setName(<span style="color: #800080;">$name</span><span style="color: #000000;">) { </span><span style="color: #800080;">$this</span>->name = <span style="color: #800080;">$name</span><span style="color: #000000;">; } </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> getName() { </span><span style="color: #0000ff;">return</span> <span style="color: #800080;">$this</span>-><span style="color: #000000;">name; } }</span>
然后在配置文件中注册这个事件:
<span style="color: #000000;">php </span><span style="color: #0000ff;">return</span> <span style="color: #800080;">$events</span> =<span style="color: #000000;"> [ </span>'App\Event\DemoEvent' =><span style="color: #000000;"> [ </span>'App\Listener\DemoListener' => 'handle',<span style="color: #000000;"> ]</span>,<span style="color: #000000;"> ];</span>
5.1.2 触发事件
Minor提供了一个事件管理类:Minor\Event\EventManger,通过调用该类的静态方法fire:EventManager::fire($event)就可以触发这个事件,例:
<span style="color: #0000ff;">class</span> FooController <span style="color: #0000ff;">extends</span><span style="color: #000000;"> Controller { </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span> bar(<span style="color: #800080;">$productName</span><span style="color: #000000;">) { </span><span style="color: #800080;">$event</span> = <span style="color: #0000ff;">new</span> DemoEvent('DemoEvent'<span style="color: #000000;">); EventManager</span>::fire(<span style="color: #800080;">$event</span><span style="color: #000000;">); </span>...<span style="color: #000000;"> } }</span>
5.2 监听器
当触发事件后事件管理器EventManager就会通过配置文件去触发监听器的制定方法。在5.1.1配置文件中我们配置了DemoEvent的监听器为App\Listener\DemoListener的handle方法,可以看一下这个类的实现:
<span style="color: #000000;">php namespace App\Listener; </span><span style="color: #0000ff;">use</span><span style="color: #000000;"> App\Event\DemoEvent; </span><span style="color: #0000ff;">use</span><span style="color: #000000;"> Minor\Event\Listener; </span><span style="color: #0000ff;">class</span> DemoListener <span style="color: #0000ff;">extends</span><span style="color: #000000;"> Listener { </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span> handle(DemoEvent <span style="color: #800080;">$event</span><span style="color: #000000;">) { </span><span style="color: #0000ff;">echo</span> '[DemoListener] handle the event:[' . <span style="color: #800080;">$event</span>->getName() .'] success!<br><br>'<span style="color: #000000;">; } }</span>