Skip to content

Instantly share code, notes, and snippets.

@Drarok
Forked from Mezzle/EventManagerAwareTrait.php
Created October 18, 2012 15:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Drarok/3912712 to your computer and use it in GitHub Desktop.
Save Drarok/3912712 to your computer and use it in GitHub Desktop.
ServiceLocator/EventManager Traits
<?php
namespace Protec\Stdlib;
use \Zend\EventManager\EventManagerInterface;
use \Zend\ServiceManager\ServiceLocatorAwareInterface;
trait EventManagerAwareTrait
{
protected $event_manager;
/**
* getEventManager
*
* @return EventManagerInterface
*/
public function getEventManager()
{
if (is_null($this->event_manager)
&& $this instanceof ServiceLocatorAwareInterface
&& ! is_null($this->getServiceLocator())
&& $this->getServiceLocator()->has('EventManager')
) {
$this->setEventManager(
$this->getServiceLocator()->get('EventManager'));
}
return $this->event_manager;
}
/**
* setEventManager
*
* @param EventManagerInterface $eventManager
*/
public function setEventManager(EventManagerInterface $eventManager)
{
$this->event_manager = $eventManager;
$this->event_manager->addIdentifiers(
array(
get_called_class(),
__CLASS__
)
);
return $this;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment