Skip to content

Instantly share code, notes, and snippets.

@SofHad
Created August 13, 2013 21:06
Show Gist options
  • Save SofHad/6225719 to your computer and use it in GitHub Desktop.
Save SofHad/6225719 to your computer and use it in GitHub Desktop.
{
"require": {
"symfony/event-dispatcher": "2.1.0-stable"
}
}
<?php
require_once 'vendor/autoload.php';
use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\EventDispatcher\Event;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class CallbackAggregateSubscriber implements EventSubscriberInterface {
public static function getSubscribedEvents(){
return array(
'setup' => 'onSetup',
'teardown' => 'onTearDown',
);
}
public function onSetUp(Event $event){
echo 'CallbackAggregateSubscriber:' . $event->getName() . "\n";
}
public function onTearDown(Event $event){
echo 'CallbackAggregateSubscriber:' . $event->getName() . "\n";
}
}
$eventManager = new EventDispatcher();
//Subscriber
$eventManager->addSubscriber( new CallbackAggregateSubscriber() );
//Closure
$eventManager->addListener('setup', function(Event $event){
echo $event->getName() . "\n";
});
//Notify event
$eventManager->dispatch('setup');
$eventManager->dispatch('teardown');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment