diff --git a/src/Config/.Configuration.php.swp b/src/Config/.Configuration.php.swp new file mode 100644 index 0000000..63936af Binary files /dev/null and b/src/Config/.Configuration.php.swp differ diff --git a/src/Config/Configuration.php b/src/Config/Configuration.php index 19bd720..acb0b3e 100644 --- a/src/Config/Configuration.php +++ b/src/Config/Configuration.php @@ -8,6 +8,11 @@ class Configuration { */ private static $instance = null; + /** + * @var + */ + private static $customConfigFilePath; + /** * ! WARNING ! * @@ -23,8 +28,9 @@ class Configuration { */ private $values = array(); - private $customConfigFilename = 'configuration.local.php'; - + /** + * + */ private function __construct() { if (!isset($this->values['session'])) { @@ -34,27 +40,47 @@ class Configuration { if (!isset($this->values['session']['save_path'])) { $this->values['session']['save_path'] = session_save_path(); } + + if (file_exists(self::getConfigFilePath())) { + $this->loadConfigFromFile(); + } + } + + /** + * The path to the file including file name + * @param string $filePath + * @return void + */ + public static function setConfigFilePath($filePath) + { + if (!file_exists($filePath)) { + throw new \RuntimeException("The file path $filePath, does not exist"); + } + self::$customConfigFilePath = $filePath; } /** * * @return String */ - public function getCustomConfigFilePath() + public static function getConfigFilePath() { - return realpath(dirname(__FILE__) . '/../../../../../config/') . $this->customConfigFilename; + if (null === self::$customConfigFilePath) { + self::$customConfigFilePath = realpath(dirname(__FILE__) . '/../../../../../config/configuration.local.php'); + } + return self::$customConfigFilePath; } /** * @return array * @throws \RuntimeException */ - public function getCustomConfigFromFile() + public function getConfigFromFile() { - if (!file_exists($this->getCustomConfigFilePath())) { - throw new \RuntimeException("The file : {$this->getCustomConfigFilePath()} does not exist"); + if (!file_exists(self::getConfigFilePath())) { + throw new \RuntimeException("The file : {self::getConfigFilePath()} does not exist"); } - return include $this->getCustomConfigFilePath(); + return include self::getConfigFilePath(); } /** @@ -62,9 +88,9 @@ class Configuration { * @return void * @throws \RuntimeException */ - public function loadCustomConfigFromFile() + public function loadConfigFromFile() { - $this->setCustomConfig($this->getCustomConfigFromFile()); + $this->setCustomConfig($this->getConfigFromFile()); } /** diff --git a/tests/Config/ConfigurationTest.php b/tests/Config/ConfigurationTest.php index 631965b..003a9e7 100644 --- a/tests/Config/ConfigurationTest.php +++ b/tests/Config/ConfigurationTest.php @@ -16,10 +16,12 @@ class ConfigurationTest extends TestCase ); protected $defaultConfig; + protected $dummyConfigFilePath; public function setUp() { $this->defaultConfig = array('session' => array('save_path' => session_save_path())); + $this->dummyConfigFilePath = realpath(dirname(__FILE__) . '/../../config/configuration.local.php'); } public function testConfigurationInstanceIsTheSame() @@ -113,4 +115,17 @@ class ConfigurationTest extends TestCase Configuration::getInstance()->setCustomConfig($this->testConfig); $this->assertSame(Configuration::get(), $this->testConfig); } + + public function testDummyConfigFileExists() + { + $this->assertEquals(file_exists($this->dummyConfigFilePath), true); + } + + public function testFileCustomConfigGetsLoadedIfFileExists() + { + $configPriorToFileLoading = Configuration::get(); + Configuration::setConfigFilePath($this->dummyConfigFilePath); + Configuration::getInstance()->loadConfigFromFile(); + $this->assertSame(Configuration::get(), array_replace_recursive($this->testConfig, include $this->dummyConfigFilePath)); + } }