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.
16 * @package Zend_Registry
17 * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
18 * @license http://framework.zend.com/license/new-bsd New BSD License
23 * Generic storage class helps to manage global data.
26 * @package Zend_Registry
27 * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
28 * @license http://framework.zend.com/license/new-bsd New BSD License
30 class Zend_Registry
extends ArrayObject
33 * Class name of the singleton registry object.
36 private static $_registryClassName = 'Zend_Registry';
39 * Registry object provides storage for shared objects.
42 private static $_registry = null;
45 * Retrieves the default registry instance.
47 * @return Zend_Registry
49 public static function getInstance()
51 if (self
::$_registry === null) {
55 return self
::$_registry;
59 * Set the default registry instance to a specified instance.
61 * @param Zend_Registry $registry An object instance of type Zend_Registry,
64 * @throws Zend_Exception if registry is already initialized.
66 public static function setInstance(Zend_Registry
$registry)
68 if (self
::$_registry !== null) {
69 require_once 'Zend/Exception.php';
70 throw new Zend_Exception('Registry is already initialized');
73 self
::setClassName(get_class($registry));
74 self
::$_registry = $registry;
78 * Initialize the default registry instance.
82 protected static function init()
84 self
::setInstance(new self
::$_registryClassName());
88 * Set the class name to use for the default registry instance.
89 * Does not affect the currently initialized instance, it only applies
90 * for the next time you instantiate.
92 * @param string $registryClassName
94 * @throws Zend_Exception if the registry is initialized or if the
95 * class name is not valid.
97 public static function setClassName($registryClassName = 'Zend_Registry')
99 if (self
::$_registry !== null) {
100 require_once 'Zend/Exception.php';
101 throw new Zend_Exception('Registry is already initialized');
104 if (!is_string($registryClassName)) {
105 require_once 'Zend/Exception.php';
106 throw new Zend_Exception("Argument is not a class name");
112 if (!class_exists($registryClassName)) {
113 require_once 'Zend/Loader.php';
114 Zend_Loader
::loadClass($registryClassName);
117 self
::$_registryClassName = $registryClassName;
121 * Unset the default registry instance.
122 * Primarily used in tearDown() in unit tests.
125 public static function _unsetInstance()
127 self
::$_registry = null;
131 * getter method, basically same as offsetGet().
133 * This method can be called from an object of type Zend_Registry, or it
134 * can be called statically. In the latter case, it uses the default
135 * static instance stored in the class.
137 * @param string $index - get the value associated with $index
139 * @throws Zend_Exception if no entry is registerd for $index.
141 public static function get($index)
143 $instance = self
::getInstance();
145 if (!$instance->offsetExists($index)) {
146 require_once 'Zend/Exception.php';
147 throw new Zend_Exception("No entry is registered for key '$index'");
150 return $instance->offsetGet($index);
154 * setter method, basically same as offsetSet().
156 * This method can be called from an object of type Zend_Registry, or it
157 * can be called statically. In the latter case, it uses the default
158 * static instance stored in the class.
160 * @param string $index The location in the ArrayObject in which to store
162 * @param mixed $value The object to store in the ArrayObject.
165 public static function set($index, $value)
167 $instance = self
::getInstance();
168 $instance->offsetSet($index, $value);
172 * Returns TRUE if the $index is a named value in the registry,
173 * or FALSE if $index was not found in the registry.
175 * @param string $index
178 public static function isRegistered($index)
180 if (self
::$_registry === null) {
183 return self
::$_registry->offsetExists($index);
187 * Constructs a parent ArrayObject with default
188 * ARRAY_AS_PROPS to allow acces as an object
190 * @param array $array data array
191 * @param integer $flags ArrayObject flags
193 public function __construct($array = array(), $flags = parent
::ARRAY_AS_PROPS
)
195 parent
::__construct($array, $flags);
199 * @param string $index
202 * Workaround for http://bugs.php.net/bug.php?id=40442 (ZF-960).
204 public function offsetExists($index)
206 return array_key_exists($index, $this);