Zend Framework 2.0 Event Manager (the EventManager) Getting Started tutorial, Zendeventmanager
Overview
Eventmanger is a component that is designed for the following usage scenarios:
Copy the Code code as follows:
Implement a simple theme/viewer pattern
Implement face-oriented design
Implementing an event-driven architecture
The basic schema allows you to add and dismiss listeners for the specified event, whether on an instance basis or a shared collection, triggering an event, or terminating the execution of the listener.
Quick Start
Typically, you will create a eventmanager in a class.
Copy the Code code as follows:
Use Zend\eventmanager\eventmanagerinterface;
Use Zend\eventmanager\eventmanager;
Use Zend\eventmanager\eventmanagerawareinterface;
Class Foo implements Eventmanagerawareinterface
{
protected $events;
Public Function Seteventmanager (Eventmanagerinterface $events)
{
$events->setidentifiers (Array (
__class__,
Get_called_class (),
));
$this->events = $events;
return $this;
}
Public Function Geteventmanager ()
{
if (null = = = $this->events) {
$this->seteventmanager (New EventManager ());
}
return $this->events;
}
}
The above code allows the user to access the EventManager instance, or to reset it using a new instance, and if it does not exist, it will be lazily instantiated when it is used.
EventManager is only interested in whether it triggers a few events. The underlying trigger accepts three parameters: the name of the event, which is usually the current function/method name; context, which is usually an instance of the current object, and a parameter, which is usually the argument supplied to the current function/method.
Copy the Code code as follows:
Class Foo
{
... assume events definition from above
Public Function Bar ($baz, $bat = null)
{
$params = Compact (' Baz ', ' bat ');
$this->geteventmanager ()->trigger (__function__, $this, $params);
}
}
In order, the triggering event only cares if there is something listening on the event. The listener is added to EventManager, specifying a specified event and a callback to be notified. The callback accepts an event object, which has an accessor for getting the event name, context, and parameters. Let's add a listener and trigger the event.
Copy the Code code as follows:
Use Zend\log\factory as logfactory;
$log = Logfactory ($someConfig);
$foo = new Foo ();
$foo->geteventmanager ()->attach (' Bar ', function ($e) use ($log) {
$event = $e->getname ();
$target = Get_class ($e->gettarget ());
$params = Json_encode ($e->getparams ());
$log->info (sprintf (
'%s called on%s, using params%s ',
$event,
$target,
$params
));
});
Results in log message:
$foo->bar (' Baz ', ' bat ');
Reading:bar called on Foo, using params {"Baz": "Baz", "bat": "Bat"} "
Note that the second parameter of attach () is a valid callback, and an anonymous function is shown in the example to keep the example self-contained. However, you can also use a valid function name, a function object, a string that references a static method, or a callback array with a specified static method or instance method. Once again, any PHP callback is valid.
Sometimes you might want to specify that a listener does not have an object instance that creates a EventManager class. The Zend framework implements it through a sharedeventcollection concept. Simply put, you can use a well-known sharedeventcollection to inject a separate EventManager instance, and the EventManager instance will query it for the additional listener. The listener added to Sharedeventcollection is roughly the same as the normal event manager; The call attach is exactly the same as EventManager, but requires an additional parameter at the beginning: a specified instance. Remember to create an instance of EventManager, how did we pass it on to him __class__? When using a sharedeventcollection, that value, or any string in the array that you provide to the constructor, may be used to identify an instance. As an example, suppose we have a sharedeventmanager instance that we know has been injected into our EventManager instance (for instance, through dependency injection), we can change the example above to add by sharing the collection:
Copy CodeThe code is as follows:
Use Zend\log\factory as logfactory;
Assume $events is a Zend\eventmanager\sharedeventmanager instance
$log = Logfactory ($someConfig);
$events->attach (' Foo ', ' Bar ', function ($e) use ($log) {
$event = $e->getname ();
$target = Get_class ($e->gettarget ());
$params = Json_encode ($e->getparams ());
$log->info (sprintf (
'%s called on%s, using params%s ',
$event,
$target,
$params
));
});
Later, instantiate Foo:
$foo = new Foo ();
$foo->geteventmanager ()->setsharedeventcollection ($events);
And we can still trigger the above event:
$foo->bar (' Baz ', ' bat ');
Results in log message:
Bar called on Foo, using params {"Baz": "Baz", "bat": "Bat"} "
Note: Staticeventmanager
In 2.0.0beta3, you can use the Staticeventmanager singleton as a sharedeventcollection. This way, you don't have to worry about where or how to access sharedeventcollection; it is globally available through a simple call to Staticeventmanager::getinstance ().
You know, however, that the framework does not approve of its use, and in 2.0.0beta4, you will replace it by configuring a Sharedeventmanager instance and injecting it into a separate EventManager instance.
Wildcard Listener
Sometimes you might want to add the same listener for many events or all events for a given instance, or you might use a collection of shared events, many contexts, and a lot of events. The EventManager component allows this to be done.
Add multiple events at once
Copy the Code code as follows:
$events = new EventManager ();
$events->attach (Array (' These ', ' is ', ' event ', ' names '), $callback);
Add with wildcard characters
Copy the Code code as follows:
$events = new EventManager ();
$events->attach (' * ', $callback);
Note that if you specify a priority, that priority will be used for any event triggered by this listener.
The above code specifies that any time the trigger will cause this particular listener to be notified.
Add multiple events at once with one Sharedeventmanager
Copy the Code code as follows:
$events = new Sharedeventmanager ();
Attach to many events on the context "foo"
$events->attach (' foo ', Array (' These ', ' is ', ' event ', ' names '), $callback);
Attach to many events on the contexts "foo" and "Bar"
$events->attach (Array (' foo ', ' Bar '), array (' These ', ' is ', ' event ', ' names '), $callback);
Note that if you specify a priority, that priority will be used for all specified events.
Add all events at once with one Sharedeventmanager
Copy the Code code as follows:
$events = new Sharedeventmanager ();
Attach to all events on the context "foo"
$events->attach (' foo ', ' * ', $callback);
Attach to all events on the contexts "foo" and "Bar"
$events->attach (Array (' foo ', ' Bar '), ' * ', $callback);
Note that if you specify a priority, that priority will be used for all specified events.
The above code specifies the context "foo" and "Bar", and the specified listener will be notified when any event is triggered.
Configuration options
EventManager options
Identifier
A given EventManager instance can answer a string or an array of strings when accessed through a sharedeventmanager.
Event_class
The name of an alternate event class is used to represent the event passed to the listener.
Shared_collections
An sharedeventcollection instance when the event is triggered.
Available methods
__construct
__construct (Null|string|int sidentifier)
Constructs a new EventManager instance, using the given identifier, if provided, in order to share the purpose of the collection.
Seteventclass
Seteventclass (String $class)
Provides the name of the replacement event class to use when creating an event that is passed to the triggered listener.
Setsharedcollections
Setsharedcollections (sharedeventcollection $collections =null)
The sharedeventcollection instance that is used to trigger the event.
Getsharedcollections
Getsharedcollections ()
Returns the Sharedeventcollection instance currently added to. If no collection is added, returns NULL, or an sharedeventcollection instance.
Trigger
Trigger (String $event, mixed $target, mixed $argv, callback $callback)
Triggers all listeners for the specified event. It is recommended to use the current function/method name for $event, followed by a ". Pre", ". Post", etc., if necessary. $context should be an instance of the current object, or the name of the function if it is not fired using an object. $params should usually be an associative array or arrayaccess instance; We recommend using arguments passed to a function/method (compact () is usually useful here). This method can also accept a callback and behaves the same as Triggeruntil ().
method returns an instance of Responsecollection, which can be used to reflect on the values returned by various listeners, test short circuits, and more.
Triggeruntil
Triggeruntil (String $event, mixed $context, mixed $argv, callback $callback)
Triggers all listeners for the specified event, just like Trigger (), which, in addition, passes the return value of each listener to $callback, and if $callback returns a Boolean True value, the execution of the listener is terminated. You can use $result->stopped () to test it.
Attach
Attach (String $event, callback $callback, int $priority)
Add $callback to EventManager instances, listening for event $event. If a $priority is provided, the listener will use that priority to insert into the internal listener stack, and the high value will be executed first. (The default priority is "1" and runs with a negative value.) )
The Zend\stdlib\callbackhandler method returns an instance of a value that can later be passed to detach (), if necessary.
Attachaggregate
Attachaggregate (string| Listeneraggregate $aggregate)
If a string is passed as $aggregate, instantiate that class. The $aggregate is then passed to the Attache () method of the EventManager instance so that he can register the listener.
Returns a Listeneraggregate instance.
Detach
Detach (CallbackHandler $listener)
Scans all listeners and cancels all listeners that match $listener so they will no longer be triggered.
Returns a true Boolean value that returns a false Boolean value if any listener has been specified and unsubscribed.
Detachaggregate
Detachaggregate (Listeneraggregate $aggregate)
Loops through all events to determine which listener the collection represents, and for all occurrences, the listener will be removed.
Returns a Boolean value of False if any listener is determined and the unsubscribe is returned with a true Boolean value.
GetEvents
GetEvent ()
Returns an array of all the event names attached to the listener.
Getlisteners
Getlisteners (String $event)
Returns a Zend\stdlib\priorityqueue instance of all listeners added to the $event
Clearlisteners
Clearlisteners (String $event)
Removes all listeners added to the $event.
Prepareargs
Prepareargs (Array $args)
Create a arrayobject from the $args provided. It would be useful if you want your listener to be able to change the parameters so that later listeners or triggered methods can see the changes.
Zend Framework 2 Getting started learning and communication
Borrow your treasure with an answer, Zend Framework 2 Exchange study, come here, people a little more. Group No. 213966183
Issues with module settings for the Zend Framework 20
Just know.
http://www.bkjia.com/PHPjc/861791.html www.bkjia.com true http://www.bkjia.com/PHPjc/861791.html techarticle Zend Framework 2.0 Event Manager (the EventManager) introductory tutorial, Zendeventmanager Overview Eventmanger is a component designed for the following usage scenarios: Copy code code as follows: ...