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:
authormattab <matthieu.aubry@gmail.com>2013-10-10 08:52:46 +0400
committermattab <matthieu.aubry@gmail.com>2013-10-11 00:15:03 +0400
commitc782b8ccd740a206bf2b7af393dcade94a38ef7d (patch)
tree1c9d40a922785b2aa279857a1520a6eb6781bdb7 /core
parenta4972f38678207e649a20f1b5ac281a679bf92fa (diff)
Refs #4208 refactor all singletons except the hard ones (with custom getInstance()
(some tests failing)
Diffstat (limited to 'core')
-rw-r--r--core/API/Proxy.php22
-rw-r--r--core/Config.php19
-rw-r--r--core/DataTable/Manager.php18
-rw-r--r--core/Db/Schema.php21
-rw-r--r--core/EventDispatcher.php18
-rw-r--r--core/FrontController.php17
-rw-r--r--core/Log.php33
-rw-r--r--core/Menu/MenuAbstract.php3
-rw-r--r--core/Menu/MenuAdmin.php13
-rw-r--r--core/Menu/MenuMain.php13
-rw-r--r--core/Menu/MenuTop.php14
-rw-r--r--core/Plugin/API.php18
-rw-r--r--core/Plugin/Controller.php11
-rw-r--r--core/Plugin/Manager.php18
-rw-r--r--core/Registry.php18
-rw-r--r--core/Singleton.php37
16 files changed, 78 insertions, 215 deletions
diff --git a/core/API/Proxy.php b/core/API/Proxy.php
index 43d07bc25f..f504bdeb8f 100644
--- a/core/API/Proxy.php
+++ b/core/API/Proxy.php
@@ -14,6 +14,7 @@ namespace Piwik\API;
use Exception;
use Piwik\Common;
use Piwik\Piwik;
+use Piwik\Singleton;
use ReflectionClass;
use ReflectionMethod;
@@ -28,7 +29,7 @@ use ReflectionMethod;
* @package Piwik
* @subpackage Piwik_API
*/
-class Proxy
+class Proxy extends Singleton
{
// array of already registered plugins names
protected $alreadyRegistered = array();
@@ -40,12 +41,6 @@ class Proxy
private $noDefaultValue;
/**
- * Singleton instance
- * @var \Piwik\API\Proxy|null
- */
- static private $instance = null;
-
- /**
* protected constructor
*/
protected function __construct()
@@ -54,19 +49,6 @@ class Proxy
}
/**
- * Singleton, returns instance
- *
- * @return \Piwik\API\Proxy
- */
- static public function getInstance()
- {
- if (self::$instance == null) {
- self::$instance = new self;
- }
- return self::$instance;
- }
-
- /**
* Returns array containing reflection meta data for all the loaded classes
* eg. number of parameters, method names, etc.
*
diff --git a/core/Config.php b/core/Config.php
index 1ed8ddee47..c7a5ad9d27 100644
--- a/core/Config.php
+++ b/core/Config.php
@@ -42,23 +42,8 @@ use Exception;
* @package Piwik
* @subpackage Piwik_Config
*/
-class Config
+class Config extends Singleton
{
- private static $instance = null;
-
- /**
- * Returns the singleton Piwik_Config
- *
- * @return \Piwik\Config
- * @api
- */
- public static function getInstance()
- {
- if (self::$instance == null) {
- self::$instance = new self;
- }
- return self::$instance;
- }
/**
* Contains configuration files values
@@ -298,7 +283,7 @@ class Config
// must be called here, not in init(), since setTestEnvironment() calls init(). (this avoids
// infinite recursion)
- Piwik::postTestEvent('Config.createConfigSingleton', array(self::$instance));
+ Piwik::postTestEvent('Config.createConfigSingleton', array( $this->getInstance() ));
}
// check cache for merged section
diff --git a/core/DataTable/Manager.php b/core/DataTable/Manager.php
index b32463b176..0ae03ede89 100644
--- a/core/DataTable/Manager.php
+++ b/core/DataTable/Manager.php
@@ -14,6 +14,7 @@ namespace Piwik\DataTable;
use Exception;
use Piwik\Common;
use Piwik\DataTable;
+use Piwik\Singleton;
/**
* The DataTable_Manager registers all the instanciated DataTable and provides an
@@ -23,23 +24,8 @@ use Piwik\DataTable;
* @package Piwik
* @subpackage DataTable
*/
-class Manager
+class Manager extends Singleton
{
- static private $instance = null;
-
- /**
- * Returns instance
- *
- * @return \Piwik\DataTable\Manager
- */
- static public function getInstance()
- {
- if (self::$instance == null) {
- self::$instance = new self;
- }
- return self::$instance;
- }
-
/**
* Array used to store the DataTable
*
diff --git a/core/Db/Schema.php b/core/Db/Schema.php
index 015899928c..5ce1d55d62 100644
--- a/core/Db/Schema.php
+++ b/core/Db/Schema.php
@@ -11,6 +11,7 @@
namespace Piwik\Db;
use Piwik\Config;
+use Piwik\Singleton;
/**
* Schema abstraction
@@ -20,14 +21,8 @@ use Piwik\Config;
* @package Piwik
* @subpackage Piwik_Db
*/
-class Schema
+class Schema extends Singleton
{
- /**
- * Singleton instance
- *
- * @var \Piwik\Db\Schema
- */
- static private $instance = null;
/**
* Type of database schema
@@ -36,18 +31,6 @@ class Schema
*/
private $schema = null;
- /**
- * Returns the singleton Schema
- *
- * @return \Piwik\Db\Schema
- */
- static public function getInstance()
- {
- if (self::$instance === null) {
- self::$instance = new self;
- }
- return self::$instance;
- }
/**
* Get schema class name
diff --git a/core/EventDispatcher.php b/core/EventDispatcher.php
index 66cef418ef..c57cea990e 100644
--- a/core/EventDispatcher.php
+++ b/core/EventDispatcher.php
@@ -17,7 +17,7 @@ use Piwik\Plugin;
* This class allows code to post events from anywhere in Piwik and for
* plugins to associate callbacks to be executed when events are posted.
*/
-class EventDispatcher
+class EventDispatcher extends Singleton
{
// implementation details for postEvent
const EVENT_CALLBACK_GROUP_FIRST = 0;
@@ -25,22 +25,6 @@ class EventDispatcher
const EVENT_CALLBACK_GROUP_THIRD = 2;
/**
- * Singleton instance.
- */
- private static $instance = null;
-
- /**
- * Returns the singleton EventDispatcher instance. Creates it if necessary.
- */
- public static function getInstance()
- {
- if (self::$instance === null) {
- self::$instance = new EventDispatcher();
- }
- return self::$instance;
- }
-
- /**
* Array of observers (callbacks attached to events) that are not methods
* of plugin classes.
*
diff --git a/core/FrontController.php b/core/FrontController.php
index f93a607ee9..8b42c87a98 100644
--- a/core/FrontController.php
+++ b/core/FrontController.php
@@ -27,7 +27,7 @@ use Piwik\Session;
* @package Piwik
* @subpackage FrontController
*/
-class FrontController
+class FrontController extends Singleton
{
/**
* Set to false and the Front Controller will not dispatch the request
@@ -36,8 +36,6 @@ class FrontController
*/
public static $enableDispatch = true;
- private static $instance = null;
-
protected function prepareDispatch($module, $action, $parameters)
{
if (is_null($module)) {
@@ -92,19 +90,6 @@ class FrontController
}
/**
- * returns singleton
- *
- * @return \Piwik\FrontController
- */
- public static function getInstance()
- {
- if (self::$instance == null) {
- self::$instance = new self;
- }
- return self::$instance;
- }
-
- /**
* Dispatches the request to the right plugin and executes the requested action on the plugin controller.
*
* @throws Exception|\Piwik\PluginDeactivatedException in case the plugin doesn't exist, the action doesn't exist, there is not enough permission, etc.
diff --git a/core/Log.php b/core/Log.php
index 026f63c839..23017acd59 100644
--- a/core/Log.php
+++ b/core/Log.php
@@ -26,7 +26,7 @@ use Piwik\Db;
* The logging utility can be configured by manipulating the INI config options in the
* [log] section.
*/
-class Log
+class Log extends Singleton
{
// log levels
const NONE = 0;
@@ -51,35 +51,6 @@ class Log
const GET_AVAILABLE_WRITERS_EVENT = 'Log.getAvailableWriters';
/**
- * The singleton Log instance.
- *
- * @var Log
- */
- private static $instance = null;
-
- /**
- * Returns the singleton Log instance or creates it if it doesn't exist.
- *
- * @return Log
- */
- public static function getInstance()
- {
- if (self::$instance === null) {
- self::$instance = new Log();
- }
- return self::$instance;
- }
-
- /**
- * Unsets the singleton instance so it will be re-created the next time getInstance() is
- * called. For testing purposes only.
- */
- public static function clearInstance()
- {
- self::$instance = null;
- }
-
- /**
* The current logging level. Everything of equal or greater priority will be logged.
* Everything else will be ignored.
*
@@ -120,7 +91,7 @@ class Log
/**
* Constructor.
*/
- private function __construct()
+ protected function __construct()
{
$logConfig = Config::getInstance()->log;
$this->setCurrentLogLevelFromConfig($logConfig);
diff --git a/core/Menu/MenuAbstract.php b/core/Menu/MenuAbstract.php
index edd53161ba..b8f5b11e1a 100644
--- a/core/Menu/MenuAbstract.php
+++ b/core/Menu/MenuAbstract.php
@@ -12,11 +12,12 @@ namespace Piwik\Menu;
use Piwik\Common;
use Piwik\Plugins\SitesManager\API;
+use Piwik\Singleton;
/**
* @package Piwik_Menu
*/
-abstract class MenuAbstract
+abstract class MenuAbstract extends Singleton
{
protected $menu = null;
diff --git a/core/Menu/MenuAdmin.php b/core/Menu/MenuAdmin.php
index 4a29db3e29..eb3576d37e 100644
--- a/core/Menu/MenuAdmin.php
+++ b/core/Menu/MenuAdmin.php
@@ -17,19 +17,6 @@ use Piwik\Piwik;
*/
class MenuAdmin extends MenuAbstract
{
- static private $instance = null;
-
- /**
- * @return MenuAdmin
- */
- static public function getInstance()
- {
- if (self::$instance == null) {
- self::$instance = new self;
- }
- return self::$instance;
- }
-
/**
* Adds a new AdminMenu entry.
*
diff --git a/core/Menu/MenuMain.php b/core/Menu/MenuMain.php
index 2485f18294..4146559fcd 100644
--- a/core/Menu/MenuMain.php
+++ b/core/Menu/MenuMain.php
@@ -17,19 +17,6 @@ use Piwik\Piwik;
*/
class MenuMain extends MenuAbstract
{
- static private $instance = null;
-
- /**
- * @return MenuAbstract
- */
- static public function getInstance()
- {
- if (self::$instance == null) {
- self::$instance = new self;
- }
- return self::$instance;
- }
-
/**
* Returns if the URL was found in the menu.
*
diff --git a/core/Menu/MenuTop.php b/core/Menu/MenuTop.php
index 29f056367a..ab2d312799 100644
--- a/core/Menu/MenuTop.php
+++ b/core/Menu/MenuTop.php
@@ -17,20 +17,6 @@ use Piwik\Piwik;
*/
class MenuTop extends MenuAbstract
{
- static private $instance = null;
-
- /**
- * @return MenuTop
- */
- static public function getInstance()
- {
- if (self::$instance == null) {
- self::$instance = new self;
- }
- return self::$instance;
- }
-
-
/**
* Adds a new entry to the TopMenu.
*
diff --git a/core/Plugin/API.php b/core/Plugin/API.php
new file mode 100644
index 0000000000..e10baf7888
--- /dev/null
+++ b/core/Plugin/API.php
@@ -0,0 +1,18 @@
+<?php
+/**
+ * Piwik - Open source web analytics
+ *
+ * @link http://piwik.org
+ * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
+ *
+ * @category Piwik
+ * @package Piwik_PluginArchiver
+ */
+
+namespace Piwik\Plugin;
+
+use Piwik\Singleton;
+
+abstract class API extends Singleton
+{
+}
diff --git a/core/Plugin/Controller.php b/core/Plugin/Controller.php
index f54c068d0d..99d5a9668d 100644
--- a/core/Plugin/Controller.php
+++ b/core/Plugin/Controller.php
@@ -24,7 +24,6 @@ use Piwik\Period\Month;
use Piwik\Period;
use Piwik\Period\Range;
use Piwik\Piwik;
-use Piwik\Plugins\API\API;
use Piwik\Plugins\LanguagesManager\LanguagesManager;
use Piwik\Plugins\SitesManager\API as APISitesManager;
use Piwik\Plugins\UsersManager\API as APIUsersManager;
@@ -207,7 +206,7 @@ abstract class Controller
$idSite = Common::getRequestVar('idSite');
$period = Common::getRequestVar('period');
$date = Common::getRequestVar('date');
- $meta = API::getInstance()->getReportMetadata($idSite, $period, $date);
+ $meta = \Piwik\Plugins\API\API::getInstance()->getReportMetadata($idSite, $period, $date);
$columns = array_merge($columnsToDisplay, $selectableColumns);
$translations = array_combine($columns, $columns);
@@ -452,10 +451,10 @@ abstract class Controller
$view->isSuperUser = Access::getInstance()->isSuperUser();
$view->hasSomeAdminAccess = Piwik::isUserHasSomeAdminAccess();
$view->isCustomLogo = Config::getInstance()->branding['use_custom_logo'];
- $view->logoHeader = API::getInstance()->getHeaderLogoUrl();
- $view->logoLarge = API::getInstance()->getLogoUrl();
- $view->logoSVG = API::getInstance()->getSVGLogoUrl();
- $view->hasSVGLogo = API::getInstance()->hasSVGLogo();
+ $view->logoHeader = \Piwik\Plugins\API\API::getInstance()->getHeaderLogoUrl();
+ $view->logoLarge = \Piwik\Plugins\API\API::getInstance()->getLogoUrl();
+ $view->logoSVG = \Piwik\Plugins\API\API::getInstance()->getSVGLogoUrl();
+ $view->hasSVGLogo = \Piwik\Plugins\API\API::getInstance()->hasSVGLogo();
$view->enableFrames = Config::getInstance()->General['enable_framed_pages']
|| @Config::getInstance()->General['enable_framed_logins'];
diff --git a/core/Plugin/Manager.php b/core/Plugin/Manager.php
index d7add95fdb..7dbb4f86c4 100644
--- a/core/Plugin/Manager.php
+++ b/core/Plugin/Manager.php
@@ -16,6 +16,7 @@ use Piwik\EventDispatcher;
use Piwik\Filesystem;
use Piwik\Option;
use Piwik\Plugin;
+use Piwik\Singleton;
use Piwik\Translate;
use Piwik\Updater;
@@ -27,7 +28,7 @@ require_once PIWIK_INCLUDE_PATH . '/core/EventDispatcher.php';
* @package Piwik
* @subpackage Manager
*/
-class Manager
+class Manager extends Singleton
{
protected $pluginsToLoad = array();
@@ -69,21 +70,6 @@ class Manager
// If a plugin hooks onto at least an event starting with "Tracker.", we load the plugin during tracker
const TRACKER_EVENT_PREFIX = 'Tracker.';
- static private $instance = null;
-
- /**
- * Returns the singleton Manager
- *
- * @return Manager
- */
- static public function getInstance()
- {
- if (self::$instance == null) {
- self::$instance = new self;
- }
- return self::$instance;
- }
-
/**
* Update Plugins config
*
diff --git a/core/Registry.php b/core/Registry.php
index 5fe90370b9..88ecc1ce95 100644
--- a/core/Registry.php
+++ b/core/Registry.php
@@ -15,24 +15,15 @@ namespace Piwik;
*
* @package Piwik
*/
-class Registry
+class Registry extends Singleton
{
- private static $instance;
private $data;
- private function __construct()
+ protected function __construct()
{
$this->data = array();
}
- public static function getInstance()
- {
- if (self::$instance == null) {
- self::$instance = new Registry();
- }
- return self::$instance;
- }
-
public static function isRegistered($key)
{
return self::getInstance()->hasKey($key);
@@ -48,11 +39,6 @@ class Registry
self::getInstance()->setKey($key, $value);
}
- public static function unsetInstance()
- {
- self::$instance = null;
- }
-
public function setKey($key, $value)
{
$this->data[$key] = $value;
diff --git a/core/Singleton.php b/core/Singleton.php
new file mode 100644
index 0000000000..7e7679c67a
--- /dev/null
+++ b/core/Singleton.php
@@ -0,0 +1,37 @@
+<?php
+/**
+ * Piwik - Open source web analytics
+ *
+ * @link http://piwik.org
+ * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
+ *
+ * @category Piwik
+ * @package Piwik
+ */
+
+namespace Piwik;
+
+class Singleton
+{
+
+ protected static $instances;
+
+ protected function __construct() { }
+
+ final private function __clone() { }
+
+ public static function getInstance() {
+ $class = get_called_class();
+
+ if (!isset(self::$instances[$class])) {
+ self::$instances[$class] = new $class;
+ }
+ return self::$instances[$class];
+ }
+
+ public static function unsetInstance()
+ {
+ $class = get_called_class();
+ unset(self::$instances[$class]);
+ }
+}