Last active
August 29, 2015 14:22
-
-
Save samuelmaddock/bb8e4886d250f72939a3 to your computer and use it in GitHub Desktop.
ZF2 Eventing Skeleton
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// place under /config/autoload/ | |
return [ | |
'example-manager' => [ | |
'listeners' => [ | |
'Example\ExampleListener' | |
] | |
] | |
]; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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 | |
} | |
// ... | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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