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