Files
spa-api/system/config.php

133 lines
3.4 KiB
PHP
Raw Normal View History

2011-06-08 23:45:08 -05:00
<?php namespace System;
class Config {
/**
* All of the loaded configuration items.
*
* @var array
*/
2011-07-26 22:56:51 -05:00
public static $items = array();
2011-06-08 23:45:08 -05:00
/**
2011-07-07 23:16:52 -05:00
* Determine if a configuration item or file exists.
*
* @param string $key
* @return bool
*/
public static function has($key)
{
return ! is_null(static::get($key));
}
2011-06-08 23:45:08 -05:00
/**
* Get a configuration item.
*
2011-07-07 23:16:52 -05:00
* 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.
*
2011-06-08 23:45:08 -05:00
* @param string $key
* @param string $default
2011-07-07 23:16:52 -05:00
* @return array
2011-06-08 23:45:08 -05:00
*/
public static function get($key, $default = null)
2011-06-08 23:45:08 -05:00
{
2011-08-03 22:10:07 -05:00
list($module, $file, $key) = static::parse($key);
2011-06-16 22:45:33 -05:00
2011-08-03 22:10:07 -05:00
if ( ! static::load($module, $file))
{
return is_callable($default) ? call_user_func($default) : $default;
}
2011-08-03 22:10:07 -05:00
if (is_null($key))
2011-06-16 20:21:23 -05:00
{
2011-08-03 22:10:07 -05:00
return static::$items[$module][$file];
2011-06-16 20:21:23 -05:00
}
2011-08-03 22:10:07 -05:00
return Arr::get(static::$items[$module][$file], $key, $default);
2011-06-08 23:45:08 -05:00
}
/**
* Set a configuration item.
*
* @param string $key
* @param mixed $value
* @return void
*/
2011-06-10 12:19:19 -07:00
public static function set($key, $value)
2011-06-08 23:45:08 -05:00
{
2011-08-03 22:10:07 -05:00
list($module, $file, $key) = static::parse($key);
2011-06-16 22:45:33 -05:00
2011-08-03 22:10:07 -05:00
if (is_null($key) or ! static::load($module, $file))
{
2011-08-06 20:42:53 -05:00
throw new \Exception("Error setting configuration option. Option [$key] is not defined.");
2011-08-03 22:10:07 -05:00
}
2011-06-08 23:45:08 -05:00
2011-08-03 22:10:07 -05:00
static::$items[$module][$file][$key] = $value;
2011-06-08 23:45:08 -05:00
}
/**
* Parse a configuration key.
*
2011-07-07 23:16:52 -05:00
* 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.
*
2011-06-08 23:45:08 -05:00
* @param string $key
* @return array
*/
private static function parse($key)
{
2011-08-03 22:10:07 -05:00
// 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';
2011-06-08 23:45:08 -05:00
2011-08-03 22:10:07 -05:00
if ($module != 'application')
2011-06-08 23:45:08 -05:00
{
2011-08-03 22:10:07 -05:00
$key = substr($key, strpos($key, ':') + 2);
2011-06-08 23:45:08 -05:00
}
2011-08-03 22:10:07 -05:00
$segments = explode('.', $key);
$key = (count($segments) > 1) ? implode('.', array_slice($segments, 1)) : null;
return array($module, $segments[0], $key);
2011-06-08 23:45:08 -05:00
}
/**
* Load all of the configuration items from a file.
2011-06-08 23:45:08 -05:00
*
* 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.
*
2011-06-08 23:45:08 -05:00
* @param string $file
2011-08-03 22:10:07 -05:00
* @param string $module
2011-08-02 10:43:12 -05:00
* @return bool
2011-06-08 23:45:08 -05:00
*/
2011-08-03 22:10:07 -05:00
public static function load($module, $file)
2011-06-08 23:45:08 -05:00
{
2011-08-03 22:10:07 -05:00
if (isset(static::$items[$module]) and array_key_exists($file, static::$items[$module])) return true;
$path = ($module === 'application') ? CONFIG_PATH : MODULE_PATH.$module.'/config/';
2011-08-03 22:10:07 -05:00
// Load the base configuration items for the module and file.
$config = (file_exists($base = $path.$file.EXT)) ? require $base : array();
2011-08-03 22:10:07 -05:00
// 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))
2011-06-08 23:45:08 -05:00
{
$config = array_merge($config, require $path);
2011-06-08 23:45:08 -05:00
}
2011-08-02 20:20:17 -05:00
if (count($config) > 0)
{
2011-08-03 22:10:07 -05:00
static::$items[$module][$file] = $config;
2011-08-02 20:20:17 -05:00
}
2011-08-03 22:10:07 -05:00
return isset(static::$items[$module][$file]);
2011-06-08 23:45:08 -05:00
}
}