Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
path: root/core
diff options
context:
space:
mode:
authorMatthieu Napoli <matthieu@mnapoli.fr>2015-03-10 05:34:18 +0300
committerMatthieu Napoli <matthieu@mnapoli.fr>2015-03-16 04:58:22 +0300
commitbbae3aecca789e3bde2878f05e8d82ad5d064fa6 (patch)
treeb4133ba09e7bba3e9d6093c88a5027195a2bf336 /core
parentca2f0d3047cee0e084bf02d1e6bae4761eb7c3c0 (diff)
Dependency injection in API classes
Diffstat (limited to 'core')
-rw-r--r--core/Plugin/API.php56
1 files changed, 53 insertions, 3 deletions
diff --git a/core/Plugin/API.php b/core/Plugin/API.php
index 4e5095a3aa..162c9e02f2 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,56 @@ 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
+ */
+ public static function unsetInstance()
+ {
+ $class = get_called_class();
+ unset(self::$instances[$class]);
+ }
+
+ /**
+ * Sets the singleton instance. For testing purposes.
+ * @ignore
+ */
+ public static function setSingletonInstance($instance)
+ {
+ $class = get_called_class();
+ self::$instances[$class] = $instance;
+ }
}