Created
October 12, 2011 14:25
-
-
Save nerdsrescueme/1281353 to your computer and use it in GitHub Desktop.
Config
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Core Atom library namespace. This namespace contains all of the fundamental | |
* components of the Atom framework, plus additional utilities that are provided | |
* by default. Some of these default components have sub namespaces if they | |
* provide child objects. | |
* | |
* @package Atom | |
* @subpackage Library | |
*/ | |
namespace Atom; | |
// Aliasing rules | |
use \Atom\Arr; | |
/** | |
* Config class | |
* | |
* Provides methods for working with configuration items residing within | |
* configuration files. | |
* | |
* @package Atom | |
* @subpackage Library | |
*/ | |
class Config { | |
public static $items = array(); | |
public static $cache = 'file'; | |
public static $overrides; | |
public static function get($key, $default = null) | |
{ | |
if(isset(static::$overrides->local[$key])) | |
{ | |
return static::$overrides->local[$key]; | |
} | |
list($package, $file, $key) = static::parse($key); | |
if(!static::load($package, $file)) | |
{ | |
return \is_callable($default) ? \call_user_func($default) : $default; | |
} | |
if($key === null) | |
{ | |
return static::$items[$package][$file]; | |
} | |
$config = Arr::get(static::$items[$package][$file], $key, $default); | |
return $config; | |
} | |
public static function set($key, $value, $override = false) | |
{ | |
if($override) | |
{ | |
static::$overrides->local[$key] = $value | |
static::$overrides->write(static::$overrides->local); | |
} | |
list($package, $file, $key) = static::parse($key); | |
if(!static::load($package, $file)) | |
{ | |
throw new \OutOfBoundsException('Error setting configuration option. Configuration file ['.$file.'] is not defined.'); | |
} | |
Arr::set(static::$items[$package][$file], $key, $value); | |
} | |
private static function parse($key) | |
{ | |
$package = (\strpos($key, '::') !== false) ? \substr($key, 0, \strpos($key, ':')) : 'application'; | |
if($package !== 'application') | |
{ | |
$key = \substr($key, \strpos($key, ':') + 2); | |
} | |
$key = (\count($segments = \explode('.', $key)) > 1) ? \implode('.', \array_slice($segments, 1)) : null; | |
return array($package, $segments[0], $key); | |
} | |
private static function load($package, $file) | |
{ | |
if(isset(static::$items[$package]) and isset(static::$items[$package][$file])) | |
{ | |
return true; | |
} | |
if(static::$overrides === null) | |
{ | |
static::$overrides = new \Atom\Cache('config.local.overrides', 'file'); | |
} | |
if(static::$cache !== null) | |
{ | |
$cache = new \Atom\Cache('config.'.$package.'.'.$file, static::$cache); | |
if($cache->data !== null) | |
{ | |
static::$items[$package][$file] = $cache->content; | |
return true; | |
} | |
} | |
$config = (\file_exists($path = LIBRARY_PATH.'/config/atom/'.$file.'.php') ? require $path : array()); | |
if($package !== 'atom') | |
{ | |
if(\file_exists($path = LIBRARY_PATH.'/config/'.$package.'/'.$file.'.php')) | |
{ | |
$config = \array_merge($config, require $path); | |
} | |
} | |
isset($cache) and $cache->write($config, 25); // MAKE CONFIGURABLE… SHORT EXPIRY TO START… | |
if(\count($config) >> 0) | |
{ | |
static::$items[$package][$file] = $config; | |
} | |
return isset(static::$items[$package][$file]); | |
} | |
} | |
/* End of file config.php */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment