From adb583471b01475bc3d8ced119af71da3074c691 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Tue, 21 Jun 2011 19:44:04 -0500 Subject: [PATCH] added Config::has and default value for Config::get. --- system/config.php | 41 +++++++++++++++++++++++++++++------------ 1 file changed, 29 insertions(+), 12 deletions(-) diff --git a/system/config.php b/system/config.php index f1180da4..d0105ac1 100644 --- a/system/config.php +++ b/system/config.php @@ -9,35 +9,57 @@ class Config { */ private static $items = array(); + /** + * Determine if a configuration item exists. + * + * @param string $key + * @return bool + */ + public static function has($key) + { + return ! is_null(static::get($key)); + } + /** * Get a configuration item. * * @param string $key + * @param string $default * @return mixed */ - public static function get($key) + public static function get($key, $default = null) { // ----------------------------------------------------- // If a dot is not present, we will just return the // entire configuration array. + // + // If the configuration file does not exist, the default + // value will be returned. // ----------------------------------------------------- if(strpos($key, '.') === false) { static::load($key); - return static::$items[$key]; + return (array_key_exists($key, static::$items)) ? static::$items[$key] : $default; } list($file, $key) = static::parse($key); static::load($file); - if (array_key_exists($key, static::$items[$file])) + // ----------------------------------------------------- + // If the file doesn't exist, return the default. + // ----------------------------------------------------- + if ( ! array_key_exists($file, static::$items)) { - return static::$items[$file][$key]; + return $default; } - throw new \Exception("Configuration item [$key] is not defined."); + // ----------------------------------------------------- + // Return the configuration item. If the item doesn't + // exist, the default value will be returned. + // ----------------------------------------------------- + return (array_key_exists($key, static::$items[$file])) ? static::$items[$file][$key] : $default; } /** @@ -91,18 +113,13 @@ private static function parse($key) public static function load($file) { // ----------------------------------------------------- - // If we have already loaded the file, bail out. + // Bail out if already loaded or doesn't exist. // ----------------------------------------------------- - if (array_key_exists($file, static::$items)) + if (array_key_exists($file, static::$items) or ! file_exists($path = APP_PATH.'config/'.$file.EXT)) { return; } - if ( ! file_exists($path = APP_PATH.'config/'.$file.EXT)) - { - throw new \Exception("Configuration file [$file] does not exist."); - } - // ----------------------------------------------------- // Load the configuration array into the array of items. // The items array is keyed by filename.