In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-08 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains "how to use the Zend Framework 2.0 event Manager". The content of the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to use the Zend Framework 2.0 event Manager".
Overview
EventManger is a component designed for the following use cases:
The copy code is as follows:
Implement a simple theme / observer pattern
Realize aspect-oriented design
Implement an 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 listeners' execution.
Getting started
Typically, you will create an EventManager in a class.
The copy code is 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 reset it with a new instance; if it does not exist, it will be instantiated lazily when used.
EventManager is only interested in whether it triggers some events. The underlying trigger takes three parameters: the name of the event, which is usually the name of the current function / method; the context, which is usually an instance of the current object; and the parameter, which is usually the parameter supplied to the current function / method.
The copy code is 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 trigger event is only concerned with whether something is listening for the event. The listener is added to the EventManager, specifying a specified event and a callback to be notified. The callback accepts an Event object that has an accessor to get the event name, context, and parameters. Let's add a listener and trigger the event.
The copy 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 argument to attach () is any 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 referencing a static method, or a callback array with a specified static method or instance method. Once again, any PHP callback is valid.
Sometimes you may want to specify that a listener does not have an object instance of a class that creates an EventManager. Zend Framework implements it through a concept of SharedEventCollection. Simply put, you can use a well-known SharedEventCollection to inject a separate EventManager instance, and the EventManager instance will query it for additional listeners. The listeners added to SharedEventCollection are roughly the same as the normal event manager; calling 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 to _ _ CLASS__? When using a SharedEventCollection, that value, or any string in any array 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 the instance, through dependency injection), we can change the above example to add by sharing the collection:
The copy 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. That way, you don't have to worry about where or how to access SharedEventCollection;-it's globally available by simply calling StaticEventManager::getInstance ().
You know, however, 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 may want to add the same listener for many or all events of a given instance, or perhaps, use a shared event set, many contexts, and many events. The EventManager component allows this.
Add more than one event at a time
The copy code is as follows:
$events = new EventManager ()
$events- > attach (array ('these',' are', 'event',' names'), $callback)
Add by wildcard
The copy code is 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 a notification that will cause this particular listener to be triggered at any time.
Add more than one event at a time through a SharedEventManager
The copy code is as follows:
$events = new SharedEventManager ()
/ / Attach to many events on the context "foo"
$events- > attach ('foo', array (' these', 'are',' event', 'names'), $callback)
/ / Attach to many events on the contexts "foo" and "bar"
$events- > attach (array ('foo',' bar'), array ('these',' are', 'event',' names'), $callback)
Note that if you specify a priority, that priority will be used for all specified events.
Add all events at once through a SharedEventManager
The copy code is 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 contexts "foo" and "bar", and the specified listener will be notified when any event is triggered.
Configuration option
EventManager option
Identifier
A string or array of strings that can be answered by a given EventManager instance when accessed through a SharedEventManager.
Event_class
The name of an alternative Event class is used to represent the event passed to the listener.
Shared_collections
An instance of SharedEventCollection when the event is triggered.
Available methods
_ _ construct
_ _ construct (null | string | int Sidentifier)
Construct a new EventManager instance, using the given identifier, if provided, for the purpose of sharing the collection.
SetEventClass
SetEventClass (string $class)
Provide the name of the alternate Event class to use when creating events passed to the triggered listener.
SetSharedCollections
SetSharedCollections (SharedEventCollection $collections=null)
The SharedEventCollection instance used when the event is triggered.
GetSharedCollections
GetSharedCollections ()
Returns the SharedEventCollection instance currently added to. If no collection is added, empty is returned, or a SharedEventCollection instance is returned.
Trigger
Trigger (string $event, mixed $target, mixed $argv, callback $callback)
All listeners that trigger the specified event. It is recommended to use the current function / method name for $event, followed by ".pre", ".post", etc., if necessary. Context should be an instance of the current object, or the name of the function if it is not triggered by the object. $params should usually be an associative array or ArrayAccess instance; we recommend using the parameters 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 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)
All listeners that trigger the specified event, like trigger (), in addition, it passes the return value of each listener to $callback;. If $callback returns a Boolean truth value, the execution of the listener will be terminated. You can test it using $result- > stopped ().
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 will use that priority to insert into the internal listener stack; high values will be executed first. (the default priority is "1" and the run uses a negative value. )
The method returns an instance of Zend\ Stdlib\ CallbackHandler; this value can be passed to detach () later, if necessary.
AttachAggregate
AttachAggregate (string | ListenerAggregate $aggregate)
If a string is passed as $aggregate, instantiate that 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 unmatch all listeners for $listener so they will no longer be triggered.
Returns a true Boolean value if any listener has been specified and unsubscribed, otherwise a false Boolean value is returned.
DetachAggregate
DetachAggregate (ListenerAggregate $aggregate)
Loop all events to determine which listener the collection represents; for all matches, the listener will be removed.
If any listener is identified and unsubscribed, a true Boolean is returned, otherwise a false Boolean is returned.
GetEvents
GetEvent ()
Returns an array of all event names attached by the listener.
GetListeners
GetListeners (string $event)
Returns an instance of Zend\ Stdlib\ PriorityQueue 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 $args provided. It is useful if you want your listener to change the parameters so that later listeners or triggered methods can see these changes.
Thank you for reading, the above is the content of "how to use Zend Framework 2.0 event Manager". After the study of this article, I believe you have a deeper understanding of how to use Zend Framework 2.0 event Manager, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.