-
-
Save geggleto/28a36199df22d266cead4c5d78f32789 to your computer and use it in GitHub Desktop.
This file contains 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
class EventDispatcher implements EventDispatcherInterface | |
{ | |
/** @var array */ | |
protected $listeners; | |
/** @var ContainerInterface */ | |
protected $container; | |
/** | |
* EventDipsatcher constructor. | |
* @param ContainerInterface $container | |
*/ | |
public function __construct(ContainerInterface $container) | |
{ | |
$this->listeners = []; | |
$this->container = $container; | |
} | |
/** | |
* @param $eventName | |
* @param $listenerClass | |
*/ | |
public function addListener($eventName, $listenerClass) { | |
if (!isset($this->listeners[$eventName])) { | |
$this->listeners[$eventName] = []; | |
} | |
$this->listeners[$eventName][] = $listenerClass; | |
} | |
/** | |
* @param AbstractEvent $event | |
*/ | |
public function raise(AbstractEvent $event) { | |
if (isset($this->listeners[$event->getName()])) { | |
foreach ($this->listeners[$event->getName()] as $listener) { | |
$base = $this->container->get($listener[0]); | |
/** @var $listener callable */ | |
call_user_func([$base, $listener[1]], $event); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment