DefaultEventManager.php
1 <?php
2 /**
3  * wCMF - wemove Content Management Framework
4  * Copyright (C) 2005-2020 wemove digital solutions GmbH
5  *
6  * Licensed under the terms of the MIT License.
7  *
8  * See the LICENSE file distributed with this work for
9  * additional information.
10  */
11 namespace wcmf\lib\core\impl;
12 
15 
16 /**
17  * DefaultEventManager is a simple EventManager implementation.
18  *
19  * @author ingo herwig <ingo@wemove.com>
20  */
22 
23  private $listeners = [];
24 
25  /**
26  * @see EventManager::addListener()
27  */
28  public function addListener($eventName, $callback) {
29  if (!isset($this->listeners[$eventName])) {
30  $this->listeners[$eventName] = [];
31  }
32  $this->listeners[$eventName][] = $callback;
33  }
34 
35  /**
36  * @see EventManager::removeListener()
37  */
38  public function removeListener($eventName, $callback) {
39  if (isset($this->listeners[$eventName])) {
40  $listeners = [];
41  for ($i=0, $count=sizeof($this->listeners[$eventName]); $i<$count; $i++) {
42  $curCallback = $this->listeners[$eventName][$i];
43  if ($curCallback != $callback) {
44  $listeners[] = $curCallback;
45  }
46  }
47  $this->listeners[$eventName] = $listeners;
48  }
49  }
50 
51  /**
52  * @see EventManager::dispatch()
53  */
54  public function dispatch($eventName, Event $event) {
55  if (isset($this->listeners[$eventName])) {
56  for ($i=0, $count=sizeof($this->listeners[$eventName]); $i<$count; $i++) {
57  $curCallback = $this->listeners[$eventName][$i];
58  call_user_func($curCallback, $event);
59  if ($event->isStopped()) {
60  break;
61  }
62  }
63  }
64  }
65 }
66 ?>
isStopped()
Check if the event is stopped.
Definition: Event.php:33
EventManager is responsible for dispatching events to registered listeners.
Event is the base class for all events.
Definition: Event.php:18
DefaultEventManager is a simple EventManager implementation.