Refactoring the Config class.

This commit is contained in:
Taylor Otwell 2011-08-08 08:46:33 -05:00
parent fe39d50783
commit 9018d6cceb
1 changed files with 6 additions and 18 deletions

View File

@ -38,15 +38,11 @@ public static function get($key, $default = null)
{ {
list($module, $file, $key) = static::parse($key); list($module, $file, $key) = static::parse($key);
if ( ! static::load($module, $file)) // If the configuration file doesn't exist, return the default value.
{ if ( ! static::load($module, $file)) return is_callable($default) ? call_user_func($default) : $default;
return is_callable($default) ? call_user_func($default) : $default;
}
if (is_null($key)) // If no key was specified, return the entire configuration array.
{ if (is_null($key)) return static::$items[$module][$file];
return static::$items[$module][$file];
}
return Arr::get(static::$items[$module][$file], $key, $default); return Arr::get(static::$items[$module][$file], $key, $default);
} }
@ -81,14 +77,9 @@ public static function set($key, $value)
*/ */
private static function parse($key) private static function parse($key)
{ {
// Check for a module qualifier. If a module name is present, we need to extract it from
// the configuration key, otherwise, we will use "application" as the module.
$module = (strpos($key, '::') !== false) ? substr($key, 0, strpos($key, ':')) : 'application'; $module = (strpos($key, '::') !== false) ? substr($key, 0, strpos($key, ':')) : 'application';
if ($module != 'application') if ($module !== 'application') $key = substr($key, strpos($key, ':') + 2);
{
$key = substr($key, strpos($key, ':') + 2);
}
$segments = explode('.', $key); $segments = explode('.', $key);
@ -122,10 +113,7 @@ public static function load($module, $file)
$config = array_merge($config, require $path); $config = array_merge($config, require $path);
} }
if (count($config) > 0) if (count($config) > 0) static::$items[$module][$file] = $config;
{
static::$items[$module][$file] = $config;
}
return isset(static::$items[$module][$file]); return isset(static::$items[$module][$file]);
} }