merged skunkworks into develop.

This commit is contained in:
Taylor Otwell
2012-01-16 13:59:24 -06:00
parent 610d8827c4
commit b5442c67fc
117 changed files with 7268 additions and 3999 deletions

View File

@@ -1,22 +1,24 @@
<?php namespace Laravel; use Closure;
<?php namespace Laravel; defined('APP_PATH') or die('No direct script access.');
use Closure;
class Config {
/**
* All of the loaded configuration items.
*
* The configuration arrays are keyed by their owning file name.
* The configuration arrays are keyed by their owning bundle and file.
*
* @var array
*/
public static $items = array();
/**
* The paths to the configuration files.
* A cache of the loaded configuration items.
*
* @var array
*/
public static $paths = array(SYS_CONFIG_PATH, CONFIG_PATH, ENV_CONFIG_PATH);
protected static $cache = array();
/**
* Determine if a configuration item or file exists.
@@ -25,7 +27,7 @@ class Config {
* // Determine if the "session" configuration file exists
* $exists = Config::has('session');
*
* // Determine if the "timezone" option exists in the "application" configuration
* // Determine if the "timezone" option exists in the configuration
* $exists = Config::has('application.timezone');
* </code>
*
@@ -46,29 +48,38 @@ class Config {
* // Get the "session" configuration array
* $session = Config::get('session');
*
* // Get a configuration item from a bundle's configuration file
* $name = Config::get('admin::names.first');
*
* // Get the "timezone" option from the "application" configuration file
* $timezone = Config::get('application.timezone');
* </code>
*
* @param string $key
* @param string $default
* @return array
*/
public static function get($key, $default = null)
public static function get($key)
{
list($file, $key) = static::parse($key);
if ( ! static::load($file))
// First, we'll check the keyed cache of configuration items, as this will
// be the fastest method of retrieving the configuration option. After an
// item is retrieved, it is always stored in the cache by its key.
if (array_key_exists($key, static::$cache))
{
return ($default instanceof Closure) ? call_user_func($default) : $default;
return static::$cache[$key];
}
$items = static::$items[$file];
list($bundle, $file, $item) = static::parse($key);
if ( ! static::load($bundle, $file)) return;
$items = static::$items[$bundle][$file];
// If a specific configuration item was not requested, the key will be null,
// meaning we need to return the entire array of configuration item from the
// requested configuration file. Otherwise we can return the item.
return (is_null($key)) ? $items : Arr::get($items, $key, $default);
$value = (is_null($item)) ? $items : array_get($items, $item);
return static::$cache[$key] = $value;
}
/**
@@ -78,6 +89,9 @@ class Config {
* // Set the "session" configuration array
* Config::set('session', $array);
*
* // Set a configuration option that belongs by a bundle
* Config::set('admin::names.first', 'Taylor');
*
* // Set the "timezone" option in the "application" configuration file
* Config::set('application.timezone', 'UTC');
* </code>
@@ -88,61 +102,53 @@ class Config {
*/
public static function set($key, $value)
{
list($file, $key) = static::parse($key);
static::load($file);
if (is_null($key))
{
Arr::set(static::$items, $file, $value);
}
else
{
Arr::set(static::$items[$file], $key, $value);
}
static::$cache[$key] = $value;
}
/**
* Parse a configuration key and return its file and key segments.
* Parse a key and return its bundle, file, and key segments.
*
* The first segment of a configuration key represents the configuration
* file, while the remaining segments represent an item within that file.
* If no item segment is present, null will be returned for the item value
* indicating that the entire configuration array should be returned.
* Configuration items are named using the {bundle}::{file}.{item} convention.
*
* @param string $key
* @return array
*/
protected static function parse($key)
{
$segments = explode('.', $key);
$bundle = Bundle::name($key);
$segments = explode('.', Bundle::element($key));
// If there are not at least two segments in the array, it means that the
// developer is requesting the entire configuration array to be returned.
// If that is the case, we'll make the item field of the array "null".
if (count($segments) >= 2)
{
return array($segments[0], implode('.', array_slice($segments, 1)));
return array($bundle, $segments[0], implode('.', array_slice($segments, 1)));
}
else
{
return array($segments[0], null);
return array($bundle, $segments[0], null);
}
}
/**
* Load all of the configuration items from a configuration file.
*
* @param string $bundle
* @param string $file
* @return bool
*/
public static function load($file)
public static function load($bundle, $file)
{
if (isset(static::$items[$file])) return true;
if (isset(static::$items[$bundle][$file])) return true;
$config = array();
// Configuration files cascade. Typically, the system configuration array is
// loaded first, followed by the application array, providing the convenient
// cascading of configuration options from system to application.
foreach (static::$paths as $directory)
// Configuration files cascade. Typically, the bundle configuration array is
// loaded first, followed by the environment array, providing the convenient
// cascading of configuration options across environments.
foreach (static::paths($bundle) as $directory)
{
if ($directory !== '' and file_exists($path = $directory.$file.EXT))
{
@@ -150,9 +156,37 @@ class Config {
}
}
if (count($config) > 0) static::$items[$file] = $config;
if (count($config) > 0)
{
static::$items[$bundle][$file] = $config;
}
return isset(static::$items[$file]);
return isset(static::$items[$bundle][$file]);
}
/**
* Get the array of configuration paths that should be searched for a bundle.
*
* @param string $bundle
* @return array
*/
protected static function paths($bundle)
{
$paths[] = Bundle::path($bundle).'config/';
// Configuration files can be made specific for a given environment. If an
// environment has been set, we will merge the environment configuration
// in last, so that it overrides all other options.
//
// This allows the developer to quickly and easily create configurations
// for various scenarios, such as local development and production,
// without constantly changing configuration files.
if (isset($_SERVER['LARAVEL_ENV']))
{
$paths[] = $paths[count($paths) - 1].$_SERVER['LARAVEL_ENV'].'/';
}
return $paths;
}
}