PluginProbe ʕ •ᴥ•ʔ
Matomo Analytics – Powerful, Privacy-First Insights for WordPress / 4.13.2
Matomo Analytics – Powerful, Privacy-First Insights for WordPress v4.13.2
5.11.1 5.11.0 5.10.2 5.10.1 trunk 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.1.0 1.1.1 1.1.2 1.1.3 1.2.0 1.3.0 1.3.1 1.3.2 4.0.0 4.0.1 4.0.2 4.0.3 4.0.4 4.1.0 4.1.1 4.1.2 4.1.3 4.10.0 4.11.0 4.12.0 4.13.0 4.13.2 4.13.3 4.13.4 4.13.5 4.14.0 4.14.1 4.14.2 4.15.0 4.15.1 4.15.2 4.15.3 4.2.0 4.3.0 4.3.1 4.4.1 4.4.2 4.5.0 4.6.0 5.0.1 5.0.2 5.0.3 5.0.4 5.0.5 5.0.6 5.0.7 5.0.8 5.1.0 5.1.1 5.1.2 5.1.3 5.1.4 5.1.5 5.1.6 5.1.7 5.10.0 5.2.0 5.2.1 5.2.2 5.3.0 5.3.1 5.3.2 5.3.3 5.6.0 5.6.1 5.7.0 5.7.1 5.8.0 5.8.1 5.8.2
matomo / app / core / EventDispatcher.php
matomo / app / core Last commit date
API 3 years ago Access 3 years ago Application 4 years ago Archive 3 years ago ArchiveProcessor 3 years ago Archiver 5 years ago AssetManager 3 years ago Auth 3 years ago Category 5 years ago Changes 4 years ago CliMulti 4 years ago Columns 3 years ago Concurrency 3 years ago Config 4 years ago Container 4 years ago CronArchive 3 years ago DataAccess 3 years ago DataFiles 5 years ago DataTable 3 years ago Db 3 years ago DeviceDetector 3 years ago Email 5 years ago Exception 3 years ago Http 4 years ago Intl 4 years ago Mail 3 years ago Measurable 5 years ago Menu 3 years ago Metrics 4 years ago Notification 4 years ago Period 4 years ago Plugin 3 years ago ProfessionalServices 4 years ago Report 5 years ago ReportRenderer 3 years ago Scheduler 4 years ago Segment 3 years ago Session 4 years ago Settings 3 years ago Tracker 3 years ago Translation 4 years ago UpdateCheck 5 years ago Updater 3 years ago Updates 3 years ago Validators 4 years ago View 4 years ago ViewDataTable 3 years ago Visualization 4 years ago Widget 5 years ago .htaccess 6 years ago Access.php 4 years ago Archive.php 4 years ago ArchiveProcessor.php 4 years ago AssetManager.php 4 years ago Auth.php 5 years ago AuthResult.php 5 years ago BaseFactory.php 5 years ago Cache.php 5 years ago CacheId.php 5 years ago CliMulti.php 4 years ago Common.php 3 years ago Config.php 3 years ago Console.php 3 years ago Context.php 5 years ago Cookie.php 3 years ago CronArchive.php 3 years ago DataArray.php 4 years ago DataTable.php 3 years ago Date.php 4 years ago Db.php 4 years ago DbHelper.php 3 years ago Development.php 5 years ago ErrorHandler.php 5 years ago EventDispatcher.php 3 years ago ExceptionHandler.php 3 years ago FileIntegrity.php 5 years ago Filechecks.php 4 years ago Filesystem.php 3 years ago FrontController.php 3 years ago Http.php 3 years ago IP.php 4 years ago Log.php 4 years ago LogDeleter.php 4 years ago Mail.php 4 years ago Metrics.php 3 years ago NoAccessException.php 5 years ago Nonce.php 3 years ago Notification.php 5 years ago NumberFormatter.php 4 years ago Option.php 4 years ago Period.php 5 years ago Piwik.php 3 years ago Plugin.php 4 years ago Profiler.php 4 years ago ProxyHeaders.php 5 years ago ProxyHttp.php 3 years ago QuickForm2.php 5 years ago RankingQuery.php 3 years ago ReportRenderer.php 4 years ago Segment.php 3 years ago Sequence.php 5 years ago Session.php 3 years ago SettingsPiwik.php 3 years ago SettingsServer.php 5 years ago Singleton.php 5 years ago Site.php 3 years ago SiteContentDetector.php 3 years ago SupportedBrowser.php 3 years ago TCPDF.php 5 years ago Theme.php 5 years ago Timer.php 5 years ago Tracker.php 4 years ago Twig.php 4 years ago Unzip.php 5 years ago UpdateCheck.php 5 years ago Updater.php 4 years ago UpdaterErrorException.php 5 years ago Updates.php 5 years ago Url.php 3 years ago UrlHelper.php 3 years ago Version.php 3 years ago View.php 3 years ago bootstrap.php 3 years ago dispatch.php 5 years ago testMinimumPhpVersion.php 3 years ago
EventDispatcher.php
219 lines
1 <?php
2 /**
3 * Matomo - 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 public static $_SKIP_EVENTS_IN_TESTS = false;
64
65 /**
66 * Constructor.
67 */
68 public function __construct(Plugin\Manager $pluginManager, array $observers = array())
69 {
70 $this->pluginManager = $pluginManager;
71
72 foreach ($observers as $observerInfo) {
73 list($eventName, $callback) = $observerInfo;
74 $this->extraObservers[$eventName][] = $callback;
75 }
76 }
77
78 /**
79 * Triggers an event, executing all callbacks associated with it.
80 *
81 * @param string $eventName The name of the event, ie, API.getReportMetadata.
82 * @param array $params The parameters to pass to each callback when executing.
83 * @param bool $pending Whether this event should be posted again for plugins
84 * loaded after the event is fired.
85 * @param array|null $plugins The plugins to post events to. If null, the event
86 * is posted to all plugins. The elements of this array
87 * can be either the Plugin objects themselves
88 * or their string names.
89 */
90 public function postEvent($eventName, $params, $pending = false, $plugins = null)
91 {
92 if (self::$_SKIP_EVENTS_IN_TESTS) {
93 return;
94 }
95
96 if ($pending) {
97 $this->pendingEvents[] = array($eventName, $params);
98 }
99
100 $manager = $this->pluginManager;
101
102 if (empty($plugins)) {
103 $plugins = $manager->getPluginsLoadedAndActivated();
104 }
105
106 $callbacks = array();
107
108 // collect all callbacks to execute
109 foreach ($plugins as $pluginName) {
110 if (!is_string($pluginName)) {
111 $pluginName = $pluginName->getPluginName();
112 }
113
114 if (!isset($this->pluginHooks[$pluginName])) {
115 $plugin = $manager->getLoadedPlugin($pluginName);
116 $this->pluginHooks[$pluginName] = $plugin->registerEvents();
117 }
118
119 $hooks = $this->pluginHooks[$pluginName];
120
121 if (isset($hooks[$eventName])) {
122 list($pluginFunction, $callbackGroup) = $this->getCallbackFunctionAndGroupNumber($hooks[$eventName]);
123
124 if (is_string($pluginFunction)) {
125 $plugin = $manager->getLoadedPlugin($pluginName);
126 $callbacks[$callbackGroup][] = array($plugin, $pluginFunction) ;
127 } else {
128 $callbacks[$callbackGroup][] = $pluginFunction;
129 }
130 }
131 }
132
133 if (isset($this->extraObservers[$eventName])) {
134 foreach ($this->extraObservers[$eventName] as $callbackInfo) {
135 list($callback, $callbackGroup) = $this->getCallbackFunctionAndGroupNumber($callbackInfo);
136
137 $callbacks[$callbackGroup][] = $callback;
138 }
139 }
140
141 // sort callbacks by their importance
142 ksort($callbacks);
143
144 // execute callbacks in order
145 foreach ($callbacks as $callbackGroup) {
146 foreach ($callbackGroup as $callback) {
147 call_user_func_array($callback, $params);
148 }
149 }
150 }
151
152 /**
153 * Associates a callback that is not a plugin class method with an event
154 * name.
155 *
156 * @param string $eventName
157 * @param array|callable $callback This can be a normal PHP callback or an array
158 * that looks like this:
159 * array(
160 * 'function' => $callback,
161 * 'before' => true
162 * )
163 * or this:
164 * array(
165 * 'function' => $callback,
166 * 'after' => true
167 * )
168 * If 'before' is set, the callback will be executed
169 * before normal & 'after' ones. If 'after' then it
170 * will be executed after normal ones.
171 */
172 public function addObserver($eventName, $callback)
173 {
174 $this->extraObservers[$eventName][] = $callback;
175 }
176
177 /**
178 * Re-posts all pending events to the given plugin.
179 *
180 * @param Plugin $plugin
181 */
182 public function postPendingEventsTo($plugin)
183 {
184 foreach ($this->pendingEvents as $eventInfo) {
185 [$eventName, $eventParams] = $eventInfo;
186 $this->postEvent($eventName, $eventParams, $pending = false, array($plugin));
187 }
188 }
189
190 /**
191 * @internal For testing purpose only
192 */
193 public function clearCache()
194 {
195 $this->pluginHooks = [];
196 }
197
198 private function getCallbackFunctionAndGroupNumber($hookInfo)
199 {
200 if (is_array($hookInfo)
201 && !empty($hookInfo['function'])
202 ) {
203 $pluginFunction = $hookInfo['function'];
204 if (!empty($hookInfo['before'])) {
205 $callbackGroup = self::EVENT_CALLBACK_GROUP_FIRST;
206 } elseif (!empty($hookInfo['after'])) {
207 $callbackGroup = self::EVENT_CALLBACK_GROUP_THIRD;
208 } else {
209 $callbackGroup = self::EVENT_CALLBACK_GROUP_SECOND;
210 }
211 } else {
212 $pluginFunction = $hookInfo;
213 $callbackGroup = self::EVENT_CALLBACK_GROUP_SECOND;
214 }
215
216 return array($pluginFunction, $callbackGroup);
217 }
218 }
219