Click here to load reader
Upload
bo-andersen
View
304
Download
0
Embed Size (px)
Citation preview
ZF2 Event Managerby Bo Andersen
Introduction to the event systemZF2 is based on the Event-driven Architecture (EDA)
Simply put, the idea is that activities occur in response to events
Thus, activities are attached to events, and when a given event occurs, its attached activities are executed
This principle is well known in programming; Java and the .NET platform, for instance, both make heavy use of event handling
Configuring ZF2 with eventsEvents are used internally in Zend Framework 2, e.g. by the module
manager
Subscribing to these events allows one to “plug into” the framework
A series of pre-defined events are available
Please see the documentation for more information and a full list of available events
Example use caseImagine placing an order on Amazon.com
When the order is placed, a series of activities or processes should occur
A confirmation should be sent to the customer
The items should be picked from the warehouse
The customer’s credit card should be debited
...
These processes are registered on the OnOrderPlaced event, and when this event is triggered, the attached processes or activities are executed
AdvantagesProvides a great deal of flexibility in a system
Easy to add or remove activities that should happen when a given event occurs
The application becomes very flexible, as it is easy to “hook into” the system
Business processes can easily be adapted to changing business needs
DisadvantagesIt can be hard to understand what actually happens during a business
process
Activities can be attached to events almost anywhere in the application, and perhaps even added dynamically
This makes it difficult to both understand and debug a process; one must know which activities are attached to an event at runtime to understand exactly what happens
The flow of the business process is therefore less transparent than if it were implemented in a completely procedural fashion
The sequence in which activities must be executed adds complexity
Activities may depend upon each other; for instance, a customer’s credit card should not be debited before the items are picked from the warehouse.
The Event Manager
Example: Triggering an eventclass UserService implements EventManagerAwareInterface {
public function setEventManager(EventManagerInterface $events) { /* ... */ }
public function getEventManager() { /* ... */ }
public function register(User $user) {
$this->getEventManager()->trigger(__function__ . '.pre', $this, array(‘user’ => $user));
// Register code here
$this->getEventManager()->trigger(__function__ . '.post', $this, array(‘user’ => $user));
}
}
Example: Attaching event listener$userService = new UserService();
$userService->setEventManager(new EventManager());
$userService->getEventManager()->attach('register.post', function(EventInterface $event) {
$logger = new \Zend\Log\Logger();
$logger->addWriter(new \Zend\Log\Writer\Stream('php://output'));
$logger->log(Zend\Log\Logger::INFO, 'New user (ID: ' . $event->getParam('user')->getId() . ')');
});
More informationA priority can also be specified when attaching an event listener to an
event
The priority is simply an integer; the listener with the highest number is executed first
A callback can be assigned to multiple events at once
A wildcard can also be used to attach a callback to all events
More information and examples: http://tinyurl.com/zf2-event-manager
that’s allThis presentation is part of my Zend Framework 2 online course.
Get 50% discount by navigating to the below URL!
https://www.udemy.com/zend-framework-2-from-beginner-to-professional/?couponCode=SS50