diff options
author | diosmosis <benaka@piwik.pro> | 2015-03-17 06:18:24 +0300 |
---|---|---|
committer | diosmosis <benaka@piwik.pro> | 2015-03-17 06:18:24 +0300 |
commit | 82a5a9fdce43b2db6313893ff13d70e90b1a7a41 (patch) | |
tree | 719e46f2f91d7344fb507110998d7fe7fe5453ab | |
parent | 25d1af263cc08a7fa1ac1ee6f12cd6cc57802a38 (diff) | |
parent | f76ec2495b19c8b33b8f74f6e3e7e8d9be45324a (diff) |
Merge branch 'master' into 7276_update_command_progress
Conflicts:
CHANGELOG.md
-rw-r--r-- | CHANGELOG.md | 4 | ||||
-rw-r--r-- | core/Plugin/API.php | 58 | ||||
-rw-r--r-- | plugins/DBStats/API.php | 13 | ||||
-rw-r--r-- | plugins/Dashboard/API.php | 4 | ||||
-rw-r--r-- | plugins/Insights/API.php | 6 | ||||
-rw-r--r-- | plugins/UsersManager/API.php | 6 |
6 files changed, 68 insertions, 23 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index 4e81aab57d..ba91000bc5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -26,8 +26,12 @@ This is a changelog for Piwik platform developers. All changes for our HTTP API' * `getHostByAddr()` ### Deprecations +* `API` classes should no longer have a protected constructor. Classes with a protected constructor will generate a notice in the logs and should expose a public constructor instead. * Update classes should not declare static `getSql()` and `update()` methods anymore. It is still supported to use those, but developers should instead override the `Updates::getMigrationQueries()` and `Updates::doUpdate()` instance methods. +### New features +* `API` classes can now use dependency injection in their constructor to inject other instances. + ### New commands * There is now a command `core:purge-old-archive-data` that can be used to manually purge temporary, error-ed and invalidated archives from one or more archive tables. diff --git a/core/Plugin/API.php b/core/Plugin/API.php index 4e5095a3aa..40bd334f95 100644 --- a/core/Plugin/API.php +++ b/core/Plugin/API.php @@ -9,7 +9,8 @@ namespace Piwik\Plugin; -use Piwik\Singleton; +use Piwik\Container\StaticContainer; +use Psr\Log\LoggerInterface; /** * The base class of all API singletons. @@ -39,7 +40,58 @@ use Piwik\Singleton; * * @api */ -abstract class API extends Singleton +abstract class API { - + private static $instances; + + /** + * Returns the singleton instance for the derived class. If the singleton instance + * has not been created, this method will create it. + * + * @return static + */ + public static function getInstance() { + $class = get_called_class(); + + if (!isset(self::$instances[$class])) { + $container = StaticContainer::getContainer(); + + $refl = new \ReflectionClass($class); + + if (!$refl->getConstructor() || $refl->getConstructor()->isPublic()) { + self::$instances[$class] = $container->get($class); + } else { + /** @var LoggerInterface $logger */ + $logger = $container->get('Psr\Log\LoggerInterface'); + + // BC with API defining a protected constructor + $logger->notice('The API class {class} defines a protected constructor which is deprecated, make the constructor public instead', array('class' => $class)); + self::$instances[$class] = new $class; + } + } + + return self::$instances[$class]; + } + + /** + * Used in tests only + * @ignore + * @deprecated + */ + public static function unsetInstance() + { + $class = get_called_class(); + unset(self::$instances[$class]); + } + + /** + * Sets the singleton instance. For testing purposes. + * @ignore + * @deprecated + */ + public static function setSingletonInstance($instance) + { + $class = get_called_class(); + self::$instances[$class] = $instance; + } } diff --git a/plugins/DBStats/API.php b/plugins/DBStats/API.php index 30def6cf79..47417dbc35 100644 --- a/plugins/DBStats/API.php +++ b/plugins/DBStats/API.php @@ -13,12 +13,6 @@ use Piwik\DataTable; use Piwik\Piwik; /** - * - * @see plugins/DBStats/MySQLMetadataProvider.php - */ -require_once PIWIK_INCLUDE_PATH . '/plugins/DBStats/MySQLMetadataProvider.php'; - -/** * DBStats API is used to request the overall status of the Mysql tables in use by Piwik. * @hideExceptForSuperUser * @method static \Piwik\Plugins\DBStats\API getInstance() @@ -30,12 +24,9 @@ class API extends \Piwik\Plugin\API */ private $metadataProvider; - /** - * Constructor. - */ - protected function __construct() + public function __construct(MySQLMetadataProvider $metadataProvider) { - $this->metadataProvider = new MySQLMetadataProvider(); + $this->metadataProvider = $metadataProvider; } /** diff --git a/plugins/Dashboard/API.php b/plugins/Dashboard/API.php index d8994c229e..f4637e862a 100644 --- a/plugins/Dashboard/API.php +++ b/plugins/Dashboard/API.php @@ -19,9 +19,9 @@ class API extends \Piwik\Plugin\API { private $dashboard = null; - protected function __construct() + public function __construct(Dashboard $dashboard) { - $this->dashboard = new Dashboard(); + $this->dashboard = $dashboard; } /** diff --git a/plugins/Insights/API.php b/plugins/Insights/API.php index 20cb649649..4b3228ffc2 100644 --- a/plugins/Insights/API.php +++ b/plugins/Insights/API.php @@ -39,11 +39,9 @@ class API extends \Piwik\Plugin\API */ private $model; - protected function __construct() + public function __construct(Model $model) { - parent::__construct(); - - $this->model = new Model(); + $this->model = $model; } private function getOverviewReports() diff --git a/plugins/UsersManager/API.php b/plugins/UsersManager/API.php index b1c7c227e3..ccb4dac126 100644 --- a/plugins/UsersManager/API.php +++ b/plugins/UsersManager/API.php @@ -45,9 +45,9 @@ class API extends \Piwik\Plugin\API private static $instance = null; - protected function __construct() + public function __construct(Model $model) { - $this->model = new Model(); + $this->model = $model; } /** @@ -71,7 +71,7 @@ class API extends \Piwik\Plugin\API self::$instance = $instance; } catch (Exception $e) { - self::$instance = new self; + self::$instance = StaticContainer::get('Piwik\Plugins\UsersManager\API'); StaticContainer::getContainer()->set('UsersManager_API', self::$instance); } |