Skip to content

Instantly share code, notes, and snippets.

@fritz-gerneth
Created April 30, 2012 17:49
Show Gist options
  • Save fritz-gerneth/2560437 to your computer and use it in GitHub Desktop.
Save fritz-gerneth/2560437 to your computer and use it in GitHub Desktop.
<?php
namespace Test;
use \Zend\EventManager\EventCollection,
\Zend\EventManager\EventManagerAware,
\Zend\Di\Locator,
\Zend\EventManager\SharedEventCollectionAware,
\Zend\EventManager\SharedEventCollection;
class Collection implements SharedEventCollectionAware
{
protected $listeners;
protected $counter = 0;
public function __construct()
{
$this->listeners = array();
}
public function addListener(Listener $listener)
{
$this->listeners[] = $listener;
}
public function setSharedCollections(SharedEventCollection $sharedEventCollection)
{
$this->counter++;
echo "Counter Collection: " . $this->counter . "<br />";
//\Zend\Debug::dump($sharedEventCollection);
}
}
<?php
return array(
'di' => array(
'instance' => array(
'Test\Collection' => array(
'injections' => array(
'addListener' => array(
array('listener' => 'Test\View'),
),
)
),
'DebuggingTools\Event\View' => array(
'shared' => true
)
),
'definition' => array(
'class' => array(
'Test\Collection' => array(
'addListener' => array(
'listener' => array(
'type' => 'Test\Listener',
'required' => false,
)
)
)
)
)
),
);
class Module
{
public function init(Manager $moduleManager)
{
$events = $moduleManager->events();
$sharedEvents = $events->getSharedCollections();
$sharedEvents->attach('bootstrap', 'bootstrap', array($this, 'test'), 900);
}
public function test($e)
{
$f = $e->getParam('application')->getLocator()->get('Test\View');
$f1 = $e->getParam('application')->getLocator()->get('Test\View');
\Zend\Debug::dump($f);
\Zend\Debug::dump($f1);
$f = $e->getParam('application')->getLocator()->get('Test\Collection');
}
}
constructed
(dump of the application's sharedeventmanager) 2x
Counter Collection: 1
Counter Collection: 2
constructed
(dump of an empty sharedeventmanager) 2x
<?php
namespace Test\Event;
use \Zend\EventManager\EventCollection,
\Zend\EventManager\EventManager,
\Zend\EventManager\SharedEventCollectionAware,
\Zend\EventManager\SharedEventCollection;
class View implements Listener, SharedEventCollectionAware
{
public function __construct()
{
echo "constructed <br />";
}
public function attach(EventCollection $events)
{
}
public function detach(EventCollection $events)
{
}
public function setSharedCollections(SharedEventCollection $sharedEventCollection)
{
\Zend\Debug::dump($sharedEventCollection);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment