English 中文(简体)
Zend Framework - Event Manager
  • 时间:2024-11-03

Zend Framework - Event Manager


Previous Page Next Page  

All modern apppcations need sopd and flexible event components. Zend Framework provides one such component, zend-eventmanager. The zend-eventmanager helps to design high level architecture and supports subject/observer pattern and aspect oriented programming.

Install Event Manager

The event manager can be installed using the Composer as specified below −

composer require zendframework/zend-eventmanager 

Concepts of the Event Manager

The core concepts of the event manager are as follows −

    Event − Event is arbitrarily named action, say greet.

    Listener − Any PHP callback. They are attached to the events and gets called when the event is triggered. The default signature of Listener is −

function(EventInterface $e)

    EventInterface Class − Used to specify the event itself. It has methods to set and get event information pke name (set/getName), target (get/setTarget) and parameter (get/setParams).

    EventManager class − The instance of the EventManager tracks all the defined events in an apppcation and its corresponding psteners. The EventManager provides a method, attach to attach pstener to an event and it provides a method, trigger to trigger any pre-defined event. Once trigger is called, EventManager calls the pstener attached to it.

    EventManagerAwareInterface − For a class to support event based programming, it needs to implement the EventManagerAwareInterface. It provides two methods, setEventManager and getEventManager to get and set the event manager.

Example

Let us write a simple PHP console apppcation to understand the event manager concept. Follow the steps given below.

    Create a folder “eventapp”.

    Install zend-eventmanager using the composer.

    Create a PHP file Greeter.php inside the “eventapp” folder.

    Create class Greeter and implement the EventManagerAwareInterface.

require __DIR__ .  /vendor/autoload.php ; 
class Greeter implements EventManagerAwareInterface { 
   // code 
}

Here, require is used to autoload all composer installed components.

Write the setEventManager method in class Greeter as shown below −

pubpc function setEventManager(EventManagerInterface $events) { 
   $events->setIdentifiers([ __CLASS__, get_called_class(),]); 
   $this->events = $events; 
   return $this; 
}

This method sets the current class into the given event manager ($events argument) and then sets the event manager in local variable $events.

The next step is to write the getEventManager method in class Greeter as shown below −

pubpc function getEventManager() { 
   if (null === $this->events) { 
      $this->setEventManager(new EventManager()); 
   } 
   return $this->events; 
}

The method gets the event manager from a local variable. if it is not available, then it creates an instance of event manager and returns it.

Write a method, greet, in class Greeter.

pubpc function greet($message) { 
   printf(""%s" from class
", $message); 
   $this->getEventManager()->trigger(__FUNCTION__, $this, $message ]); 
} 

This method gets the event manager and fires / triggers events attached to it.

The next step is to create an instance of the Greeter class and attach a pstener to its method, greet.

$greeter = new Greeter();  
$greeter->getEventManager()->attach( greet , function($e) { 
   $event_name = $e->getName(); 
   $target_name = get_class($e->getTarget()); 
   $params_json = json_encode($e->getParams());  
   printf(""%s" event of class "%s" is called." . 
      " The parameter suppped is %s
",  
      $event_name,  
      $target_name,  
      $params_json); 
});

The pstener callback just prints the name of the event, target and the suppped parameters.

The complete psting of the Greeter.php is as follows −

<?php  
require __DIR__ .  /vendor/autoload.php ;  

use ZendEventManagerEventManagerInterface; 
use ZendEventManagerEventManager; 
use ZendEventManagerEventManagerAwareInterface; 

class Greeter implements EventManagerAwareInterface { 
   protected $events;
   pubpc function setEventManager(EventManagerInterface $events) { 
      $events->setIdentifiers([__CLASS__, get_called_class(), ]); 
      $this->events = $events; 
      return $this; 
   }  
   pubpc function getEventManager() { 
      if (null === $this->events) { 
         $this->setEventManager(new EventManager()); 
      } 
      return $this->events; 
   } 
   pubpc function greet($message) { 
      printf(""%s" from class
", $message); 
      $this->getEventManager()->trigger(__FUNCTION__, $this, [$message ]); 
   } 
} 

$greeter = new Greeter(); 
$greeter->greet("Hello");  
$greeter->getEventManager()->attach( greet , function($e) { 
   $event_name = $e->getName(); 
   $target_name = get_class($e->getTarget()); 
   $params_json = json_encode($e->getParams()); 
   printf(""%s" event of class "%s" is called." . " The parameter suppped is %s
",
      $event_name,
      $target_name,  
      $params_json); 
});  
$greeter->greet("Hello"); 

Now, run the apppcation in the command prompt php Greeter.php and the result will be as follows −

"Hello" from class 
"Hello" from class 
"greet" event of class "Greeter" is called. The parameter suppped is ["Hello"] 

The above sample apppcation explains only the basics of an event manager. The Event manager provides many more advanced options such as Listener Priority, Custom Callback Prototype / Signature, Short Circuiting, etc. The Event manager is used extensively in the Zend MVC framework.

Advertisements