diff --git a/core/Application/Environment.php b/core/Application/Environment.php index 6687b015c46cba228e33884b5ac4f101f378284f..ed3658676931a1aec2514baff770760622059709 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 4ebae1a13922dae36ce2dcf4a3a507489e513db3..ef013c7041274c8eda7c7515856ab543b27aaaae 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 1b4a810ecb0a6b55a9daea4628b1c05f0918068a..8a70a4fb29d4335e0988c51524c92c90fadc4a83 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 05f9b3c49030de1d316ae85585129049be61de78..0000000000000000000000000000000000000000 --- 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 8825661e8255220118ae9acdc515ceacc17c6c8e..a523e50e71b535f07bcf2c4102b510bbfaa18b9a 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 3e8ab98797ba862821d8385bbd6075b75e2bdee1..ac6f9c18af64b22943b6e11a14ff14c3b5934491 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 90f2ff95cc392eca70459a3507681ed153f4ae49..460deb10d85a9d346d78b0e5642d0a9e742351a2 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 75c67e7d628541f92a5581c7f59648d3d235ff36..2abb04c5aee65f3ce96cc67ff60cf5e64aae695d 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 e753a7ff965e0297668fb954675a074ab4ab68e7..275f91eed898f650d750f05dc1a9cb3c95344ab2 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 e8864a22bb2fc3b0f2615bfbed05f85ec46f0f16..a7a3978e85b5941ed1a258a3ed86d117e95d274c 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 8122fd195cbbcd5236403a297ef0002d4dfec281..9785f31fd1970e406aad3e3148ecbe11eb893422 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 b7590eee17cc36155f16e6de8c56fc83b6818af1..f0b3e8ed5087c29ccd3134f19927d9d084942a27 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 6ccd93a0fd30841f8af080e0f1427af2d7181479..b7b0c4778216c568716e4b8b8b7067e610458384 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 72b3cd6a97cfe06632b8849ff6c2d8414efef802..ffcf9293ae11fabe3c7c804a12c3792151f4d431 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')); }