PluginProbe ʕ •ᴥ•ʔ
Matomo Analytics – Powerful, Privacy-First Insights for WordPress / 5.12.1
Matomo Analytics – Powerful, Privacy-First Insights for WordPress v5.12.1
5.12.1 5.12.0 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 / API / Proxy.php
matomo / app / core / API Last commit date
DataTableManipulator 3 weeks ago ApiRenderer.php 3 weeks ago CORSHandler.php 1 year ago DataTableGenericFilter.php 3 weeks ago DataTableManipulator.php 3 weeks ago DataTablePostProcessor.php 3 weeks ago DocumentationGenerator.php 3 weeks ago Inconsistencies.php 3 weeks ago NoDefaultValue.php 2 years ago Proxy.php 3 weeks ago Request.php 2 months ago ResponseBuilder.php 3 weeks ago
Proxy.php
647 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\API;
10
11 use Exception;
12 use Piwik\Http\BadRequestException;
13 use Piwik\Common;
14 use Piwik\Container\StaticContainer;
15 use Piwik\Context;
16 use Piwik\Piwik;
17 use Piwik\Plugin\API;
18 use Piwik\Plugin\Manager;
19 use ReflectionClass;
20 use ReflectionMethod;
21 // prevent upgrade error eg from Matomo 3.x to Matomo 4.x. Refs https://github.com/matomo-org/matomo/pull/16468
22 // the `false` is important otherwise it would fail and try to load the proxy.php file again.
23 if (!class_exists('Piwik\\API\\NoDefaultValue', \false)) {
24 // phpcs:ignoreFile PSR1.Classes.ClassDeclaration.MultipleClasses
25 class NoDefaultValue
26 {
27 }
28 }
29 /**
30 * Proxy is a singleton that has the knowledge of every method available, their parameters
31 * and default values.
32 * Proxy receives all the API calls requests via call() and forwards them to the right
33 * object, with the parameters in the right order.
34 */
35 class Proxy
36 {
37 // array of already registered plugins names
38 protected $alreadyRegistered = array();
39 protected $metadataArray = array();
40 private $hideIgnoredFunctions = \true;
41 // when a parameter doesn't have a default value we use this
42 private $noDefaultValue;
43 public function __construct()
44 {
45 $this->noDefaultValue = new \Piwik\API\NoDefaultValue();
46 }
47 public static function getInstance()
48 {
49 return StaticContainer::get(self::class);
50 }
51 /**
52 * Returns array containing reflection meta data for all the loaded classes
53 * eg. number of parameters, method names, etc.
54 *
55 * @return array
56 */
57 public function getMetadata()
58 {
59 ksort($this->metadataArray);
60 return $this->metadataArray;
61 }
62 /**
63 * Registers the API information of a given module.
64 *
65 * The module to be registered must be
66 * - a singleton (providing a getInstance() method)
67 * - the API file must be located in plugins/ModuleName/API.php
68 * for example plugins/Referrers/API.php
69 *
70 * The method will introspect the methods, their parameters, etc.
71 *
72 * @param string $className Fully qualified API class name, eg. "\Piwik\Plugins\Referrers\API"
73 */
74 public function registerClass($className)
75 {
76 if (isset($this->alreadyRegistered[$className])) {
77 return;
78 }
79 $this->includeApiFile($className);
80 $this->checkClassIsSingleton($className);
81 $rClass = new ReflectionClass($className);
82 if (!$this->shouldHideAPIMethod($rClass->getDocComment())) {
83 foreach ($rClass->getMethods() as $method) {
84 $this->loadMethodMetadata($className, $method);
85 }
86 $this->setDocumentation($rClass, $className);
87 $this->alreadyRegistered[$className] = \true;
88 }
89 }
90 /**
91 * Will be displayed in the API page
92 *
93 * @param ReflectionClass $rClass Instance of ReflectionClass
94 * @param string $className Name of the class
95 */
96 private function setDocumentation($rClass, $className)
97 {
98 // Doc comment
99 $doc = $rClass->getDocComment();
100 $doc = $this->removeDocblockAnnotationBlocks($doc, 'phpstan');
101 $doc = str_replace(" * " . \PHP_EOL, "<br>", $doc);
102 // boldify the first line only if there is more than one line, otherwise too much bold
103 if (substr_count($doc, '<br>') > 1) {
104 $firstLineBreak = strpos($doc, "<br>");
105 $doc = "<div class='apiFirstLine'>" . substr($doc, 0, $firstLineBreak) . "</div>" . substr($doc, $firstLineBreak + strlen("<br>"));
106 }
107 $doc = preg_replace("/(@package)[a-z _A-Z]*/", "", $doc);
108 $doc = preg_replace("/(@method).*/", "", $doc);
109 $doc = str_replace(array("\t", "\n", "/**", "*/", " * ", " *", " ", "\t*", " * @package"), " ", $doc);
110 // replace 'foo' and `bar` and "foobar" with code blocks... much magic
111 $doc = preg_replace('/`(.*?)`/', '<code>$1</code>', $doc);
112 $this->metadataArray[$className]['__documentation'] = $doc;
113 }
114 /**
115 * Removes docblock annotations and their continuation lines.
116 *
117 * For example, this removes `@phpstan-type` and the following multiline shape definition.
118 *
119 * @param string|false $doc
120 * @param string $annotationPrefix
121 * @return string|false
122 */
123 private function removeDocblockAnnotationBlocks($doc, $annotationPrefix)
124 {
125 if (!is_string($doc) || $doc === '') {
126 return $doc;
127 }
128 $lines = preg_split('/\\R/', $doc);
129 $result = [];
130 $isSkipping = \false;
131 foreach ($lines as $line) {
132 if (preg_match('/^\\s*\\*\\s*@' . preg_quote($annotationPrefix, '/') . '\\S*/', $line)) {
133 $isSkipping = \true;
134 continue;
135 }
136 if ($isSkipping) {
137 // stop skipping once a new annotation starts
138 if (preg_match('/^\\s*\\*\\s*@\\S+/', $line)) {
139 $isSkipping = \false;
140 } elseif (preg_match('/^\\s*\\*\\/\\s*$/', $line)) {
141 $isSkipping = \false;
142 } else {
143 continue;
144 }
145 }
146 $result[] = $line;
147 }
148 return implode(\PHP_EOL, $result);
149 }
150 /**
151 * Returns number of classes already loaded
152 * @return int
153 */
154 public function getCountRegisteredClasses()
155 {
156 return count($this->alreadyRegistered);
157 }
158 /**
159 * Will execute $className->$methodName($parametersValues)
160 * If any error is detected (wrong number of parameters, method not found, class not found, etc.)
161 * it will throw an exception
162 *
163 * @param string $className The class name (eg. API)
164 * @param string $methodName The method name
165 * @param array $parametersRequest The parameters pairs (name=>value)
166 *
167 * @return mixed|null
168 * @throws Exception|\Piwik\NoAccessException
169 */
170 public function call($className, $methodName, $parametersRequest)
171 {
172 // Temporarily sets the Request array to this API call context
173 return Context::executeWithQueryParameters($parametersRequest, function () use($className, $methodName, $parametersRequest) {
174 $this->registerClass($className);
175 $request = new \Piwik\Request($parametersRequest);
176 /**
177 * instantiate the object
178 * @var API $object
179 */
180 $object = $className::getInstance();
181 // check method exists
182 $this->checkMethodExists($className, $methodName);
183 // get the list of parameters required by the method
184 $parameterNamesDefaultValuesAndTypes = $this->getParametersListWithTypes($className, $methodName);
185 // load parameters in the right order, etc.
186 if ($object->usesAutoSanitizeInputParams() && !$this->usesUnsanitizedInputParams($className, $methodName)) {
187 $finalParameters = $this->getSanitizedRequestParametersArray($parameterNamesDefaultValuesAndTypes, $request->getParameters());
188 } else {
189 $finalParameters = $this->getRequestParametersArray($parameterNamesDefaultValuesAndTypes, $request);
190 }
191 // allow plugins to manipulate the value
192 $pluginName = $this->getModuleNameFromClassName($className);
193 $returnedValue = null;
194 /**
195 * Triggered before an API request is dispatched.
196 *
197 * This event can be used to modify the arguments passed to one or more API methods.
198 *
199 * **Example**
200 *
201 * Piwik::addAction('API.Request.dispatch', function (&$parameters, $pluginName, $methodName) {
202 * if ($pluginName == 'Actions') {
203 * if ($methodName == 'getPageUrls') {
204 * // ... do something ...
205 * } else {
206 * // ... do something else ...
207 * }
208 * }
209 * });
210 *
211 * @param array &$finalParameters List of parameters that will be passed to the API method.
212 * @param string $pluginName The name of the plugin the API method belongs to.
213 * @param string $methodName The name of the API method that will be called.
214 */
215 Piwik::postEvent('API.Request.dispatch', array(&$finalParameters, $pluginName, $methodName));
216 /**
217 * Triggered before an API request is dispatched.
218 *
219 * This event exists for convenience and is triggered directly after the {@hook API.Request.dispatch}
220 * event is triggered. It can be used to modify the arguments passed to a **single** API method.
221 *
222 * _Note: This is can be accomplished with the {@hook API.Request.dispatch} event as well, however
223 * event handlers for that event will have to do more work._
224 *
225 * **Example**
226 *
227 * Piwik::addAction('API.Actions.getPageUrls', function (&$parameters) {
228 * // force use of a single website. for some reason.
229 * $parameters['idSite'] = 1;
230 * });
231 *
232 * @param array &$finalParameters List of parameters that will be passed to the API method.
233 */
234 Piwik::postEvent(sprintf('API.%s.%s', $pluginName, $methodName), array(&$finalParameters));
235 /**
236 * Triggered before an API request is dispatched.
237 *
238 * Use this event to intercept an API request and execute your own code instead. If you set
239 * `$returnedValue` in a handler for this event, the original API method will not be executed,
240 * and the result will be what you set in the event handler.
241 *
242 * @param mixed &$returnedValue Set this to set the result and preempt normal API invocation.
243 * @param array &$finalParameters List of parameters that will be passed to the API method.
244 * @param string $pluginName The name of the plugin the API method belongs to.
245 * @param string $methodName The name of the API method that will be called.
246 * @param array $parametersRequest The query parameters for this request.
247 */
248 Piwik::postEvent('API.Request.intercept', [&$returnedValue, $finalParameters, $pluginName, $methodName, $parametersRequest]);
249 $apiParametersInCorrectOrder = array();
250 foreach ($parameterNamesDefaultValuesAndTypes as $name => $parameter) {
251 if (isset($finalParameters[$name]) || array_key_exists($name, $finalParameters)) {
252 $apiParametersInCorrectOrder[] = $finalParameters[$name];
253 }
254 }
255 // call the method if a hook hasn't already set an output variable
256 if ($returnedValue === null) {
257 $returnedValue = call_user_func_array(array($object, $methodName), $apiParametersInCorrectOrder);
258 }
259 $endHookParams = array(&$returnedValue, array('className' => $className, 'module' => $pluginName, 'action' => $methodName, 'parameters' => $finalParameters));
260 /**
261 * Triggered directly after an API request is dispatched.
262 *
263 * This event exists for convenience and is triggered immediately before the
264 * {@hook API.Request.dispatch.end} event. It can be used to modify the output of a **single**
265 * API method.
266 *
267 * _Note: This can be accomplished with the {@hook API.Request.dispatch.end} event as well,
268 * however event handlers for that event will have to do more work._
269 *
270 * **Example**
271 *
272 * // append (0 hits) to the end of row labels whose row has 0 hits
273 * Piwik::addAction('API.Actions.getPageUrls', function (&$returnValue, $info)) {
274 * $returnValue->filter('ColumnCallbackReplace', 'label', function ($label, $hits) {
275 * if ($hits === 0) {
276 * return $label . " (0 hits)";
277 * } else {
278 * return $label;
279 * }
280 * }, null, array('nb_hits'));
281 * }
282 *
283 * @param mixed &$returnedValue The API method's return value. Can be an object, such as a
284 * {@link Piwik\DataTable DataTable} instance.
285 * could be a {@link Piwik\DataTable DataTable}.
286 * @param array $extraInfo An array holding information regarding the API request. Will
287 * contain the following data:
288 *
289 * - **className**: The namespace-d class name of the API instance
290 * that's being called.
291 * - **module**: The name of the plugin the API request was
292 * dispatched to.
293 * - **action**: The name of the API method that was executed.
294 * - **parameters**: The array of parameters passed to the API
295 * method.
296 */
297 Piwik::postEvent(sprintf('API.%s.%s.end', $pluginName, $methodName), $endHookParams);
298 /**
299 * Triggered directly after an API request is dispatched.
300 *
301 * This event can be used to modify the output of any API method.
302 *
303 * **Example**
304 *
305 * // append (0 hits) to the end of row labels whose row has 0 hits for any report that has the 'nb_hits' metric
306 * Piwik::addAction('API.Actions.getPageUrls.end', function (&$returnValue, $info)) {
307 * // don't process non-DataTable reports and reports that don't have the nb_hits column
308 * if (!($returnValue instanceof DataTableInterface)
309 * || in_array('nb_hits', $returnValue->getColumns())
310 * ) {
311 * return;
312 * }
313 *
314 * $returnValue->filter('ColumnCallbackReplace', 'label', function ($label, $hits) {
315 * if ($hits === 0) {
316 * return $label . " (0 hits)";
317 * } else {
318 * return $label;
319 * }
320 * }, null, array('nb_hits'));
321 * }
322 *
323 * @param mixed &$returnedValue The API method's return value. Can be an object, such as a
324 * {@link Piwik\DataTable DataTable} instance.
325 * @param array $extraInfo An array holding information regarding the API request. Will
326 * contain the following data:
327 *
328 * - **className**: The namespace-d class name of the API instance
329 * that's being called.
330 * - **module**: The name of the plugin the API request was
331 * dispatched to.
332 * - **action**: The name of the API method that was executed.
333 * - **parameters**: The array of parameters passed to the API
334 * method.
335 */
336 Piwik::postEvent('API.Request.dispatch.end', $endHookParams);
337 return $returnedValue;
338 });
339 }
340 /**
341 * Returns the parameters names and default values for the method $name
342 * of the class $class
343 *
344 * @param string $class The class name
345 * @param string $name The method name
346 * @return array Format array(
347 * 'testParameter' => null, // no default value
348 * 'life' => 42, // default value = 42
349 * 'date' => 'yesterday',
350 * );
351 */
352 public function getParametersList($class, $name)
353 {
354 return array_combine(array_keys($this->metadataArray[$class][$name]['parameters']), array_column($this->metadataArray[$class][$name]['parameters'], 'default'));
355 }
356 /**
357 * Returns the parameters names, default values and types for the method $name
358 * of the class $class
359 *
360 * @param string $class The class name
361 * @param string $name The method name
362 * @return array Format array(
363 * 'testParameter' => ['default' => null, 'type' => null], // no default value
364 * 'life' => ['default' => 42, 'type' => 'int'], // default value 42, type hint is int
365 * );
366 */
367 public function getParametersListWithTypes($class, $name)
368 {
369 return $this->metadataArray[$class][$name]['parameters'];
370 }
371 /**
372 * Check if given method name is deprecated or not.
373 */
374 public function isDeprecatedMethod($class, $methodName)
375 {
376 return $this->metadataArray[$class][$methodName]['isDeprecated'] ?? \false;
377 }
378 /**
379 * Check if given method uses unsanitized input parameters.
380 */
381 public function usesUnsanitizedInputParams($class, $methodName)
382 {
383 return $this->metadataArray[$class][$methodName]['unsanitizedInputParams'] ?? \false;
384 }
385 /**
386 * Returns the 'moduleName' part of '\\Piwik\\Plugins\\moduleName\\API'
387 *
388 * @param string $className "API"
389 * @return string "Referrers"
390 */
391 public function getModuleNameFromClassName($className)
392 {
393 return str_replace(array('\\Piwik\\Plugins\\', '\\API'), '', $className);
394 }
395 public function isExistingApiAction($pluginName, $apiAction)
396 {
397 $namespacedApiClassName = "\\Piwik\\Plugins\\{$pluginName}\\API";
398 $api = $namespacedApiClassName::getInstance();
399 return method_exists($api, $apiAction);
400 }
401 public function buildApiActionName($pluginName, $apiAction)
402 {
403 return sprintf("%s.%s", $pluginName, $apiAction);
404 }
405 /**
406 * Sets whether to hide '@ignore'd functions from method metadata or not.
407 *
408 * @param bool $hideIgnoredFunctions
409 */
410 public function setHideIgnoredFunctions($hideIgnoredFunctions)
411 {
412 $this->hideIgnoredFunctions = $hideIgnoredFunctions;
413 // make sure metadata gets reloaded
414 $this->alreadyRegistered = array();
415 $this->metadataArray = array();
416 }
417 /**
418 * Returns an array containing the *sanitized* values of the parameters to pass to the method to call
419 *
420 * @param array $requiredParameters array mapping parameter name to ['default' => value, 'type' => type]
421 * @param array $parametersRequest
422 * @throws Exception
423 * @return array values to pass to the function call
424 */
425 private function getSanitizedRequestParametersArray($requiredParameters, $parametersRequest)
426 {
427 $finalParameters = [];
428 foreach ($requiredParameters as $name => $parameter) {
429 try {
430 $defaultValue = $parameter['default'];
431 $type = $parameter['type'];
432 $request = new \Piwik\Request($parametersRequest);
433 if (in_array($name, ['segment', 'password', 'passwordConfirmation']) && !empty($parametersRequest[$name])) {
434 // special handling for some parameters:
435 // segment: we do not want to sanitize user input as it would break the segment encoding
436 // password / passwordConfirmation: sanitizing this parameters might change special chars in passwords, breaking login and confirmation boxes
437 $requestValue = $parametersRequest[$name];
438 } elseif ($defaultValue instanceof \Piwik\API\NoDefaultValue) {
439 if ($type === 'bool') {
440 $requestValue = $request->getBoolParameter($name);
441 } else {
442 $requestValue = Common::getRequestVar($name, null, $type, $parametersRequest);
443 }
444 } else {
445 try {
446 if ($type === 'bool') {
447 $requestValue = $request->getBoolParameter($name, $defaultValue);
448 } else {
449 $requestValue = Common::getRequestVar($name, $defaultValue, $type, $parametersRequest);
450 }
451 } catch (Exception $e) {
452 // Special case: empty parameter in the URL, should return the empty string, if no incompatible type is defined
453 if (isset($parametersRequest[$name]) && $parametersRequest[$name] === '' && (empty($type) || $type === 'string')) {
454 $requestValue = '';
455 } else {
456 $requestValue = $defaultValue;
457 }
458 }
459 }
460 } catch (Exception $e) {
461 throw new BadRequestException(Piwik::translate('General_PleaseSpecifyValue', [$name]));
462 }
463 $finalParameters[$name] = $requestValue;
464 }
465 return $finalParameters;
466 }
467 /**
468 * Returns an array containing the values of the parameters to pass to the method to call
469 *
470 * @param array $requiredParameters array mapping parameter name to ['default' => value, 'type' => type]
471 * @param \Piwik\Request $request
472 * @throws Exception
473 * @return array values to pass to the function call
474 */
475 private function getRequestParametersArray($requiredParameters, \Piwik\Request $request) : array
476 {
477 $finalParameters = [];
478 foreach ($requiredParameters as $name => $parameter) {
479 try {
480 $defaultValue = $parameter['default'];
481 $type = $parameter['type'] ?? '';
482 $requestValue = null;
483 switch (strtolower($type)) {
484 case 'bool':
485 $method = 'getBoolParameter';
486 break;
487 case 'int':
488 $method = 'getIntegerParameter';
489 break;
490 case 'string':
491 $method = 'getStringParameter';
492 break;
493 case 'float':
494 $method = 'getFloatParameter';
495 break;
496 case 'array':
497 $method = 'getArrayParameter';
498 break;
499 default:
500 $method = 'getParameter';
501 }
502 if ($defaultValue instanceof \Piwik\API\NoDefaultValue) {
503 $requestValue = $request->{$method}($name);
504 } elseif ($defaultValue === null) {
505 try {
506 $requestValue = $request->{$method}($name);
507 } catch (\InvalidArgumentException $e) {
508 $requestValue = null;
509 }
510 } else {
511 $requestValue = $request->{$method}($name, $defaultValue);
512 }
513 } catch (Exception $e) {
514 throw new BadRequestException(Piwik::translate('General_PleaseSpecifyValue', [$name]));
515 }
516 $finalParameters[$name] = $requestValue;
517 }
518 return $finalParameters;
519 }
520 /**
521 * Includes the class API by looking up plugins/xxx/API.php
522 *
523 * @param string $fileName api class name eg. "API"
524 * @throws Exception
525 */
526 private function includeApiFile($fileName)
527 {
528 $module = self::getModuleNameFromClassName($fileName);
529 $path = Manager::getPluginDirectory($module) . '/API.php';
530 if (is_readable($path)) {
531 require_once $path;
532 // prefixed by PIWIK_INCLUDE_PATH
533 } else {
534 throw new Exception("API module {$module} not found.");
535 }
536 }
537 /**
538 * @param string $class name of a class
539 * @param ReflectionMethod $method instance of ReflectionMethod
540 */
541 private function loadMethodMetadata($class, $method)
542 {
543 if (!$this->checkIfMethodIsAvailable($method)) {
544 return;
545 }
546 $name = $method->getName();
547 $parameters = $method->getParameters();
548 $docComment = $method->getDocComment();
549 $aParameters = array();
550 foreach ($parameters as $parameter) {
551 $nameVariable = $parameter->getName();
552 $defaultValue = $this->noDefaultValue;
553 if ($parameter->isDefaultValueAvailable()) {
554 $defaultValue = $parameter->getDefaultValue();
555 }
556 $type = $parameter->getType();
557 // In case no default value is defined in the method, but the type hint allows null, we assume null as default value
558 if ($type && $type->allowsNull() && $defaultValue === $this->noDefaultValue) {
559 $defaultValue = null;
560 }
561 $aParameters[$nameVariable] = ['default' => $defaultValue, 'type' => $type && $type->isBuiltin() ? $type->getName() : null, 'allowsNull' => $type ? $type->allowsNull() : $defaultValue === null];
562 }
563 $this->metadataArray[$class][$name]['parameters'] = $aParameters;
564 $this->metadataArray[$class][$name]['numberOfRequiredParameters'] = $method->getNumberOfRequiredParameters();
565 $this->metadataArray[$class][$name]['isDeprecated'] = \false !== strstr($docComment, '@deprecated');
566 $this->metadataArray[$class][$name]['unsanitizedInputParams'] = \false !== strstr($docComment, '@unsanitized');
567 }
568 /**
569 * Checks that the method exists in the class
570 *
571 * @param string $className The class name
572 * @param string $methodName The method name
573 * @throws Exception If the method is not found
574 */
575 private function checkMethodExists($className, $methodName)
576 {
577 if (!$this->isMethodAvailable($className, $methodName)) {
578 throw new BadRequestException(Piwik::translate('General_ExceptionMethodNotFound', [$methodName, $className]));
579 }
580 }
581 /**
582 * @param string|false $docComment
583 * @return bool
584 */
585 public function shouldHideAPIMethod($docComment)
586 {
587 $hideLine = strstr($docComment, '@hide');
588 if ($hideLine === \false) {
589 return \false;
590 }
591 $hideLine = trim($hideLine);
592 $hideLine .= ' ';
593 $token = trim(strtok($hideLine, " "), "\n");
594 $hide = \false;
595 if (!empty($token)) {
596 /**
597 * This event exists for checking whether a Plugin API class or a Plugin API method tagged
598 * with a `@hideXYZ` should be hidden in the API listing.
599 *
600 * @param bool &$hide whether to hide APIs tagged with $token should be displayed.
601 */
602 Piwik::postEvent(sprintf('API.DocumentationGenerator.%s', $token), array(&$hide));
603 }
604 return $hide;
605 }
606 /**
607 * @param ReflectionMethod $method
608 * @return bool
609 */
610 protected function checkIfMethodIsAvailable(ReflectionMethod $method)
611 {
612 if (!$method->isPublic() || $method->isConstructor() || $method->getName() === 'getInstance') {
613 return \false;
614 }
615 if ($this->hideIgnoredFunctions && \false !== strstr($method->getDocComment(), '@ignore')) {
616 return \false;
617 }
618 if ($this->shouldHideAPIMethod($method->getDocComment())) {
619 return \false;
620 }
621 return \true;
622 }
623 /**
624 * Returns true if the method is found in the API of the given class name.
625 *
626 * @param string $className The class name
627 * @param string $methodName The method name
628 * @return bool
629 */
630 private function isMethodAvailable($className, $methodName)
631 {
632 return isset($this->metadataArray[$className][$methodName]);
633 }
634 /**
635 * Checks that the class is a Singleton (presence of the getInstance() method)
636 *
637 * @param string $className The class name
638 * @throws Exception If the class is not a Singleton
639 */
640 private function checkClassIsSingleton($className)
641 {
642 if (!method_exists($className, "getInstance")) {
643 throw new Exception("{$className} that provide an API must be Singleton and have a 'public static function getInstance()' method.");
644 }
645 }
646 }
647