[ZF-6295] Generic:
[zend.git] / library / Zend / Registry.php
blob3c3cf496523c6ce61c2c104c3e3821c4a395571f
1 <?php
2 /**
3 * Zend Framework
5 * LICENSE
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.
15 * @category Zend
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
19 * @version $Id$
22 /**
23 * Generic storage class helps to manage global data.
25 * @category Zend
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
32 /**
33 * Class name of the singleton registry object.
34 * @var string
36 private static $_registryClassName = 'Zend_Registry';
38 /**
39 * Registry object provides storage for shared objects.
40 * @var Zend_Registry
42 private static $_registry = null;
44 /**
45 * Retrieves the default registry instance.
47 * @return Zend_Registry
49 public static function getInstance()
51 if (self::$_registry === null) {
52 self::init();
55 return self::$_registry;
58 /**
59 * Set the default registry instance to a specified instance.
61 * @param Zend_Registry $registry An object instance of type Zend_Registry,
62 * or a subclass.
63 * @return void
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;
77 /**
78 * Initialize the default registry instance.
80 * @return void
82 protected static function init()
84 self::setInstance(new self::$_registryClassName());
87 /**
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
93 * @return void
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");
110 * @see Zend_Loader
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.
123 * @returns void
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
138 * @return mixed
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
161 * the value.
162 * @param mixed $value The object to store in the ArrayObject.
163 * @return void
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
176 * @return boolean
178 public static function isRegistered($index)
180 if (self::$_registry === null) {
181 return false;
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
200 * @returns mixed
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);