You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
67 lines
1.5 KiB
67 lines
1.5 KiB
<?php
|
|
/**
|
|
* @copyright NetMonsters <team@netmonsters.ru>
|
|
* @link http://netmonsters.ru
|
|
* @package Majestic
|
|
* @subpackage Model
|
|
* @since 2010-02-17
|
|
* @version SVN: $Id$
|
|
* @filesource $URL$
|
|
*/
|
|
|
|
class Config extends ArrayObject
|
|
{
|
|
|
|
private static $_instance = null;
|
|
|
|
public function __construct($config = array())
|
|
{
|
|
parent::__construct($config, ArrayObject::ARRAY_AS_PROPS);
|
|
}
|
|
|
|
private function __clone(){}
|
|
|
|
/**
|
|
* @return Config
|
|
*/
|
|
static public function getInstance()
|
|
{
|
|
if (!isset(self::$_instance)) {
|
|
self::$_instance = new Config();
|
|
}
|
|
return self::$_instance;
|
|
}
|
|
|
|
static public function get($name, $default = null)
|
|
{
|
|
$instance = self::getInstance();
|
|
if (!$instance->offsetExists($name)) {
|
|
return $default;
|
|
}
|
|
return $instance->offsetGet($name);
|
|
}
|
|
|
|
static public function set($name, $value)
|
|
{
|
|
if (is_array($value)) {
|
|
$value = new ConfigArray($value);
|
|
}
|
|
self::getInstance()->offsetSet($name, $value);
|
|
}
|
|
}
|
|
|
|
class ConfigArray extends ArrayObject
|
|
{
|
|
public function __construct($array)
|
|
{
|
|
parent::__construct($array, ArrayObject::ARRAY_AS_PROPS);
|
|
}
|
|
|
|
public function offsetGet($index)
|
|
{
|
|
if (!$this->offsetExists($index)) {
|
|
throw new Exception('Configuration variable "' . $index . '" undefined');
|
|
}
|
|
return parent::offsetGet($index);
|
|
}
|
|
}
|