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-2008 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 12065 2008-10-21 20:56:32Z doctorrock83 $
|
---|
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-2008 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 | require_once 'Zend/Loader.php';
|
---|
113 | Zend_Loader::loadClass($registryClassName);
|
---|
114 |
|
---|
115 | self::$_registryClassName = $registryClassName;
|
---|
116 | }
|
---|
117 |
|
---|
118 | /**
|
---|
119 | * Unset the default registry instance.
|
---|
120 | * Primarily used in tearDown() in unit tests.
|
---|
121 | * @returns void
|
---|
122 | */
|
---|
123 | public static function _unsetInstance()
|
---|
124 | {
|
---|
125 | self::$_registry = null;
|
---|
126 | }
|
---|
127 |
|
---|
128 | /**
|
---|
129 | * getter method, basically same as offsetGet().
|
---|
130 | *
|
---|
131 | * This method can be called from an object of type Zend_Registry, or it
|
---|
132 | * can be called statically. In the latter case, it uses the default
|
---|
133 | * static instance stored in the class.
|
---|
134 | *
|
---|
135 | * @param string $index - get the value associated with $index
|
---|
136 | * @return mixed
|
---|
137 | * @throws Zend_Exception if no entry is registerd for $index.
|
---|
138 | */
|
---|
139 | public static function get($index)
|
---|
140 | {
|
---|
141 | $instance = self::getInstance();
|
---|
142 |
|
---|
143 | if (!$instance->offsetExists($index)) {
|
---|
144 | require_once 'Zend/Exception.php';
|
---|
145 | throw new Zend_Exception("No entry is registered for key '$index'");
|
---|
146 | }
|
---|
147 |
|
---|
148 | return $instance->offsetGet($index);
|
---|
149 | }
|
---|
150 |
|
---|
151 | /**
|
---|
152 | * setter method, basically same as offsetSet().
|
---|
153 | *
|
---|
154 | * This method can be called from an object of type Zend_Registry, or it
|
---|
155 | * can be called statically. In the latter case, it uses the default
|
---|
156 | * static instance stored in the class.
|
---|
157 | *
|
---|
158 | * @param string $index The location in the ArrayObject in which to store
|
---|
159 | * the value.
|
---|
160 | * @param mixed $value The object to store in the ArrayObject.
|
---|
161 | * @return void
|
---|
162 | */
|
---|
163 | public static function set($index, $value)
|
---|
164 | {
|
---|
165 | $instance = self::getInstance();
|
---|
166 | $instance->offsetSet($index, $value);
|
---|
167 | }
|
---|
168 |
|
---|
169 | /**
|
---|
170 | * Returns TRUE if the $index is a named value in the registry,
|
---|
171 | * or FALSE if $index was not found in the registry.
|
---|
172 | *
|
---|
173 | * @param string $index
|
---|
174 | * @return boolean
|
---|
175 | */
|
---|
176 | public static function isRegistered($index)
|
---|
177 | {
|
---|
178 | if (self::$_registry === null) {
|
---|
179 | return false;
|
---|
180 | }
|
---|
181 | return self::$_registry->offsetExists($index);
|
---|
182 | }
|
---|
183 |
|
---|
184 | /**
|
---|
185 | * Constructs a parent ArrayObject with default
|
---|
186 | * ARRAY_AS_PROPS to allow acces as an object
|
---|
187 | *
|
---|
188 | * @param array $array data array
|
---|
189 | * @param integer $flags ArrayObject flags
|
---|
190 | */
|
---|
191 | public function __construct($array = array(), $flags = parent::ARRAY_AS_PROPS)
|
---|
192 | {
|
---|
193 | parent::__construct($array, $flags);
|
---|
194 | }
|
---|
195 |
|
---|
196 | /**
|
---|
197 | * @param string $index
|
---|
198 | * @returns mixed
|
---|
199 | *
|
---|
200 | * Workaround for http://bugs.php.net/bug.php?id=40442 (ZF-960).
|
---|
201 | */
|
---|
202 | public function offsetExists($index)
|
---|
203 | {
|
---|
204 | return array_key_exists($index, $this);
|
---|
205 | }
|
---|
206 |
|
---|
207 | }
|
---|