Skip to content

Instantly share code, notes, and snippets.

@geggleto
Created June 22, 2020 02:16
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save geggleto/28a36199df22d266cead4c5d78f32789 to your computer and use it in GitHub Desktop.
Save geggleto/28a36199df22d266cead4c5d78f32789 to your computer and use it in GitHub Desktop.
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