From d35273b3c77c19db9a1ad754d88ba4fb95ea239b Mon Sep 17 00:00:00 2001 From: diosmosis <benaka@piwik.pro> Date: Thu, 30 Apr 2015 20:03:58 -0700 Subject: [PATCH] Remove GlobalSettingsProvider interface and replace w/ GlobalSettingsProvider class, the class is the old IniSettingsProvider class. --- core/Application/Environment.php | 3 +- .../Kernel/EnvironmentValidator.php | 12 +- .../Kernel/GlobalSettingsProvider.php | 114 +++++++++++++++- .../IniSettingsProvider.php | 125 ------------------ .../Kernel/PluginList/IniPluginList.php | 5 +- core/Config.php | 5 +- tests/PHPUnit/Framework/Fixture.php | 6 +- tests/PHPUnit/Framework/Mock/TestConfig.php | 2 +- .../Framework/TestCase/UnitTestCase.php | 6 +- tests/PHPUnit/Integration/TrackerTest.php | 5 +- .../PHPUnit/System/BlobReportLimitingTest.php | 3 +- tests/PHPUnit/TestingEnvironment.php | 4 +- tests/PHPUnit/Unit/ConfigTest.php | 6 +- .../IniConfigDefinitionSourceTest.php | 7 +- 14 files changed, 137 insertions(+), 166 deletions(-) delete mode 100644 core/Application/Kernel/GlobalSettingsProvider/IniSettingsProvider.php diff --git a/core/Application/Environment.php b/core/Application/Environment.php index 6687b015c4..ed36586769 100644 --- a/core/Application/Environment.php +++ b/core/Application/Environment.php @@ -13,7 +13,6 @@ use Piwik\Application\Kernel\EnvironmentValidator; use Piwik\Application\Kernel\GlobalSettingsProvider; use Piwik\Application\Kernel\PluginList; use Piwik\Application\Kernel\PluginList\IniPluginList; -use Piwik\Application\Kernel\GlobalSettingsProvider\IniSettingsProvider; use Piwik\Container\ContainerFactory; use Piwik\Container\StaticContainer; use Piwik\Piwik; @@ -152,7 +151,7 @@ class Environment { // TODO: need to be able to set path global/local/etc. which is in DI... for now works because TestingEnvironment creates // singleton instance before this method. - return IniSettingsProvider::getSingletonInstance(); + return GlobalSettingsProvider::getSingletonInstance(); } /** diff --git a/core/Application/Kernel/EnvironmentValidator.php b/core/Application/Kernel/EnvironmentValidator.php index 4ebae1a139..ef013c7041 100644 --- a/core/Application/Kernel/EnvironmentValidator.php +++ b/core/Application/Kernel/EnvironmentValidator.php @@ -8,7 +8,7 @@ namespace Piwik\Application\Kernel; -use Piwik\Application\Kernel\GlobalSettingsProvider\IniSettingsProvider; +use Piwik\Application\Kernel\GlobalSettingsProvider; use Piwik\Common; use Piwik\Piwik; use Piwik\SettingsServer; @@ -22,9 +22,9 @@ use Piwik\Translation\Translator; class EnvironmentValidator { /** - * @var IniSettingsProvider + * @var GlobalSettingsProvider */ - protected $iniSettingsProvider; + protected $settingsProvider; /** * @var Translator @@ -33,7 +33,7 @@ class EnvironmentValidator public function __construct(GlobalSettingsProvider $settingsProvider, Translator $translator) { - $this->iniSettingsProvider = $settingsProvider; + $this->settingsProvider = $settingsProvider; $this->translator = $translator; } @@ -42,8 +42,8 @@ class EnvironmentValidator $inTrackerRequest = SettingsServer::isTrackerApiRequest(); $inConsole = Common::isPhpCliMode(); - $this->checkConfigFileExists($this->iniSettingsProvider->getPathGlobal()); - $this->checkConfigFileExists($this->iniSettingsProvider->getPathLocal(), $startInstaller = !$inTrackerRequest && !$inConsole); + $this->checkConfigFileExists($this->settingsProvider->getPathGlobal()); + $this->checkConfigFileExists($this->settingsProvider->getPathLocal(), $startInstaller = !$inTrackerRequest && !$inConsole); } /** diff --git a/core/Application/Kernel/GlobalSettingsProvider.php b/core/Application/Kernel/GlobalSettingsProvider.php index 1b4a810ecb..8a70a4fb29 100644 --- a/core/Application/Kernel/GlobalSettingsProvider.php +++ b/core/Application/Kernel/GlobalSettingsProvider.php @@ -8,24 +8,77 @@ namespace Piwik\Application\Kernel; +use Piwik\Config; +use Piwik\Config\IniFileChain; + /** * Provides global settings. Global settings are organized in sections where * each section contains a list of name => value pairs. Setting values can * be primitive values or arrays of primitive values. * - * By default, IniSettingsProvider is used which loads all global settings - * from the config.ini.php, global.ini.php files and the optional - * common.ini.php file. + * Uses the config.ini.php, common.ini.php and global.ini.php files to provide global settings. + * + * At the moment a singleton instance of this class is used in order to get tests to pass. */ -interface GlobalSettingsProvider +class GlobalSettingsProvider { + private static $instance = null; + + /** + * @var IniFileChain + */ + protected $iniFileChain; + + /** + * @var string + */ + protected $pathGlobal = null; + + /** + * @var string + */ + protected $pathCommon = null; + + /** + * @var string + */ + protected $pathLocal = null; + + /** + * @param string|null $pathGlobal Path to the global.ini.php file. Or null to use the default. + * @param string|null $pathLocal Path to the config.ini.php file. Or null to use the default. + * @param string|null $pathCommon Path to the common.ini.php file. Or null to use the default. + */ + public function __construct($pathGlobal = null, $pathLocal = null, $pathCommon = null) + { + $this->pathGlobal = $pathGlobal ?: Config::getGlobalConfigPath(); + $this->pathCommon = $pathCommon ?: Config::getCommonConfigPath(); + $this->pathLocal = $pathLocal ?: Config::getLocalConfigPath(); + + $this->iniFileChain = new IniFileChain(); + $this->reload(); + } + + public function reload($pathGlobal = null, $pathLocal = null, $pathCommon = null) + { + $this->pathGlobal = $pathGlobal ?: $this->pathGlobal; + $this->pathCommon = $pathCommon ?: $this->pathCommon; + $this->pathLocal = $pathLocal ?: $this->pathLocal; + + $this->iniFileChain->reload(array($this->pathGlobal, $this->pathCommon), $this->pathLocal); + } + /** * Returns a settings section. * * @param string $name * @return array */ - public function &getSection($name); + public function &getSection($name) + { + $section =& $this->iniFileChain->get($name); + return $section; + } /** * Sets a settings section. @@ -33,5 +86,54 @@ interface GlobalSettingsProvider * @param string $name * @param array $value */ - public function setSection($name, $value); + public function setSection($name, $value) + { + $this->iniFileChain->set($name, $value); + } + + public function getIniFileChain() + { + return $this->iniFileChain; + } + + public function getPathGlobal() + { + return $this->pathGlobal; + } + + public function getPathLocal() + { + return $this->pathLocal; + } + + public function getPathCommon() + { + return $this->pathCommon; + } + + public static function getSingletonInstance($pathGlobal = null, $pathLocal = null, $pathCommon = null) + { + if (self::$instance === null) { + self::$instance = new GlobalSettingsProvider($pathGlobal, $pathLocal, $pathCommon); + } else { + // sanity check. the parameters should only be non-null when creating the GlobalSettingsProvider the first time. + // if it's done after, it may point to a problem in the tests. (tests are the only place where these arguments + // should be specified) + if ($pathGlobal !== null + || $pathLocal !== null + || $pathCommon !== null + ) { + $message = "Unexpected state in GlobalSettingsProvider::getSingletonInstance: singleton already created but paths supplied:\n"; + $message .= "global = '$pathGlobal', local = '$pathLocal', common = '$pathCommon'\n"; + throw new \Exception($message); + } + } + + return self::$instance; + } + + public static function unsetSingletonInstance() + { + self::$instance = null; + } } \ No newline at end of file diff --git a/core/Application/Kernel/GlobalSettingsProvider/IniSettingsProvider.php b/core/Application/Kernel/GlobalSettingsProvider/IniSettingsProvider.php deleted file mode 100644 index 05f9b3c490..0000000000 --- a/core/Application/Kernel/GlobalSettingsProvider/IniSettingsProvider.php +++ /dev/null @@ -1,125 +0,0 @@ -<?php -/** - * Piwik - free/libre analytics platform - * - * @link http://piwik.org - * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later - */ - -namespace Piwik\Application\Kernel\GlobalSettingsProvider; - -use Piwik\Config; -use Piwik\Config\IniFileChain; -use Piwik\Application\Kernel\GlobalSettingsProvider; - -/** - * Default GlobalSettingsProvider implementation. This provider uses the config.ini.php, - * common.ini.php and global.ini.php files to provide global settings. - * - * At the moment a singleton instance of this class is used in order to get tests to pass. - */ -class IniSettingsProvider implements GlobalSettingsProvider -{ - private static $instance = null; - - /** - * @var IniFileChain - */ - protected $iniFileChain; - - /** - * @var string - */ - protected $pathGlobal = null; - - /** - * @var string - */ - protected $pathCommon = null; - - /** - * @var string - */ - protected $pathLocal = null; - - /** - * @param string|null $pathGlobal Path to the global.ini.php file. Or null to use the default. - * @param string|null $pathLocal Path to the config.ini.php file. Or null to use the default. - * @param string|null $pathCommon Path to the common.ini.php file. Or null to use the default. - */ - public function __construct($pathGlobal = null, $pathLocal = null, $pathCommon = null) - { - $this->pathGlobal = $pathGlobal ?: Config::getGlobalConfigPath(); - $this->pathCommon = $pathCommon ?: Config::getCommonConfigPath(); - $this->pathLocal = $pathLocal ?: Config::getLocalConfigPath(); - - $this->iniFileChain = new IniFileChain(); - $this->reload(); - } - - public function reload($pathGlobal = null, $pathLocal = null, $pathCommon = null) - { - $this->pathGlobal = $pathGlobal ?: $this->pathGlobal; - $this->pathCommon = $pathCommon ?: $this->pathCommon; - $this->pathLocal = $pathLocal ?: $this->pathLocal; - - $this->iniFileChain->reload(array($this->pathGlobal, $this->pathCommon), $this->pathLocal); - } - - public function &getSection($name) - { - $section =& $this->iniFileChain->get($name); - return $section; - } - - public function setSection($name, $value) - { - $this->iniFileChain->set($name, $value); - } - - public function getIniFileChain() - { - return $this->iniFileChain; - } - - public function getPathGlobal() - { - return $this->pathGlobal; - } - - public function getPathLocal() - { - return $this->pathLocal; - } - - public function getPathCommon() - { - return $this->pathCommon; - } - - public static function getSingletonInstance($pathGlobal = null, $pathLocal = null, $pathCommon = null) - { - if (self::$instance === null) { - self::$instance = new IniSettingsProvider($pathGlobal, $pathLocal, $pathCommon); - } else { - // sanity check. the parameters should only be non-null when creating the IniSettingsProvider the first time. - // if it's done after, it may point to a problem in the tests. (tests are the only place where these arguments - // should be specified) - if ($pathGlobal !== null - || $pathLocal !== null - || $pathCommon !== null - ) { - $message = "Unexpected state in IniSettingsProvider::getSingletonInstance: singleton already created but paths supplied:\n"; - $message .= "global = '$pathGlobal', local = '$pathLocal', common = '$pathCommon'\n"; - throw new \Exception($message); - } - } - - return self::$instance; - } - - public static function unsetSingletonInstance() - { - self::$instance = null; - } -} \ No newline at end of file diff --git a/core/Application/Kernel/PluginList/IniPluginList.php b/core/Application/Kernel/PluginList/IniPluginList.php index 8825661e82..a523e50e71 100644 --- a/core/Application/Kernel/PluginList/IniPluginList.php +++ b/core/Application/Kernel/PluginList/IniPluginList.php @@ -9,19 +9,18 @@ namespace Piwik\Application\Kernel\PluginList; use Piwik\Application\Kernel\GlobalSettingsProvider; -use Piwik\Application\Kernel\GlobalSettingsProvider\IniSettingsProvider; use Piwik\Application\Kernel\PluginList; /** * Default implementation of the PluginList interface. Uses the [Plugins] section * in Piwik's INI config to get the activated plugins. * - * Depends on IniSettingsProvider being used. + * Depends on GlobalSettingsProvider being used. */ class IniPluginList implements PluginList { /** - * @var IniSettingsProvider + * @var GlobalSettingsProvider */ private $settings; diff --git a/core/Config.php b/core/Config.php index 3e8ab98797..ac6f9c18af 100644 --- a/core/Config.php +++ b/core/Config.php @@ -11,7 +11,6 @@ namespace Piwik; use Exception; use Piwik\Application\Kernel\GlobalSettingsProvider; -use Piwik\Application\Kernel\GlobalSettingsProvider\IniSettingsProvider; /** * Singleton that provides read & write access to Piwik's INI configuration. @@ -52,7 +51,7 @@ class Config extends Singleton protected $doNotWriteConfigInTests = false; /** - * @var IniSettingsProvider + * @var GlobalSettingsProvider */ protected $settings; @@ -60,7 +59,7 @@ class Config extends Singleton public function __construct($pathGlobal = null, $pathLocal = null, $pathCommon = null) { - $this->settings = IniSettingsProvider::getSingletonInstance($pathGlobal, $pathLocal, $pathCommon); + $this->settings = GlobalSettingsProvider::getSingletonInstance($pathGlobal, $pathLocal, $pathCommon); } /** diff --git a/tests/PHPUnit/Framework/Fixture.php b/tests/PHPUnit/Framework/Fixture.php index 90f2ff95cc..460deb10d8 100644 --- a/tests/PHPUnit/Framework/Fixture.php +++ b/tests/PHPUnit/Framework/Fixture.php @@ -9,7 +9,7 @@ namespace Piwik\Tests\Framework; use Piwik\Access; use Piwik\Application\Environment; -use Piwik\Application\Kernel\GlobalSettingsProvider\IniSettingsProvider; +use Piwik\Application\Kernel\GlobalSettingsProvider; use Piwik\Archive; use Piwik\Cache\Backend\File; use Piwik\Cache as PiwikCache; @@ -151,7 +151,7 @@ class Fixture extends \PHPUnit_Framework_Assert public function performSetUp($setupEnvironmentOnly = false) { if ($this->createConfig) { - IniSettingsProvider::unsetSingletonInstance(); + GlobalSettingsProvider::unsetSingletonInstance(); } $this->piwikEnvironment = new Environment('test'); @@ -329,7 +329,7 @@ class Fixture extends \PHPUnit_Framework_Assert $_GET = $_REQUEST = array(); Translate::reset(); - IniSettingsProvider::unsetSingletonInstance(); + GlobalSettingsProvider::unsetSingletonInstance(); Config::setSingletonInstance(new TestConfig()); Config::getInstance()->Plugins; // make sure Plugins exists in a config object for next tests that use Plugin\Manager diff --git a/tests/PHPUnit/Framework/Mock/TestConfig.php b/tests/PHPUnit/Framework/Mock/TestConfig.php index 75c67e7d62..2abb04c5ae 100644 --- a/tests/PHPUnit/Framework/Mock/TestConfig.php +++ b/tests/PHPUnit/Framework/Mock/TestConfig.php @@ -20,7 +20,7 @@ class TestConfig extends Config public function __construct($pathGlobal = null, $pathLocal = null, $pathCommon = null, $allowSave = false, $doSetTestEnvironment = true) { - \Piwik\Application\Kernel\GlobalSettingsProvider\IniSettingsProvider::unsetSingletonInstance(); + \Piwik\Application\Kernel\GlobalSettingsProvider::unsetSingletonInstance(); parent::__construct($pathGlobal, $pathLocal, $pathCommon); diff --git a/tests/PHPUnit/Framework/TestCase/UnitTestCase.php b/tests/PHPUnit/Framework/TestCase/UnitTestCase.php index e753a7ff96..275f91eed8 100755 --- a/tests/PHPUnit/Framework/TestCase/UnitTestCase.php +++ b/tests/PHPUnit/Framework/TestCase/UnitTestCase.php @@ -9,7 +9,7 @@ namespace Piwik\Tests\Framework\TestCase; use Piwik\Application\Environment; -use Piwik\Application\Kernel\GlobalSettingsProvider\IniSettingsProvider; +use Piwik\Application\Kernel\GlobalSettingsProvider; use Piwik\Container\StaticContainer; use Piwik\EventDispatcher; use Piwik\Tests\Framework\Mock\File; @@ -30,7 +30,7 @@ abstract class UnitTestCase extends \PHPUnit_Framework_TestCase { parent::setUp(); - IniSettingsProvider::unsetSingletonInstance(); + GlobalSettingsProvider::unsetSingletonInstance(); $this->initEnvironment(); @@ -42,7 +42,7 @@ abstract class UnitTestCase extends \PHPUnit_Framework_TestCase { File::reset(); - IniSettingsProvider::unsetSingletonInstance(); + GlobalSettingsProvider::unsetSingletonInstance(); // make sure the global container exists for the next test case that is executed (since logging can be done // before a test sets up an environment) diff --git a/tests/PHPUnit/Integration/TrackerTest.php b/tests/PHPUnit/Integration/TrackerTest.php index e8864a22bb..a7a3978e85 100644 --- a/tests/PHPUnit/Integration/TrackerTest.php +++ b/tests/PHPUnit/Integration/TrackerTest.php @@ -8,7 +8,7 @@ namespace Piwik\Tests\Integration; -use Piwik\Application\Kernel\GlobalSettingsProvider\IniSettingsProvider; +use Piwik\Application\Kernel\GlobalSettingsProvider; use Piwik\Common; use Piwik\Config; use Piwik\EventDispatcher; @@ -20,7 +20,6 @@ use Piwik\Tests\Framework\TestCase\IntegrationTestCase; use Piwik\Tracker; use Piwik\Tracker\RequestSet; use Piwik\Tracker\Request; -use Piwik\Translate; class TestTracker extends Tracker { @@ -60,7 +59,7 @@ class TrackerTest extends IntegrationTestCase { parent::setUp(); - IniSettingsProvider::unsetSingletonInstance(); + GlobalSettingsProvider::unsetSingletonInstance(); Config::unsetInstance(); Fixture::createWebsite('2014-01-01 00:00:00'); diff --git a/tests/PHPUnit/System/BlobReportLimitingTest.php b/tests/PHPUnit/System/BlobReportLimitingTest.php index 8122fd195c..9785f31fd1 100755 --- a/tests/PHPUnit/System/BlobReportLimitingTest.php +++ b/tests/PHPUnit/System/BlobReportLimitingTest.php @@ -7,7 +7,7 @@ */ namespace Piwik\Tests\System; -use Piwik\Application\Kernel\GlobalSettingsProvider\IniSettingsProvider; +use Piwik\Application\Kernel\GlobalSettingsProvider; use Piwik\Cache; use Piwik\Config; use Piwik\Plugins\Actions\ArchivingHelper; @@ -174,7 +174,6 @@ class BlobReportLimitingTest extends SystemTestCase protected static function setUpConfigOptions() { - IniSettingsProvider::unsetSingletonInstance(); Config::setSingletonInstance(new TestConfig()); $generalConfig =& Config::getInstance()->General; diff --git a/tests/PHPUnit/TestingEnvironment.php b/tests/PHPUnit/TestingEnvironment.php index b7590eee17..f0b3e8ed50 100644 --- a/tests/PHPUnit/TestingEnvironment.php +++ b/tests/PHPUnit/TestingEnvironment.php @@ -102,7 +102,7 @@ class Piwik_TestingEnvironment public function getCoreAndSupportedPlugins() { - $settings = new \Piwik\Application\Kernel\GlobalSettingsProvider\IniSettingsProvider(); + $settings = new \Piwik\Application\Kernel\GlobalSettingsProvider(); $pluginManager = new PluginManager(new \Piwik\Application\Kernel\PluginList\IniPluginList($settings)); $disabledPlugins = $pluginManager->getCorePluginsDisabledByDefault(); @@ -151,7 +151,7 @@ class Piwik_TestingEnvironment \Piwik\Profiler::setupProfilerXHProf($mainRun = false, $setupDuringTracking = true); } - \Piwik\Application\Kernel\GlobalSettingsProvider\IniSettingsProvider::getSingletonInstance( + \Piwik\Application\Kernel\GlobalSettingsProvider::getSingletonInstance( $testingEnvironment->configFileGlobal, $testingEnvironment->configFileLocal, $testingEnvironment->configFileCommon diff --git a/tests/PHPUnit/Unit/ConfigTest.php b/tests/PHPUnit/Unit/ConfigTest.php index 6ccd93a0fd..b7b0c47782 100644 --- a/tests/PHPUnit/Unit/ConfigTest.php +++ b/tests/PHPUnit/Unit/ConfigTest.php @@ -9,7 +9,7 @@ namespace Piwik\Tests\Unit; use PHPUnit_Framework_TestCase; -use Piwik\Application\Kernel\GlobalSettingsProvider\IniSettingsProvider; +use Piwik\Application\Kernel\GlobalSettingsProvider; use Piwik\Config; use Piwik\Tests\Framework\Mock\TestConfig; @@ -24,7 +24,7 @@ class DumpConfigTestMockIniFileChain extends Config\IniFileChain } } -class MockIniSettingsProvider extends IniSettingsProvider +class MockIniSettingsProvider extends GlobalSettingsProvider { public function __construct($configLocal, $configGlobal, $configCommon, $configCache) { @@ -60,7 +60,7 @@ class ConfigTest extends \PHPUnit_Framework_TestCase { parent::setUp(); - IniSettingsProvider::unsetSingletonInstance(); + GlobalSettingsProvider::unsetSingletonInstance(); } public function testUserConfigOverwritesSectionGlobalConfigValue() diff --git a/tests/PHPUnit/Unit/Container/IniConfigDefinitionSourceTest.php b/tests/PHPUnit/Unit/Container/IniConfigDefinitionSourceTest.php index 72b3cd6a97..ffcf9293ae 100644 --- a/tests/PHPUnit/Unit/Container/IniConfigDefinitionSourceTest.php +++ b/tests/PHPUnit/Unit/Container/IniConfigDefinitionSourceTest.php @@ -10,7 +10,6 @@ namespace Piwik\Tests\Unit\Container; use DI\Definition\ValueDefinition; use Piwik\Application\Kernel\GlobalSettingsProvider; -use Piwik\Application\Kernel\GlobalSettingsProvider\IniSettingsProvider; use Piwik\Container\IniConfigDefinitionSource; class IniConfigDefinitionSourceTest extends \PHPUnit_Framework_TestCase @@ -30,7 +29,7 @@ class IniConfigDefinitionSourceTest extends \PHPUnit_Framework_TestCase */ public function getDefinition_withUnknownConfigSection_shouldReturnEmptyArray() { - $definitionSource = new IniConfigDefinitionSource(new IniSettingsProvider()); + $definitionSource = new IniConfigDefinitionSource(new GlobalSettingsProvider()); /** @var ValueDefinition $definition */ $definition = $definitionSource->getDefinition('ini.foo'); @@ -45,7 +44,7 @@ class IniConfigDefinitionSourceTest extends \PHPUnit_Framework_TestCase */ public function getDefinition_withUnknownConfigSectionAndKey_shouldReturnNull() { - $definitionSource = new IniConfigDefinitionSource(new IniSettingsProvider()); + $definitionSource = new IniConfigDefinitionSource(new GlobalSettingsProvider()); $this->assertNull($definitionSource->getDefinition('ini.foo.bar')); } @@ -55,7 +54,7 @@ class IniConfigDefinitionSourceTest extends \PHPUnit_Framework_TestCase */ public function getDefinition_withUnknownConfigKey_shouldReturnNull() { - $definitionSource = new IniConfigDefinitionSource(new IniSettingsProvider()); + $definitionSource = new IniConfigDefinitionSource(new GlobalSettingsProvider()); $this->assertNull($definitionSource->getDefinition('ini.General.foo')); } -- GitLab