Skip to content

Instantly share code, notes, and snippets.

@samuelmaddock
Last active August 29, 2015 14:22
Show Gist options
  • Save samuelmaddock/bb8e4886d250f72939a3 to your computer and use it in GitHub Desktop.
Save samuelmaddock/bb8e4886d250f72939a3 to your computer and use it in GitHub Desktop.
ZF2 Eventing Skeleton
<?php
// place under /config/autoload/
return [
'example-manager' => [
'listeners' => [
'Example\ExampleListener'
]
]
];
<?php
namespace Example;
use Zend\EventManager\Event;
class ExampleEvent extends Event
{
const EVENT_FOOBAR = 'foobar';
protected $value;
public function getValue()
{
return $this->value;
}
public function setValue($value)
{
$this->value = $value;
return $this;
}
}
<?php
namespace Example;
use Zend\EventManager\AbstractListenerAggregate;
use Zend\EventManager\EventManager;
use Zend\EventManager\EventManagerInterface;
use Example\ExampleEvent;
class ExampleListener extends AbstractListenerAggregate
{
public function attach(EventManagerInterface $events)
{
$this->listeners[] = $events->attach(ExampleEvent::EVENT_FOOBAR, [$this, 'handleEvent']);
}
public function handleEvent(ExampleEvent $e)
{
$value = $e->getValue();
if (!$value) {
return; // no value to process
}
// ...
}
}
<?php
namespace Example;
use Zend\EventManager\EventManagerAwareTrait;
use Zend\EventManager\EventManagerAwareInterface;
use Example\ExampleEvent;
class ExampleManager implements EventManagerAwareInterface
{
use EventManagerAwareTrait;
public function newExample(ExampleEvent $event)
{
$this->getEventManager()->trigger($event);
}
}
<?php
namespace Example;
use Zend\ServiceManager\FactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
use Example\ExampleManager;
class ExampleManagerFactory implements FactoryInterface
{
private static $defaultConfig = ['listeners' => []];
public function createService(ServiceLocatorInterface $serviceLocator)
{
$exampleManager = new ExampleManager();
// optional
$this->attachListenersFromConfig($serviceLocator, $exampleManager);
return $exampleManager;
}
private function attachListenersFromConfig($serviceLocator, $exampleManager)
{
$config = $serviceLocator->get('config');
$exampleConfig = isset($config['example-manager']) ?
$config['example-manager'] :
static::DEFAULT_CONFIG;
$em = $exampleManager->getEventManager();
foreach ($exampleConfig['listeners'] as $listenerName)
{
if (!$serviceLocator->has($listenerName))
{
continue;
}
$listener = $serviceLocator->get($listenerName);
$em->attachAggregate($listener);
}
}
}
<?php
namespace Example;
use Zend\ServiceManager\ServiceLocatorAwareInterface;
use Zend\ServiceManager\ServiceLocatorAwareTrait;
use Zend\ServiceManager\ServiceLocatorInterface;
class ExampleService implements ServiceLocatorAwareInterface
{
use ServiceLocatorAwareTrait;
public function doSomething()
{
// ...
$event = new ExampleEvent(ExampleEvent::EVENT_FOOBAR);
$event->setValue(42);
$serviceLocator = $this->getServiceLocator();
$exampleManager = $serviceLocator->get('Example\ExampleManager');
$exampleManager->newExample($event);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment