MIF_E31222691/system/config.php

139 lines
3.8 KiB
PHP

<?php namespace System;
class Config {
/**
* All of the loaded configuration items.
*
* @var array
*/
public static $items = array();
/**
* Determine if a configuration item or file exists.
*
* @param string $key
* @return bool
*/
public static function has($key)
{
return ! is_null(static::get($key));
}
/**
* Get a configuration item.
*
* Configuration items are retrieved using "dot" notation. So, asking for the
* "application.timezone" configuration item would return the "timezone" option
* from the "application" configuration file.
*
* If the name of a configuration file is passed without specifying an item, the
* entire configuration array will be returned.
*
* @param string $key
* @param string $default
* @return array
*/
public static function get($key, $default = null)
{
list($module, $file, $key) = static::parse($key);
if ( ! static::load($module, $file))
{
return is_callable($default) ? call_user_func($default) : $default;
}
if (is_null($key))
{
return static::$items[$module][$file];
}
return Arr::get(static::$items[$module][$file], $key, $default);
}
/**
* Set a configuration item.
*
* @param string $key
* @param mixed $value
* @return void
*/
public static function set($key, $value)
{
list($module, $file, $key) = static::parse($key);
if (is_null($key) or ! static::load($module, $file))
{
throw new \Exception("Unable to find configuration file item [$key].");
}
static::$items[$module][$file][$key] = $value;
}
/**
* Parse a configuration key.
*
* The value on the left side of the dot is the configuration file
* name, while the right side of the dot is the item within that file.
*
* @param string $key
* @return array
*/
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';
// If the configuration item is stored in a module, we need to strip the module qualifier
// off of the configuration key before continuing.
if ($module != 'application')
{
$key = substr($key, strpos($key, ':') + 2);
}
$segments = explode('.', $key);
// If there is more than one segment, we need to splice together and portion of the
// configuration key that comes after the first segment, which is the file name.
$key = (count($segments) > 1) ? implode('.', array_slice($segments, 1)) : null;
return array($module, $segments[0], $key);
}
/**
* Load all of the configuration items from a file.
*
* If it exists, the configuration file in the application/config directory will be loaded first.
* Any environment specific configuration files will be merged with the root file.
*
* @param string $file
* @param string $module
* @return bool
*/
public static function load($module, $file)
{
// If the configuration items for this module and file have already been
// loaded, we can bail out of this method.
if (isset(static::$items[$module]) and array_key_exists($file, static::$items[$module])) return true;
$path = ($module === 'application') ? CONFIG_PATH : MODULE_PATH.$module.'/config/';
// Load the base configuration items for the module and file.
$config = (file_exists($base = $path.$file.EXT)) ? require $base : array();
// Merge any enviornment specific configuration items for the module and file.
if (isset($_SERVER['LARAVEL_ENV']) and file_exists($path = $path.$_SERVER['LARAVEL_ENV'].'/'.$file.EXT))
{
$config = array_merge($config, require $path);
}
if (count($config) > 0)
{
static::$items[$module][$file] = $config;
}
return isset(static::$items[$module][$file]);
}
}