values['session'])) { $this->values['session'] = array(); } 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, $clearPrevious = false) { if (!file_exists($filePath)) { throw new \RuntimeException("The file path $filePath, does not exist"); } self::$customConfigFilePath = $filePath; $instance = self::getInstance(); if ($clearPrevious) { $instance->clear(); } self::getInstance()->loadConfigFromFile(); } /** * * @return String */ public static function getConfigFilePath() { if (null === self::$customConfigFilePath) { $envfile = getenv('APPLICATION_ENV') === 'development' ? 'local' : 'global'; self::$customConfigFilePath = realpath(dirname(__FILE__) . "/../../../../../../config/configuration.$envfile.php"); } return self::$customConfigFilePath; } /** * @return array * @throws \RuntimeException */ public function getConfigFromFile() { if (!file_exists(self::getConfigFilePath())) { throw new \RuntimeException("The file : {self::getConfigFilePath()} does not exist"); } return include self::getConfigFilePath(); } /** * * @return void * @throws \RuntimeException */ private function loadConfigFromFile() { $this->setCustomConfig($this->getConfigFromFile()); } /** * @param array $config * @return void */ public function setCustomConfig(array $config) { $this->values = array_replace_recursive($this->values, $config); } /** * @return array */ public function getConfig() { return $this->values; } /** * Checks whether the nested key exists * * @param array $data * @param array $keys * @return boolean */ public static function existsKeys(array $data, array $keys) { foreach ($keys as $k) { if (!isset($data[$k])) { return false; } $data = $data[$k]; } return true; } /** * @param array $data * @param array $keys * @param any $default * @return boolean */ public static function fetchRecursive(array $data, array $keys, $default = null) { foreach ($keys as $k) { if (!is_array($data)) { throw new \Exception("Try to access non-array as array, key '$key''"); } if (!isset($data[$k])) { return $default; } $data = $data[$k]; } return $data; } /** * Return array of keys if dot notation string is passed * @param string|array $keys */ public static function parseKeys($keys) { if (is_string($keys)) { $keys = explode('.', $keys); } else if (!is_array($keys)) { throw new \RuntimeException('Unsupported $keys paramater type'); } return $keys; } /** * Allow clearing previously loaded config */ public function clear() { $this->values = array(); } /** * @return string * @throws \Exception */ public function value($keys, $default) { return self::fetchRecursive($this->values, self::parseKeys($keys), $default); } /** * @return boolean */ public static function has($keys) { return self::existsKeys(self::get(), self::parseKeys($keys)); } /** * @param $name * @param mixed $default the default value for your configuration option * @return mixed return the configuration value if the key is find, the default otherwise */ public static function get($name = null, $default = null) { if (null === $name) { return self::getInstance()->getConfig(); } return self::getInstance()->value($name, $default); } /** * @return Bsr\Config\Configuration */ public static function getInstance() { if(is_null(self::$instance)) { self::$instance = new Configuration(); } return self::$instance; } }