Registry.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Registry
  17. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. * @version $Id$
  20. */
  21. /**
  22. * Generic storage class helps to manage global data.
  23. *
  24. * @category Zend
  25. * @package Zend_Registry
  26. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  27. * @license http://framework.zend.com/license/new-bsd New BSD License
  28. */
  29. class Zend_Registry extends ArrayObject
  30. {
  31. /**
  32. * Class name of the singleton registry object.
  33. * @var string
  34. */
  35. private static $_registryClassName = 'Zend_Registry';
  36. /**
  37. * Registry object provides storage for shared objects.
  38. * @var Zend_Registry
  39. */
  40. private static $_registry = null;
  41. /**
  42. * Retrieves the default registry instance.
  43. *
  44. * @return Zend_Registry
  45. */
  46. public static function getInstance()
  47. {
  48. if (self::$_registry === null) {
  49. self::init();
  50. }
  51. return self::$_registry;
  52. }
  53. /**
  54. * Set the default registry instance to a specified instance.
  55. *
  56. * @param Zend_Registry $registry An object instance of type Zend_Registry,
  57. * or a subclass.
  58. * @return void
  59. * @throws Zend_Exception if registry is already initialized.
  60. */
  61. public static function setInstance(Zend_Registry $registry)
  62. {
  63. if (self::$_registry !== null) {
  64. #require_once 'Zend/Exception.php';
  65. throw new Zend_Exception('Registry is already initialized');
  66. }
  67. self::setClassName(get_class($registry));
  68. self::$_registry = $registry;
  69. }
  70. /**
  71. * Initialize the default registry instance.
  72. *
  73. * @return void
  74. */
  75. protected static function init()
  76. {
  77. self::setInstance(new self::$_registryClassName());
  78. }
  79. /**
  80. * Set the class name to use for the default registry instance.
  81. * Does not affect the currently initialized instance, it only applies
  82. * for the next time you instantiate.
  83. *
  84. * @param string $registryClassName
  85. * @return void
  86. * @throws Zend_Exception if the registry is initialized or if the
  87. * class name is not valid.
  88. */
  89. public static function setClassName($registryClassName = 'Zend_Registry')
  90. {
  91. if (self::$_registry !== null) {
  92. #require_once 'Zend/Exception.php';
  93. throw new Zend_Exception('Registry is already initialized');
  94. }
  95. if (!is_string($registryClassName)) {
  96. #require_once 'Zend/Exception.php';
  97. throw new Zend_Exception("Argument is not a class name");
  98. }
  99. /**
  100. * @see Zend_Loader
  101. */
  102. if (!class_exists($registryClassName)) {
  103. #require_once 'Zend/Loader.php';
  104. Zend_Loader::loadClass($registryClassName);
  105. }
  106. self::$_registryClassName = $registryClassName;
  107. }
  108. /**
  109. * Unset the default registry instance.
  110. * Primarily used in tearDown() in unit tests.
  111. * @returns void
  112. */
  113. public static function _unsetInstance()
  114. {
  115. self::$_registry = null;
  116. }
  117. /**
  118. * getter method, basically same as offsetGet().
  119. *
  120. * This method can be called from an object of type Zend_Registry, or it
  121. * can be called statically. In the latter case, it uses the default
  122. * static instance stored in the class.
  123. *
  124. * @param string $index - get the value associated with $index
  125. * @return mixed
  126. * @throws Zend_Exception if no entry is registered for $index.
  127. */
  128. public static function get($index)
  129. {
  130. $instance = self::getInstance();
  131. if (!$instance->offsetExists($index)) {
  132. #require_once 'Zend/Exception.php';
  133. throw new Zend_Exception("No entry is registered for key '$index'");
  134. }
  135. return $instance->offsetGet($index);
  136. }
  137. /**
  138. * setter method, basically same as offsetSet().
  139. *
  140. * This method can be called from an object of type Zend_Registry, or it
  141. * can be called statically. In the latter case, it uses the default
  142. * static instance stored in the class.
  143. *
  144. * @param string $index The location in the ArrayObject in which to store
  145. * the value.
  146. * @param mixed $value The object to store in the ArrayObject.
  147. * @return void
  148. */
  149. public static function set($index, $value)
  150. {
  151. $instance = self::getInstance();
  152. $instance->offsetSet($index, $value);
  153. }
  154. /**
  155. * Returns TRUE if the $index is a named value in the registry,
  156. * or FALSE if $index was not found in the registry.
  157. *
  158. * @param string $index
  159. * @return boolean
  160. */
  161. public static function isRegistered($index)
  162. {
  163. if (self::$_registry === null) {
  164. return false;
  165. }
  166. return self::$_registry->offsetExists($index);
  167. }
  168. /**
  169. * Constructs a parent ArrayObject with default
  170. * ARRAY_AS_PROPS to allow acces as an object
  171. *
  172. * @param array $array data array
  173. * @param integer $flags ArrayObject flags
  174. */
  175. public function __construct($array = array(), $flags = parent::ARRAY_AS_PROPS)
  176. {
  177. parent::__construct($array, $flags);
  178. }
  179. /**
  180. * @param string $index
  181. * @returns mixed
  182. *
  183. * Workaround for http://bugs.php.net/bug.php?id=40442 (ZF-960).
  184. */
  185. public function offsetExists($index)
  186. {
  187. return array_key_exists($index, $this);
  188. }
  189. }