码迷,mamicode.com
首页 > 其他好文 > 详细

Events

时间:2016-07-05 12:19:50      阅读:107      评论:0      收藏:0      [点我收藏+]

标签:

Events

The idea behind Events is the ability to send data, as parameters, to interested Listeners and call them when an Event happens. The Listeners could be a Closure or a static Class Method.

To note that if the Events\Dispatcher does not instantiate a Listener‘s Class, then the called method must be static.

Its usage is simple; first, we have to register a Listener, adding:

use Event;

// Add a Listener to the Event ‘test‘.
Event::listen(‘test‘, ‘App\Events\Test@handle‘);

Where the class App\Events\Test is

namespace App\Events;

class Test
{
    public static function handle($message)
    {
        echo $message;
    }
}

Then, when the payload is needed, we would fire the Event:

// Prepare the Event payload.
$payload = array(
    ‘Hello, this is an Event!‘,
);

// Fire the Event ‘test‘.
Event::fire(‘test‘, $payload);

Queued Events

The Queued Events is a method to add a number of Events to a Queue, then firing them together, flushing the Queue. E.g

// Queue the Events
Event::queue(‘test‘, array(‘This is the First Event‘));
Event::queue(‘test‘, array(‘This is the Second Event‘));
Event::queue(‘test‘, array(‘This is the Third Event‘));

// Flush the Queue, firing all defined Events
Event::flush(‘test‘);

until() Method

The new method Events\Dispatcher@until is about not firing an Event until the first Listener returns a valid and non-null response.

Its usage is simple:

$response = Event::until(‘testing‘, $payload);

if($response !== null) {
    // Do something with $response
}

Events

标签:

原文地址:http://www.cnblogs.com/chunguang/p/5642988.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!