Files
spa-api/system/config.php

102 lines
2.5 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
*/
private static $items = array();
/**
* Get a configuration item.
*
* @param string $key
* @return mixed
*/
public static function get($key)
{
2011-06-14 17:27:11 -05:00
// -----------------------------------------------------
// Parse the key to separate the file and key name.
// -----------------------------------------------------
2011-06-08 23:45:08 -05:00
list($file, $key) = static::parse($key);
2011-06-14 17:27:11 -05:00
// -----------------------------------------------------
// Load the appropriate configuration file.
// -----------------------------------------------------
2011-06-08 23:45:08 -05:00
static::load($file);
return (array_key_exists($key, static::$items[$file])) ? static::$items[$file][$key] : null;
}
/**
* 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-06-14 17:27:11 -05:00
// -----------------------------------------------------
// Parse the key to separate the file and key name.
// -----------------------------------------------------
2011-06-08 23:45:08 -05:00
list($file, $key) = static::parse($key);
2011-06-14 17:27:11 -05:00
// -----------------------------------------------------
// Load the appropriate configuration file.
// -----------------------------------------------------
2011-06-08 23:45:08 -05:00
static::load($file);
static::$items[$file][$key] = $value;
}
/**
* Parse a configuration key.
*
* @param string $key
* @return array
*/
private static function parse($key)
{
$segments = explode('.', $key);
if (count($segments) < 2)
{
throw new \Exception("Invalid configuration key [$key].");
}
2011-06-14 17:27:11 -05:00
// -----------------------------------------------------
// The left side of the dot is the file name, while
// the right side of the dot is the item within that
// file being requested.
// -----------------------------------------------------
2011-06-08 23:45:08 -05:00
return array($segments[0], implode('.', array_slice($segments, 1)));
}
/**
* Load all of the configuration items.
*
* @param string $file
* @return void
*/
public static function load($file)
{
2011-06-14 17:27:11 -05:00
// -----------------------------------------------------
// If we have already loaded the file, bail out.
// -----------------------------------------------------
2011-06-08 23:45:08 -05:00
if (array_key_exists($file, static::$items))
{
return;
}
if ( ! file_exists($path = APP_PATH.'config/'.$file.EXT))
{
throw new \Exception("Configuration file [$file] does not exist.");
}
static::$items[$file] = require $path;
}
}