Zend Framework 2.0 event manager (The EventManager) Getting Started tutorial, zendeventmanager
Overview
EventManger is a component designed for the following scenarios:
Copy codeThe Code is as follows:
Implement simple topic/Observer Mode
Achieve face-oriented design
Event-driven architecture
The basic architecture allows you to add and remove listeners for specified events, whether on an instance basis or a shared collection, trigger events, and terminate the execution of listeners.
Quick Start
Generally, you will create an EventManager in a class.
Copy codeThe Code is as follows:
Use Zend \ EventManager \ EventManagerInterface;
Use Zend \ 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 reset it with a new instance; if it does not exist, it will be instantiated in inertia when used.
EventManager is only interested in whether it triggers some events. The basic trigger accepts three parameters: the event name, which is usually the current function/method name; context, which is usually the instance of the current object; and parameter, it is usually a parameter provided to the current function/method.
Copy codeThe Code is as follows:
Class Foo
{
//... Assume events definition from abve
Public function bar ($ baz, $ bat = null)
{
$ Params = compact ('baz', 'bat ');
$ This-> getEventManager ()-> trigger (_ FUNCTION __, $ this, $ params );
}
}
In order, the trigger event only cares about whether something has listened to the event. Adds the listener to EventManager, specifying a specified event and the callback to be notified. The callback accepts an Event object, which has an accessor used to obtain the Event name, context, and parameters. Let's add a listener and trigger the event.
Copy codeThe Code is 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. The example shows an anonymous function 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. Again, any PHP callback is valid.
Sometimes you may want to specify that one listener does not have an object instance that creates an EventManager class. Zend Framework implements it through the concept of javasdeventcollection. To put it simply, you can use a well-known javasdeventcollection to inject an independent EventManager instance, and the EventManager instance will query it as an additional listener. The listener added to the destdeventcollection is slightly the same as the normal event manager. The call to attach is identical to the EventManager, but an additional parameter is required at the beginning: a specified instance. Remember how to pass an EventManager instance to him _ CLASS? The value, or any string in the array you provide to the constructor, when using a javasdeventcollection, may be used to identify an instance. As an example, suppose we have a zoodeventmanager instance and we know it has been injected into our EventManager instance (for instances, through dependency injection ), we can change the example above to add a shared set:
Copy codeThe Code is as follows:
Use Zend \ Log \ Factory as LogFactory;
// Assume $ events is a Zend \ EventManager \ login deventmanager 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 ()-> setincludeventcollection ($ 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 deployment deventcollection. In this way, you do not need to worry about where or how to access the javasdeventcollection; it is globally available by simply calling StaticEventManager: getInstance.
You must know, however, the Framework does not approve of its use, and in 2.0.0beta4, You will replace it by configuring a zoodeventmanager instance and injecting it into a separate EventManager instance.
Wildcard interceptor
Sometimes you may want to add the same listener for many or all events of a given instance, or use a shared event set, many contexts, and many events. The EventManager component allows this.
Add multiple events at a time
Copy codeThe Code is as follows:
$ Events = new EventManager ();
$ Events-> attach (array ('thes', 'all', 'event', 'names'), $ callback );
Add By wildcard
Copy codeThe Code is as follows:
$ Events = new EventManager ();
$ Events-> attach ('*', $ callback );
Note that if you specify a priority, it will be used for any event triggered by the listener.
The code above specifies that any time trigger will cause the notification of this particular listener.
Add multiple events at a time using a javasdeventmanager
Copy codeThe Code is as follows:
$ Events = new includeventmanager ();
// Attach to your events on the context "foo"
$ Events-> attach ('foo', array ('these ', 'all', 'event', 'names'), $ callback );
// Attach to your events on the contexts "foo" and "bar"
$ Events-> attach (array ('foo', 'bar'), array ('these ', 'all', 'event', 'names'), $ callback );
Note that if you specify a priority, it will be used for all specified events.
Add all events at a time using a javasdeventmanager
Copy codeThe Code is as follows:
$ Events = new includeventmanager ();
// 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, it will be used for all specified events.
The code above specifies the context "foo" and "bar". The specified listener will be notified when any event is triggered.
Configuration Options
EventManager options
Identifier
A string or string array that can be answered by a given EventManager instance. It is accessed through a javasdeventmanager.
Event_class
The name of an alternative Event class is used to represent the Event that is passed to the listener.
Shared_collections
A producer deventcollection instance when an event is triggered.
Available Methods
_ Construct
_ Construct (null | string | int Sidentifier)
Construct a new EventManager instance and use the given identifier to share the set.
SetEventClass
SetEventClass (string $ class)
Replace the name of the Event class when creating an Event that is passed to the triggered listener.
SetSharedCollections
SetSharedCollections (includeventcollection $ collections = null)
The producer deventcollection instance used to trigger the event.
GetSharedCollections
GetSharedCollections ()
Returns the currently added javasdeventcollection instance. If no collection is added, null is returned, or a javasdeventcollection instance is returned.
Trigger
Trigger (string $ event, mixed $ target, mixed $ argv, callback $ callback)
All listeners that trigger the specified event. We recommend that you use the current function/method name for $ event, and add ". pre" and ". post" to it if necessary. $ Context should be the instance of the current object, or the function name, if not triggered by the object. $ Params is usually an associated array or ArrayAccess instance. We recommend that you use the parameter passed to the function/method (compact () is usually useful here ). This method can also accept a callback and behave the same as triggerUntil.
Method returns a ResponseCollection instance, which can be used to reflect on the values returned by a variety of listeners, test short circuits, and more.
TriggerUntil
TriggerUntil (string $ event, mixed $ context, mixed $ argv, callback $ callback)
All listeners that trigger a specified event are like trigger (). Additionally, it passes the returned values of each listener to $ callback. If $ callback returns a Boolean true value, the listener is terminated. You can use $ result-> stopped () to test it.
Attach
Attach (string $ event, callback $ callback, int $ priority)
Add $ callback to the EventManager instance and listen for the event $ event. If a $ priority is provided, the listener inserts the priority value into the internal listener stack. A high value is executed first. (The default priority is "1" and the running uses a negative value .)
Method returns an instance of Zend \ Stdlib \ CallbackHandler. This value can be passed to detach () Later, if needed.
AttachAggregate
AttachAggregate (string | ListenerAggregate $ aggregate)
If a string is passed as $ aggregate, instantiate the class. $ Aggregate is then passed to the attache () method of the EventManager instance so that he can register the listener.
Returns the ListenerAggregate instance.
Detach
Detach (CallbackHandler $ listener)
Scan all listeners and cancel all listeners that match $ listener, so they will no longer be triggered.
Returns a Boolean value of true. If any listener has been specified and the subscription is canceled, a Boolean value of false is returned.
DetachAggregate
DetachAggregate (ListenerAggregate $ aggregate)
Loop all events to determine the listener represented by the set. For all matching items, the listener will be removed.
If any listener is determined and the subscription is canceled, a Boolean value of true is returned. Otherwise, a Boolean value of false is returned.
GetEvents
GetEvent ()
Returns an array of all event names appended to the listener.
GetListeners
GetListeners (string $ event)
Returns a Zend \ Stdlib \ PriorityQueue instance of all listeners added to $ event.
ClearListeners
ClearListeners (string $ event)
Remove all listeners added to $ event.
PrepareArgs
PrepareArgs (array $ args)
Create an ArrayObject from the provided $ args. If you want your listener to change the parameters, it will be useful for later listeners or triggered methods to see these changes.
Introduction to zend framework 2
Let's talk about zend framework 2 with a reply from your site. Come here. There are more people. Group No. 213966183
About zend framework 20 module Settings
Yes.