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-2010 Zend Technologies USA Inc. (http://www.zend.com) |
---|
18 | * @license http://framework.zend.com/license/new-bsd New BSD License |
---|
19 | * @version $Id: Registry.php 20096 2010-01-06 02:05:09Z bkarwin $ |
---|
20 | */ |
---|
21 | |
---|
22 | /** |
---|
23 | * Generic storage class helps to manage global data. |
---|
24 | * |
---|
25 | * @category Zend |
---|
26 | * @package Zend_Registry |
---|
27 | * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) |
---|
28 | * @license http://framework.zend.com/license/new-bsd New BSD License |
---|
29 | */ |
---|
30 | class Zend_Registry extends ArrayObject |
---|
31 | { |
---|
32 | /** |
---|
33 | * Class name of the singleton registry object. |
---|
34 | * @var string |
---|
35 | */ |
---|
36 | private static $_registryClassName = 'Zend_Registry'; |
---|
37 | |
---|
38 | /** |
---|
39 | * Registry object provides storage for shared objects. |
---|
40 | * @var Zend_Registry |
---|
41 | */ |
---|
42 | private static $_registry = null; |
---|
43 | |
---|
44 | /** |
---|
45 | * Retrieves the default registry instance. |
---|
46 | * |
---|
47 | * @return Zend_Registry |
---|
48 | */ |
---|
49 | public static function getInstance() |
---|
50 | { |
---|
51 | if (self::$_registry === null) { |
---|
52 | self::init(); |
---|
53 | } |
---|
54 | |
---|
55 | return self::$_registry; |
---|
56 | } |
---|
57 | |
---|
58 | /** |
---|
59 | * Set the default registry instance to a specified instance. |
---|
60 | * |
---|
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. |
---|
65 | */ |
---|
66 | public static function setInstance(Zend_Registry $registry) |
---|
67 | { |
---|
68 | if (self::$_registry !== null) { |
---|
69 | require_once 'Zend/Exception.php'; |
---|
70 | throw new Zend_Exception('Registry is already initialized'); |
---|
71 | } |
---|
72 | |
---|
73 | self::setClassName(get_class($registry)); |
---|
74 | self::$_registry = $registry; |
---|
75 | } |
---|
76 | |
---|
77 | /** |
---|
78 | * Initialize the default registry instance. |
---|
79 | * |
---|
80 | * @return void |
---|
81 | */ |
---|
82 | protected static function init() |
---|
83 | { |
---|
84 | self::setInstance(new self::$_registryClassName()); |
---|
85 | } |
---|
86 | |
---|
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. |
---|
91 | * |
---|
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. |
---|
96 | */ |
---|
97 | public static function setClassName($registryClassName = 'Zend_Registry') |
---|
98 | { |
---|
99 | if (self::$_registry !== null) { |
---|
100 | require_once 'Zend/Exception.php'; |
---|
101 | throw new Zend_Exception('Registry is already initialized'); |
---|
102 | } |
---|
103 | |
---|
104 | if (!is_string($registryClassName)) { |
---|
105 | require_once 'Zend/Exception.php'; |
---|
106 | throw new Zend_Exception("Argument is not a class name"); |
---|
107 | } |
---|
108 | |
---|
109 | /** |
---|
110 | * @see Zend_Loader |
---|
111 | */ |
---|
112 | if (!class_exists($registryClassName)) { |
---|
113 | require_once 'Zend/Loader.php'; |
---|
114 | Zend_Loader::loadClass($registryClassName); |
---|
115 | } |
---|
116 | |
---|
117 | self::$_registryClassName = $registryClassName; |
---|
118 | } |
---|
119 | |
---|
120 | /** |
---|
121 | * Unset the default registry instance. |
---|
122 | * Primarily used in tearDown() in unit tests. |
---|
123 | * @returns void |
---|
124 | */ |
---|
125 | public static function _unsetInstance() |
---|
126 | { |
---|
127 | self::$_registry = null; |
---|
128 | } |
---|
129 | |
---|
130 | /** |
---|
131 | * getter method, basically same as offsetGet(). |
---|
132 | * |
---|
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. |
---|
136 | * |
---|
137 | * @param string $index - get the value associated with $index |
---|
138 | * @return mixed |
---|
139 | * @throws Zend_Exception if no entry is registerd for $index. |
---|
140 | */ |
---|
141 | public static function get($index) |
---|
142 | { |
---|
143 | $instance = self::getInstance(); |
---|
144 | |
---|
145 | if (!$instance->offsetExists($index)) { |
---|
146 | require_once 'Zend/Exception.php'; |
---|
147 | throw new Zend_Exception("No entry is registered for key '$index'"); |
---|
148 | } |
---|
149 | |
---|
150 | return $instance->offsetGet($index); |
---|
151 | } |
---|
152 | |
---|
153 | /** |
---|
154 | * setter method, basically same as offsetSet(). |
---|
155 | * |
---|
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. |
---|
159 | * |
---|
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 |
---|
164 | */ |
---|
165 | public static function set($index, $value) |
---|
166 | { |
---|
167 | $instance = self::getInstance(); |
---|
168 | $instance->offsetSet($index, $value); |
---|
169 | } |
---|
170 | |
---|
171 | /** |
---|
172 | * Returns TRUE if the $index is a named value in the registry, |
---|
173 | * or FALSE if $index was not found in the registry. |
---|
174 | * |
---|
175 | * @param string $index |
---|
176 | * @return boolean |
---|
177 | */ |
---|
178 | public static function isRegistered($index) |
---|
179 | { |
---|
180 | if (self::$_registry === null) { |
---|
181 | return false; |
---|
182 | } |
---|
183 | return self::$_registry->offsetExists($index); |
---|
184 | } |
---|
185 | |
---|
186 | /** |
---|
187 | * Constructs a parent ArrayObject with default |
---|
188 | * ARRAY_AS_PROPS to allow acces as an object |
---|
189 | * |
---|
190 | * @param array $array data array |
---|
191 | * @param integer $flags ArrayObject flags |
---|
192 | */ |
---|
193 | public function __construct($array = array(), $flags = parent::ARRAY_AS_PROPS) |
---|
194 | { |
---|
195 | parent::__construct($array, $flags); |
---|
196 | } |
---|
197 | |
---|
198 | /** |
---|
199 | * @param string $index |
---|
200 | * @returns mixed |
---|
201 | * |
---|
202 | * Workaround for http://bugs.php.net/bug.php?id=40442 (ZF-960). |
---|
203 | */ |
---|
204 | public function offsetExists($index) |
---|
205 | { |
---|
206 | return array_key_exists($index, $this); |
---|
207 | } |
---|
208 | |
---|
209 | } |
---|