API
6 years ago
Access
6 years ago
Application
6 years ago
Archive
6 years ago
ArchiveProcessor
6 years ago
Archiver
6 years ago
AssetManager
6 years ago
Auth
6 years ago
Category
6 years ago
CliMulti
6 years ago
Columns
6 years ago
Composer
6 years ago
Concurrency
6 years ago
Config
6 years ago
Container
6 years ago
CronArchive
6 years ago
DataAccess
5 years ago
DataFiles
6 years ago
DataTable
6 years ago
Db
6 years ago
DeviceDetector
5 years ago
Email
6 years ago
Exception
6 years ago
Http
6 years ago
Intl
6 years ago
Mail
6 years ago
Measurable
6 years ago
Menu
6 years ago
Metrics
6 years ago
Notification
6 years ago
Period
6 years ago
Plugin
6 years ago
ProfessionalServices
6 years ago
Report
6 years ago
ReportRenderer
6 years ago
Scheduler
6 years ago
Segment
6 years ago
Session
6 years ago
Settings
6 years ago
Tracker
5 years ago
Translation
6 years ago
UpdateCheck
6 years ago
Updater
6 years ago
Updates
6 years ago
Validators
6 years ago
View
6 years ago
ViewDataTable
6 years ago
Visualization
6 years ago
Widget
6 years ago
.htaccess
6 years ago
Access.php
6 years ago
Archive.php
6 years ago
ArchiveProcessor.php
6 years ago
AssetManager.php
6 years ago
Auth.php
6 years ago
BaseFactory.php
6 years ago
Cache.php
6 years ago
CacheId.php
6 years ago
CliMulti.php
6 years ago
Common.php
6 years ago
Config.php
6 years ago
Console.php
6 years ago
Context.php
6 years ago
Cookie.php
5 years ago
CronArchive.php
5 years ago
DataArray.php
6 years ago
DataTable.php
6 years ago
Date.php
6 years ago
Db.php
6 years ago
DbHelper.php
6 years ago
Development.php
6 years ago
DeviceDetectorFactory.php
6 years ago
ErrorHandler.php
6 years ago
EventDispatcher.php
6 years ago
ExceptionHandler.php
6 years ago
FileIntegrity.php
6 years ago
Filechecks.php
6 years ago
Filesystem.php
6 years ago
FrontController.php
6 years ago
Http.php
6 years ago
IP.php
6 years ago
Log.php
6 years ago
LogDeleter.php
6 years ago
Mail.php
6 years ago
Metrics.php
6 years ago
MetricsFormatter.php
6 years ago
Nonce.php
5 years ago
Notification.php
6 years ago
NumberFormatter.php
6 years ago
Option.php
5 years ago
Period.php
6 years ago
Piwik.php
6 years ago
Plugin.php
6 years ago
Profiler.php
6 years ago
ProxyHeaders.php
6 years ago
ProxyHttp.php
6 years ago
QuickForm2.php
6 years ago
RankingQuery.php
6 years ago
Registry.php
6 years ago
ReportRenderer.php
6 years ago
ScheduledTask.php
6 years ago
Segment.php
6 years ago
Sequence.php
6 years ago
Session.php
6 years ago
SettingsPiwik.php
6 years ago
SettingsServer.php
6 years ago
Singleton.php
6 years ago
Site.php
6 years ago
TCPDF.php
6 years ago
TaskScheduler.php
6 years ago
Theme.php
6 years ago
Timer.php
6 years ago
Tracker.php
6 years ago
Translate.php
6 years ago
Twig.php
6 years ago
Unzip.php
6 years ago
UpdateCheck.php
6 years ago
Updater.php
6 years ago
Updates.php
6 years ago
Url.php
6 years ago
UrlHelper.php
6 years ago
Version.php
5 years ago
View.php
6 years ago
bootstrap.php
6 years ago
dispatch.php
6 years ago
testMinimumPhpVersion.php
6 years ago
EventDispatcher.php
205 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Piwik - free/libre analytics platform |
| 4 | * |
| 5 | * @link https://matomo.org |
| 6 | * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later |
| 7 | * |
| 8 | */ |
| 9 | |
| 10 | namespace Piwik; |
| 11 | |
| 12 | use Piwik\Container\StaticContainer; |
| 13 | |
| 14 | /** |
| 15 | * This class allows code to post events from anywhere in Piwik and for |
| 16 | * plugins to associate callbacks to be executed when events are posted. |
| 17 | */ |
| 18 | class EventDispatcher |
| 19 | { |
| 20 | /** |
| 21 | * @return EventDispatcher |
| 22 | */ |
| 23 | public static function getInstance() |
| 24 | { |
| 25 | return StaticContainer::get('Piwik\EventDispatcher'); |
| 26 | } |
| 27 | |
| 28 | // implementation details for postEvent |
| 29 | const EVENT_CALLBACK_GROUP_FIRST = 0; |
| 30 | const EVENT_CALLBACK_GROUP_SECOND = 1; |
| 31 | const EVENT_CALLBACK_GROUP_THIRD = 2; |
| 32 | |
| 33 | /** |
| 34 | * Array of observers (callbacks attached to events) that are not methods |
| 35 | * of plugin classes. |
| 36 | * |
| 37 | * @var array |
| 38 | */ |
| 39 | private $extraObservers = array(); |
| 40 | |
| 41 | /** |
| 42 | * Array storing information for all pending events. Each item in the array |
| 43 | * will be an array w/ two elements: |
| 44 | * |
| 45 | * array( |
| 46 | * 'Event.Name', // the event name |
| 47 | * array('event', 'parameters') // the parameters to pass to event observers |
| 48 | * ) |
| 49 | * |
| 50 | * @var array |
| 51 | */ |
| 52 | private $pendingEvents = array(); |
| 53 | |
| 54 | /** |
| 55 | * Plugin\Manager instance used to get list of loaded plugins. |
| 56 | * |
| 57 | * @var \Piwik\Plugin\Manager |
| 58 | */ |
| 59 | private $pluginManager; |
| 60 | |
| 61 | private $pluginHooks = array(); |
| 62 | |
| 63 | /** |
| 64 | * Constructor. |
| 65 | */ |
| 66 | public function __construct(Plugin\Manager $pluginManager, array $observers = array()) |
| 67 | { |
| 68 | $this->pluginManager = $pluginManager; |
| 69 | |
| 70 | foreach ($observers as $observerInfo) { |
| 71 | list($eventName, $callback) = $observerInfo; |
| 72 | $this->extraObservers[$eventName][] = $callback; |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Triggers an event, executing all callbacks associated with it. |
| 78 | * |
| 79 | * @param string $eventName The name of the event, ie, API.getReportMetadata. |
| 80 | * @param array $params The parameters to pass to each callback when executing. |
| 81 | * @param bool $pending Whether this event should be posted again for plugins |
| 82 | * loaded after the event is fired. |
| 83 | * @param array|null $plugins The plugins to post events to. If null, the event |
| 84 | * is posted to all plugins. The elements of this array |
| 85 | * can be either the Plugin objects themselves |
| 86 | * or their string names. |
| 87 | */ |
| 88 | public function postEvent($eventName, $params, $pending = false, $plugins = null) |
| 89 | { |
| 90 | if ($pending) { |
| 91 | $this->pendingEvents[] = array($eventName, $params); |
| 92 | } |
| 93 | |
| 94 | $manager = $this->pluginManager; |
| 95 | |
| 96 | if (empty($plugins)) { |
| 97 | $plugins = $manager->getPluginsLoadedAndActivated(); |
| 98 | } |
| 99 | |
| 100 | $callbacks = array(); |
| 101 | |
| 102 | // collect all callbacks to execute |
| 103 | foreach ($plugins as $pluginName) { |
| 104 | if (!is_string($pluginName)) { |
| 105 | $pluginName = $pluginName->getPluginName(); |
| 106 | } |
| 107 | |
| 108 | if (!isset($this->pluginHooks[$pluginName])) { |
| 109 | $plugin = $manager->getLoadedPlugin($pluginName); |
| 110 | $this->pluginHooks[$pluginName] = $plugin->getListHooksRegistered(); |
| 111 | } |
| 112 | |
| 113 | $hooks = $this->pluginHooks[$pluginName]; |
| 114 | |
| 115 | if (isset($hooks[$eventName])) { |
| 116 | list($pluginFunction, $callbackGroup) = $this->getCallbackFunctionAndGroupNumber($hooks[$eventName]); |
| 117 | |
| 118 | if (is_string($pluginFunction)) { |
| 119 | $plugin = $manager->getLoadedPlugin($pluginName); |
| 120 | $callbacks[$callbackGroup][] = array($plugin, $pluginFunction) ; |
| 121 | } else { |
| 122 | $callbacks[$callbackGroup][] = $pluginFunction; |
| 123 | } |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | if (isset($this->extraObservers[$eventName])) { |
| 128 | foreach ($this->extraObservers[$eventName] as $callbackInfo) { |
| 129 | list($callback, $callbackGroup) = $this->getCallbackFunctionAndGroupNumber($callbackInfo); |
| 130 | |
| 131 | $callbacks[$callbackGroup][] = $callback; |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | // sort callbacks by their importance |
| 136 | ksort($callbacks); |
| 137 | |
| 138 | // execute callbacks in order |
| 139 | foreach ($callbacks as $callbackGroup) { |
| 140 | foreach ($callbackGroup as $callback) { |
| 141 | call_user_func_array($callback, $params); |
| 142 | } |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * Associates a callback that is not a plugin class method with an event |
| 148 | * name. |
| 149 | * |
| 150 | * @param string $eventName |
| 151 | * @param array|callable $callback This can be a normal PHP callback or an array |
| 152 | * that looks like this: |
| 153 | * array( |
| 154 | * 'function' => $callback, |
| 155 | * 'before' => true |
| 156 | * ) |
| 157 | * or this: |
| 158 | * array( |
| 159 | * 'function' => $callback, |
| 160 | * 'after' => true |
| 161 | * ) |
| 162 | * If 'before' is set, the callback will be executed |
| 163 | * before normal & 'after' ones. If 'after' then it |
| 164 | * will be executed after normal ones. |
| 165 | */ |
| 166 | public function addObserver($eventName, $callback) |
| 167 | { |
| 168 | $this->extraObservers[$eventName][] = $callback; |
| 169 | } |
| 170 | |
| 171 | /** |
| 172 | * Re-posts all pending events to the given plugin. |
| 173 | * |
| 174 | * @param Plugin $plugin |
| 175 | */ |
| 176 | public function postPendingEventsTo($plugin) |
| 177 | { |
| 178 | foreach ($this->pendingEvents as $eventInfo) { |
| 179 | list($eventName, $eventParams) = $eventInfo; |
| 180 | $this->postEvent($eventName, $eventParams, $pending = false, array($plugin)); |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | private function getCallbackFunctionAndGroupNumber($hookInfo) |
| 185 | { |
| 186 | if (is_array($hookInfo) |
| 187 | && !empty($hookInfo['function']) |
| 188 | ) { |
| 189 | $pluginFunction = $hookInfo['function']; |
| 190 | if (!empty($hookInfo['before'])) { |
| 191 | $callbackGroup = self::EVENT_CALLBACK_GROUP_FIRST; |
| 192 | } elseif (!empty($hookInfo['after'])) { |
| 193 | $callbackGroup = self::EVENT_CALLBACK_GROUP_THIRD; |
| 194 | } else { |
| 195 | $callbackGroup = self::EVENT_CALLBACK_GROUP_SECOND; |
| 196 | } |
| 197 | } else { |
| 198 | $pluginFunction = $hookInfo; |
| 199 | $callbackGroup = self::EVENT_CALLBACK_GROUP_SECOND; |
| 200 | } |
| 201 | |
| 202 | return array($pluginFunction, $callbackGroup); |
| 203 | } |
| 204 | } |
| 205 |