Skip to content

Instantly share code, notes, and snippets.

@weierophinney
Created March 9, 2012 21:51
Show Gist options
  • Save weierophinney/2008921 to your computer and use it in GitHub Desktop.
Save weierophinney/2008921 to your computer and use it in GitHub Desktop.
EventManager injection
<?php
namespace Zend\EventManager;
/**
* EventManager can compose static connections.
*
* What I'd like to see: classes that accept an EM always get a new instance,
* but each EM instance each receives the *same* instance of a
* StaticEventCollection.
*
*/
class EventManager
{
protected $staticConnections;
/**
* @var EventManager
*/
public function setStaticConnections(StaticEventCollection $collection)
{
$this->staticConnections = $collection;
}
}
<?php
namespace Foo\Controller
use Zend\Mvc\Controller\ActionController,
Zend\EventManager\EventCollection,
Zend\EventManager\EventManager;
/**
* controllers compose an EventManager instance
*/
class FooController
{
/**
* @var EventManager
*/
protected $events;
public function setEventManager(EventCollection $events)
{
$events->setIdentifier(array(
'Zend\Stdlib\Dispatchable', __CLASS__, get_called_class(),
));
$this->events = $events;
}
public function events()
{
if (!$this->events instanceof EventCollection) {
$this->setEventManager(new EventManager());
}
return $this->events;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment