Skip to content

Commit

Permalink
Merge pull request #7124 from piwik/removed-registry
Browse files Browse the repository at this point in the history
Removed Piwik\Registry and replaced its usage with the container
  • Loading branch information
Matthieu Aubry committed Feb 9, 2015
2 parents 6a8228c + 5723e93 commit e954f84
Show file tree
Hide file tree
Showing 15 changed files with 55 additions and 82 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ This is a changelog for Piwik platform developers. All changes for our HTTP API'
* `Piwik\ScheduledTask`: use `Piwik\Scheduler\Task` instead
* The API method `UserSettings.getLanguage` is deprecated and will be removed from May 1st 2015. Use `UserLanguage.getLanguage` instead
* The API method `UserSettings.getLanguageCode` is deprecated and will be removed from May 1st 2015. Use `UserLanguage.getLanguageCode` instead

* The `Piwik\Registry` class has been removed in favor of using the container:
* `Registry::get('auth')` should be replaced with `StaticContainer::get('Piwik\Auth')`
* `Registry::set('auth', $auth)` should be replaced with `StaticContainer::getContainer()->set('Piwik\Auth', $auth)`

### New features
* You can now generate UI / screenshot tests using the command `generate:test`
* During UI tests we do now add a CSS class to the HTML element called `uiTest`. This allows you do hide content when screenshots are captured.
Expand Down
2 changes: 1 addition & 1 deletion core/API/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ public static function reloadAuthUsingTokenAuth($request = null)
* query parameter is found in the request.
*
* Plugins that provide authentication capabilities should subscribe to this event
* and make sure the global authentication object (the object returned by `Registry::get('auth')`)
* and make sure the global authentication object (the object returned by `StaticContainer::get('Piwik\Auth')`)
* is setup to use `$token_auth` when its `authenticate()` method is executed.
*
* @param string $token_auth The value of the **token_auth** query parameter.
Expand Down
6 changes: 3 additions & 3 deletions core/Auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*
* Plugins that provide Auth implementations must provide a class that implements
* this interface. Additionally, an instance of that class must be set in the
* {@link \Piwik\Registry} class with the 'auth' key during the
* container with the 'Piwik\Auth' key during the
* [Request.initAuthenticationObject](http://developer.piwik.org/api-reference/events#requestinitauthenticationobject)
* event.
*
Expand All @@ -34,13 +34,13 @@
* **How an Auth implementation will be used**
*
* // authenticating by password
* $auth = \Piwik\Registry::get('auth');
* $auth = StaticContainer::get('Piwik\Auth');
* $auth->setLogin('user');
* $auth->setPassword('password');
* $result = $auth->authenticate();
*
* // authenticating by token auth
* $auth = \Piwik\Registry::get('auth');
* $auth = StaticContainer::get('Piwik\Auth');
* $auth->setLogin('user');
* $auth->setTokenAuth('...');
* $result = $auth->authenticate();
Expand Down
8 changes: 4 additions & 4 deletions core/FrontController.php
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ public static function triggerSafeModeWhenError()
}

/**
* Loads the config file and assign to the global registry
* Loads the config file
* This is overridden in tests to ensure test config file is used
*
* @return Exception
Expand Down Expand Up @@ -415,14 +415,14 @@ public function init()
* **Example**
*
* Piwik::addAction('Request.initAuthenticationObject', function() {
* Piwik\Registry::set('auth', new MyAuthImplementation());
* StaticContainer::getContainer()->set('Piwik\Auth', new MyAuthImplementation());
* });
*/
Piwik::postEvent('Request.initAuthenticationObject');
try {
$authAdapter = Registry::get('auth');
$authAdapter = StaticContainer::get('Piwik\Auth');
} catch (Exception $e) {
$message = "Authentication object cannot be found in the Registry. Maybe the Login plugin is not activated?
$message = "Authentication object cannot be found in the container. Maybe the Login plugin is not activated?
<br />You can activate the plugin by adding:<br />
<code>Plugins[] = Login</code><br />
under the <code>[Plugins]</code> section in your config/config.ini.php";
Expand Down
2 changes: 1 addition & 1 deletion core/Piwik.php
Original file line number Diff line number Diff line change
Expand Up @@ -489,7 +489,7 @@ public static function checkUserHasSomeViewAccess()
*/
public static function getLoginPluginName()
{
return Registry::get('auth')->getName();
return StaticContainer::get('Piwik\Auth')->getName();
}

/**
Expand Down
4 changes: 2 additions & 2 deletions core/Plugin/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Piwik\Common;
use Piwik\Config as PiwikConfig;
use Piwik\Config;
use Piwik\Container\StaticContainer;
use Piwik\DataTable\Filter\CalculateEvolutionFilter;
use Piwik\Date;
use Piwik\Exception\NoPrivilegesException;
Expand All @@ -31,7 +32,6 @@
use Piwik\Plugins\CoreAdminHome\CustomLogo;
use Piwik\Plugins\CoreVisualizations\Visualizations\JqplotGraph\Evolution;
use Piwik\Plugins\LanguagesManager\LanguagesManager;
use Piwik\Registry;
use Piwik\SettingsPiwik;
use Piwik\Site;
use Piwik\Url;
Expand Down Expand Up @@ -876,7 +876,7 @@ public function redirectToIndex($moduleToRedirect, $actionToRedirect, $websiteId
$currentLogin = Piwik::getCurrentUserLogin();
$emails = implode(',', Piwik::getAllSuperUserAccessEmailAddresses());
$errorMessage = sprintf(Piwik::translate('CoreHome_NoPrivilegesAskPiwikAdmin'), $currentLogin, "<br/><a href='mailto:" . $emails . "?subject=Access to Piwik for user $currentLogin'>", "</a>");
$errorMessage .= "<br /><br />&nbsp;&nbsp;&nbsp;<b><a href='index.php?module=" . Registry::get('auth')->getName() . "&amp;action=logout'>&rsaquo; " . Piwik::translate('General_Logout') . "</a></b><br />";
$errorMessage .= "<br /><br />&nbsp;&nbsp;&nbsp;<b><a href='index.php?module=" . StaticContainer::get('Piwik\Auth')->getName() . "&amp;action=logout'>&rsaquo; " . Piwik::translate('General_Logout') . "</a></b><br />";

$ex = new NoPrivilegesException($errorMessage);
$ex->setIsHtmlMessage();
Expand Down
31 changes: 18 additions & 13 deletions core/Registry.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,21 @@
*
* @link http://piwik.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*
*/

namespace Piwik;

use Piwik\Container\StaticContainer;

/**
* Registry class.
*
* @method static Registry getInstance()
* @api
* @deprecated This class will be removed, use the container instead.
*/
class Registry extends Singleton
{
private $data;

protected function __construct()
{
$this->data = array();
}

public static function isRegistered($key)
{
return self::getInstance()->hasKey($key);
Expand All @@ -40,19 +36,28 @@ public static function set($key, $value)

public function setKey($key, $value)
{
$this->data[$key] = $value;
if ($key === 'auth') {
$key = 'Piwik\Auth';
}

StaticContainer::getContainer()->set($key, $value);
}

public function getKey($key)
{
if (!$this->hasKey($key)) {
throw new \Exception(sprintf("Key '%s' doesn't exist in Registry", $key));
if ($key === 'auth') {
$key = 'Piwik\Auth';
}
return $this->data[$key];

return StaticContainer::get($key);
}

public function hasKey($key)
{
return array_key_exists($key, $this->data);
if ($key === 'auth') {
$key = 'Piwik\Auth';
}

return StaticContainer::getContainer()->has($key);
}
}
4 changes: 2 additions & 2 deletions core/Tracker/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@
use Exception;
use Piwik\Common;
use Piwik\Config;
use Piwik\Container\StaticContainer;
use Piwik\Cookie;
use Piwik\Exception\InvalidRequestParameterException;
use Piwik\Exception\UnexpectedWebsiteFoundException;
use Piwik\IP;
use Piwik\Network\IPUtils;
use Piwik\Piwik;
use Piwik\Plugins\CustomVariables\CustomVariables;
use Piwik\Registry;
use Piwik\Tracker;

/**
Expand Down Expand Up @@ -138,7 +138,7 @@ public static function authenticateSuperUserOrAdmin($tokenAuth, $idSite)
Piwik::postEvent('Request.initAuthenticationObject');

/** @var \Piwik\Auth $auth */
$auth = Registry::get('auth');
$auth = StaticContainer::get('Piwik\Auth');
$auth->setTokenAuth($tokenAuth);
$auth->setLogin(null);
$access = $auth->authenticate();
Expand Down
3 changes: 2 additions & 1 deletion plugins/Login/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Piwik\Auth as AuthInterface;
use Piwik\Common;
use Piwik\Config;
use Piwik\Container\StaticContainer;
use Piwik\Cookie;
use Piwik\Log;
use Piwik\Nonce;
Expand Down Expand Up @@ -60,7 +61,7 @@ public function __construct($passwordResetter = null, $auth = null, $sessionInit
$this->passwordResetter = $passwordResetter;

if (empty($auth)) {
$auth = \Piwik\Registry::get('auth');
$auth = StaticContainer::get('Piwik\Auth');
}
$this->auth = $auth;

Expand Down
9 changes: 6 additions & 3 deletions plugins/Login/Login.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

use Exception;
use Piwik\Config;
use Piwik\Container\StaticContainer;
use Piwik\Cookie;
use Piwik\FrontController;
use Piwik\Option;
Expand Down Expand Up @@ -65,8 +66,10 @@ public function noAccess(Exception $exception)
*/
public function ApiRequestAuthenticate($tokenAuth)
{
\Piwik\Registry::get('auth')->setLogin($login = null);
\Piwik\Registry::get('auth')->setTokenAuth($tokenAuth);
/** @var \Piwik\Auth $auth */
$auth = StaticContainer::get('Piwik\Auth');
$auth->setLogin($login = null);
$auth->setTokenAuth($tokenAuth);
}

protected static function isModuleIsAPI()
Expand All @@ -82,7 +85,7 @@ protected static function isModuleIsAPI()
function initAuthenticationObject($activateCookieAuth = false)
{
$auth = new Auth();
\Piwik\Registry::set('auth', $auth);
StaticContainer::getContainer()->set('Piwik\Auth', $auth);

$this->initAuthenticationFromCookie($auth, $activateCookieAuth);
}
Expand Down
11 changes: 6 additions & 5 deletions plugins/Overlay/API.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Exception;
use Piwik\Access;
use Piwik\Config;
use Piwik\Container\StaticContainer;
use Piwik\DataTable;
use Piwik\Piwik;
use Piwik\Plugins\SitesManager\API as APISitesManager;
Expand Down Expand Up @@ -107,24 +108,24 @@ private function authenticate($idSite)
* Triggered immediately before the user is authenticated.
*
* This event can be used by plugins that provide their own authentication mechanism
* to make that mechanism available. Subscribers should set the `'auth'` object in
* the {@link Piwik\Registry} to an object that implements the {@link Piwik\Auth} interface.
* to make that mechanism available. Subscribers should set the `'Piwik\Auth'` object in
* the container to an object that implements the {@link Piwik\Auth} interface.
*
* **Example**
*
* use Piwik\Registry;
* use Piwik\Container\StaticContainer;
*
* public function initAuthenticationObject($activateCookieAuth)
* {
* Registry::set('auth', new LDAPAuth($activateCookieAuth));
* StaticContainer::getContainer()->set('Piwik\Auth', new LDAPAuth($activateCookieAuth));
* }
*
* @param bool $activateCookieAuth Whether authentication based on `$_COOKIE` values should
* be allowed.
*/
Piwik::postEvent('Request.initAuthenticationObject', array($activateCookieAuth = true));

$auth = \Piwik\Registry::get('auth');
$auth = StaticContainer::get('Piwik\Auth');
$success = Access::getInstance()->reloadAccess($auth);

if (!$success) {
Expand Down
7 changes: 4 additions & 3 deletions plugins/UsersManager/API.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Piwik\Access;
use Piwik\Common;
use Piwik\Config;
use Piwik\Container\StaticContainer;
use Piwik\Date;
use Piwik\Option;
use Piwik\Piwik;
Expand Down Expand Up @@ -54,15 +55,15 @@ protected function __construct()
* Example of how you would overwrite the UsersManager_API with your own class:
* Call the following in your plugin __construct() for example:
*
* Registry::set('UsersManager_API', \Piwik\Plugins\MyCustomUsersManager\API::getInstance());
* StaticContainer::getContainer()->set('UsersManager_API', \Piwik\Plugins\MyCustomUsersManager\API::getInstance());
*
* @throws Exception
* @return \Piwik\Plugins\UsersManager\API
*/
public static function getInstance()
{
try {
$instance = \Piwik\Registry::get('UsersManager_API');
$instance = StaticContainer::get('UsersManager_API');
if (!($instance instanceof API)) {
// Exception is caught below and corrected
throw new Exception('UsersManager_API must inherit API');
Expand All @@ -71,7 +72,7 @@ public static function getInstance()

} catch (Exception $e) {
self::$instance = new self;
\Piwik\Registry::set('UsersManager_API', self::$instance);
StaticContainer::getContainer()->set('UsersManager_API', self::$instance);
}

return self::$instance;
Expand Down
3 changes: 2 additions & 1 deletion plugins/UsersManager/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Exception;
use Piwik\API\ResponseBuilder;
use Piwik\Common;
use Piwik\Container\StaticContainer;
use Piwik\Metrics\Formatter;
use Piwik\Piwik;
use Piwik\Plugin\ControllerAdmin;
Expand Down Expand Up @@ -425,7 +426,7 @@ private function processPasswordChange($userLogin)
// logs the user in with the new password
if ($newPassword !== false) {
$sessionInitializer = new SessionInitializer();
$auth = \Piwik\Registry::get('auth');
$auth = StaticContainer::get('Piwik\Auth');
$auth->setLogin($userLogin);
$auth->setPassword($password);
$sessionInitializer->initSession($auth, $rememberMe = false);
Expand Down
2 changes: 0 additions & 2 deletions tests/PHPUnit/Framework/Fixture.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
use Piwik\Plugins\UserCountry\LocationProvider;
use Piwik\Plugins\UsersManager\API as APIUsersManager;
use Piwik\Plugins\UsersManager\UsersManager;
use Piwik\Registry;
use Piwik\ReportRenderer;
use Piwik\SettingsPiwik;
use Piwik\SettingsServer;
Expand Down Expand Up @@ -307,7 +306,6 @@ public function clearInMemoryCaches()
Config::getInstance()->clear();
ArchiveTableCreator::clear();
\Piwik\Plugins\ScheduledReports\API::$cache = array();
Registry::unsetInstance();
EventDispatcher::getInstance()->clearAllObservers();

$_GET = $_REQUEST = array();
Expand Down
40 changes: 0 additions & 40 deletions tests/PHPUnit/Unit/RegistryTest.php

This file was deleted.

0 comments on commit e954f84

Please sign in to comment.