Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/ADF-660/Use new container to get services #925

Draft
wants to merge 2 commits into
base: develop
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 9 additions & 10 deletions common/oatbox/event/EventManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* Copyright (c) 2015-2022 (original work) Open Assessment Technologies SA;
*
*/

namespace oat\oatbox\event;
Expand All @@ -36,9 +35,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 @@ -48,26 +47,26 @@ class EventManager extends ConfigurableService
public function trigger($event, $params = [])
{
$container = $this->getServiceManager()->getContainer();

$event = is_object($event) ? $event : new GenericEvent($event, $params);

foreach ($this->getListeners($event) 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 = $container->get($key);
$callback = [$service, $function];
} catch (ServiceNotFoundException $e) {
//do nothing
// Do nothing
}
}
}

call_user_func($callback, $event);
}
}

/**
* Attach a Listener to one or multiple events
*
Expand All @@ -90,7 +89,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 @@ -130,7 +129,7 @@ public function detach($event, $callback)
}
$this->setOption(self::OPTION_LISTENERS, $listeners);
}

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