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.

66 lines
1.5 KiB

  1. <?php
  2. /**
  3. * @copyright NetMonsters <team@netmonsters.ru>
  4. * @link http://netmonsters.ru
  5. * @package Majestic
  6. * @subpackage Model
  7. * @since 2010-02-17
  8. * @version SVN: $Id$
  9. * @filesource $URL$
  10. */
  11. class Config extends ArrayObject
  12. {
  13. private static $_instance = null;
  14. public function __construct($config = array())
  15. {
  16. parent::__construct($config, ArrayObject::ARRAY_AS_PROPS);
  17. }
  18. private function __clone(){}
  19. /**
  20. * @return Config
  21. */
  22. static public function getInstance()
  23. {
  24. if (! isset(self::$_instance)) {
  25. self::$_instance = new Config();
  26. }
  27. return self::$_instance;
  28. }
  29. static public function get($name)
  30. {
  31. $instance = self::getInstance();
  32. if (! $instance->offsetExists($name)) {
  33. throw new Exception('Configuration variable "' . $name . '" undefined');
  34. }
  35. return $instance->offsetGet($name);
  36. }
  37. static public function set($name, $value)
  38. {
  39. if (is_array($value)) {
  40. $value = new ConfigArray($value);
  41. }
  42. self::getInstance()->offsetSet($name, $value);
  43. }
  44. }
  45. class ConfigArray extends ArrayObject
  46. {
  47. public function __construct($array)
  48. {
  49. parent::__construct($array, ArrayObject::ARRAY_AS_PROPS);
  50. }
  51. public function offsetGet($index)
  52. {
  53. if (! $this->offsetExists($index)) {
  54. throw new Exception('Configuration variable "' . $index . '" undefined');
  55. }
  56. return parent::offsetGet($index);
  57. }
  58. }