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.

72 lines
1.7 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. */
  9. class Registry extends ArrayObject
  10. {
  11. /**
  12. * Class name of the singleton registry object.
  13. * @var string
  14. */
  15. private static $_class_name = 'Registry';
  16. /**
  17. * Registry object provides storage for shared objects.
  18. * @var Registry
  19. */
  20. private static $_registry = null;
  21. /**
  22. * Constructs a parent ArrayObject with default
  23. * ARRAY_AS_PROPS to allow acces as an object
  24. *
  25. * @param array $array data array
  26. * @param integer $flags ArrayObject flags
  27. */
  28. public function __construct($array = array(), $flags = parent::ARRAY_AS_PROPS)
  29. {
  30. parent::__construct($array, $flags);
  31. }
  32. private function __clone(){}
  33. /**
  34. * Retrieves the default registry instance.
  35. *
  36. * @return Registry
  37. */
  38. public static function getInstance()
  39. {
  40. if (self::$_registry === null) {
  41. self::$_registry = new self::$_class_name();
  42. }
  43. return self::$_registry;
  44. }
  45. static public function get($name, $default = null)
  46. {
  47. $instance = self::getInstance();
  48. if (!$instance->offsetExists($name)) {
  49. return $default;
  50. }
  51. return $instance->offsetGet($name);
  52. }
  53. public static function set($index, $value)
  54. {
  55. $instance = self::getInstance();
  56. $instance->offsetSet($index, $value);
  57. }
  58. public static function isRegistered($index)
  59. {
  60. if (self::$_registry === null) {
  61. return false;
  62. }
  63. return self::$_registry->offsetExists($index);
  64. }
  65. }