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. /**
  19. * @return Config
  20. */
  21. static public function getInstance()
  22. {
  23. if (is_null(self::$_instance)) {
  24. self::$_instance = new Config();
  25. }
  26. return self::$_instance;
  27. }
  28. static public function get($name)
  29. {
  30. $instance = self::getInstance();
  31. if (! $instance->offsetExists($name)) {
  32. throw new Exception('Configuration variable "' . $name . '" undefined');
  33. }
  34. return $instance->offsetGet($name);
  35. }
  36. static public function set($name, $value)
  37. {
  38. if (is_array($value)) {
  39. $value = new ConfigArray($value);
  40. }
  41. self::getInstance()->offsetSet($name, $value);
  42. }
  43. }
  44. class ConfigArray extends ArrayObject
  45. {
  46. public function __construct($array)
  47. {
  48. parent::__construct($array, ArrayObject::ARRAY_AS_PROPS);
  49. }
  50. public function offsetGet($index)
  51. {
  52. if (! $this->offsetExists($index)) {
  53. throw new Exception('Configuration variable "' . $index . '" undefined');
  54. }
  55. return parent::offsetGet($index);
  56. }
  57. }