Skip to content

Commit

Permalink
refactor: use new container to get services
Browse files Browse the repository at this point in the history
  • Loading branch information
shpran committed Sep 29, 2021
1 parent d34eccc commit b8c75fd
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions common/oatbox/event/EventManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ class EventManager extends ConfigurableService
* @deprecated use SERVICE_ID
*/
const CONFIG_ID = 'generis/event';

const OPTION_LISTENERS = 'listeners';

/**
* Dispatch an event and trigger its listeners
*
Expand All @@ -49,21 +49,23 @@ public function trigger($event, $params = [])
{
$eventObject = is_object($event) ? $event : new GenericEvent($event, $params);
foreach ($this->getListeners($eventObject) as $callback) {
if (is_array($callback) && count($callback) == 2) {
list($key, $function) = $callback;
if (is_array($callback) && count($callback) === 2) {
[$key, $function] = $callback;

if (is_string($key)) {
try {
$service = $this->getServiceManager()->get($key);
$service = $this->getServiceLocator()->getContainer()->get($key);
$callback = [$service, $function];
} catch (ServiceNotFoundException $e) {
//do nothing
}
}
}

call_user_func($callback, $eventObject);
}
}

/**
* Attach a Listener to one or multiple events
*
Expand All @@ -86,7 +88,7 @@ public function attach($event, $callback)
}
$this->setOption(self::OPTION_LISTENERS, $listeners);
}

/**
* remove listener from an event and delete event if it dosn't have any listeners
* @param array $listeners
Expand Down Expand Up @@ -126,7 +128,7 @@ public function detach($event, $callback)
}
$this->setOption(self::OPTION_LISTENERS, $listeners);
}

/**
* Get all Listeners listening to this kind of event
*
Expand Down

0 comments on commit b8c75fd

Please sign in to comment.