Dimension
6 years ago
API.php
6 years ago
AggregatedMetric.php
6 years ago
ArchivedMetric.php
6 years ago
Archiver.php
6 years ago
Categories.php
6 years ago
ComponentFactory.php
6 years ago
ComputedMetric.php
6 years ago
ConsoleCommand.php
6 years ago
Controller.php
6 years ago
ControllerAdmin.php
6 years ago
Dependency.php
6 years ago
LogTablesProvider.php
6 years ago
Manager.php
6 years ago
Menu.php
6 years ago
MetadataLoader.php
6 years ago
Metric.php
6 years ago
PluginException.php
6 years ago
ProcessedMetric.php
6 years ago
ReleaseChannels.php
6 years ago
Report.php
6 years ago
ReportsProvider.php
6 years ago
RequestProcessors.php
6 years ago
Segment.php
6 years ago
SettingsProvider.php
6 years ago
Tasks.php
6 years ago
ThemeStyles.php
6 years ago
ViewDataTable.php
6 years ago
Visualization.php
6 years ago
WidgetsProvider.php
6 years ago
API.php
109 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\Plugin; |
| 11 | |
| 12 | use Piwik\Container\StaticContainer; |
| 13 | use Psr\Log\LoggerInterface; |
| 14 | |
| 15 | /** |
| 16 | * The base class of all API singletons. |
| 17 | * |
| 18 | * Plugins that want to expose functionality through the Reporting API should create a class |
| 19 | * that extends this one. Every public method in that class that is not annotated with **@ignore** |
| 20 | * will be callable through Matomo's Web API. |
| 21 | * |
| 22 | * _Note: If your plugin calculates and stores reports, they should be made available through the API._ |
| 23 | * |
| 24 | * ### Examples |
| 25 | * |
| 26 | * **Defining an API for a plugin** |
| 27 | * |
| 28 | * class API extends \Piwik\Plugin\API |
| 29 | * { |
| 30 | * public function myMethod($idSite, $period, $date, $segment = false) |
| 31 | * { |
| 32 | * $dataTable = // ... get some data ... |
| 33 | * return $dataTable; |
| 34 | * } |
| 35 | * } |
| 36 | * |
| 37 | * **Linking to an API method** |
| 38 | * |
| 39 | * <a href="?module=API&method=MyPlugin.myMethod&idSite=1&period=day&date=2013-10-23">Link</a> |
| 40 | * |
| 41 | * @api |
| 42 | */ |
| 43 | abstract class API |
| 44 | { |
| 45 | private static $instances; |
| 46 | |
| 47 | /** |
| 48 | * Returns the singleton instance for the derived class. If the singleton instance |
| 49 | * has not been created, this method will create it. |
| 50 | * |
| 51 | * @return static |
| 52 | */ |
| 53 | public static function getInstance() |
| 54 | { |
| 55 | $class = get_called_class(); |
| 56 | |
| 57 | if (!isset(self::$instances[$class])) { |
| 58 | $container = StaticContainer::getContainer(); |
| 59 | |
| 60 | $refl = new \ReflectionClass($class); |
| 61 | |
| 62 | if (!$refl->getConstructor() || $refl->getConstructor()->isPublic()) { |
| 63 | self::$instances[$class] = $container->get($class); |
| 64 | } else { |
| 65 | /** @var LoggerInterface $logger */ |
| 66 | $logger = $container->get('Psr\Log\LoggerInterface'); |
| 67 | |
| 68 | // BC with API defining a protected constructor |
| 69 | $logger->notice('The API class {class} defines a protected constructor which is deprecated, make the constructor public instead', array('class' => $class)); |
| 70 | self::$instances[$class] = new $class; |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | return self::$instances[$class]; |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Used in tests only |
| 79 | * @ignore |
| 80 | * @deprecated |
| 81 | */ |
| 82 | public static function unsetInstance() |
| 83 | { |
| 84 | $class = get_called_class(); |
| 85 | unset(self::$instances[$class]); |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Used in tests only |
| 90 | * @ignore |
| 91 | * @deprecated |
| 92 | */ |
| 93 | public static function unsetAllInstances() |
| 94 | { |
| 95 | self::$instances = array(); |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Sets the singleton instance. For testing purposes. |
| 100 | * @ignore |
| 101 | * @deprecated |
| 102 | */ |
| 103 | public static function setSingletonInstance($instance) |
| 104 | { |
| 105 | $class = get_called_class(); |
| 106 | self::$instances[$class] = $instance; |
| 107 | } |
| 108 | } |
| 109 |