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.class.php 234 2012-02-02 13:23:06Z oroger $
20: */
21: namespace Pry\Util;
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 Registry extends \ArrayObject
31: {
32: /**
33: * Class name of the singleton registry object.
34: * @var string
35: */
36: private static $_registryClassName = 'Pry\Util\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 Exception if registry is already initialized.
65: */
66: public static function setInstance(Registry $registry)
67: {
68: if (self::$_registry !== null) {
69: throw new Exception('Registry is already initialized');
70: }
71:
72: self::setClassName(get_class($registry));
73: self::$_registry = $registry;
74: }
75:
76: /**
77: * Initialize the default registry instance.
78: *
79: * @return void
80: */
81: protected static function init()
82: {
83: self::setInstance(new self::$_registryClassName());
84: }
85:
86: /**
87: * Set the class name to use for the default registry instance.
88: * Does not affect the currently initialized instance, it only applies
89: * for the next time you instantiate.
90: *
91: * @param string $registryClassName
92: * @return void
93: * @throws Exception if the registry is initialized or if the
94: * class name is not valid.
95: */
96: public static function setClassName($registryClassName = 'Registry')
97: {
98: if (self::$_registry !== null) {
99: throw new Exception('Registry is already initialized');
100: }
101:
102: if (!is_string($registryClassName)) {
103: throw new Exception("Argument is not a class name");
104: }
105:
106: self::$_registryClassName = $registryClassName;
107: }
108:
109: /**
110: * Unset the default registry instance.
111: * Primarily used in tearDown() in unit tests.
112: * @returns void
113: */
114: public static function _unsetInstance()
115: {
116: self::$_registry = null;
117: }
118:
119: /**
120: * getter method, basically same as offsetGet().
121: *
122: * This method can be called from an object of type Zend_Registry, or it
123: * can be called statically. In the latter case, it uses the default
124: * static instance stored in the class.
125: *
126: * @param string $index - get the value associated with $index
127: * @return mixed
128: * @throws OutOfBoundsException if no entry is registerd for $index.
129: */
130: public static function get($index)
131: {
132: $instance = self::getInstance();
133:
134: if (!$instance->offsetExists($index)) {
135: throw new OutOfBoundsException("No entry is registered for key '$index'");
136: }
137:
138: return $instance->offsetGet($index);
139: }
140:
141: /**
142: * setter method, basically same as offsetSet().
143: *
144: * This method can be called from an object of type Zend_Registry, or it
145: * can be called statically. In the latter case, it uses the default
146: * static instance stored in the class.
147: *
148: * @param string $index The location in the ArrayObject in which to store
149: * the value.
150: * @param mixed $value The object to store in the ArrayObject.
151: * @return void
152: */
153: public static function set($index, $value)
154: {
155: $instance = self::getInstance();
156: $instance->offsetSet($index, $value);
157: }
158:
159: /**
160: * Returns TRUE if the $index is a named value in the registry,
161: * or FALSE if $index was not found in the registry.
162: *
163: * @param string $index
164: * @return boolean
165: */
166: public static function isRegistered($index)
167: {
168: if (self::$_registry === null) {
169: return false;
170: }
171: return self::$_registry->offsetExists($index);
172: }
173:
174: /**
175: * Constructs a parent ArrayObject with default
176: * ARRAY_AS_PROPS to allow acces as an object
177: *
178: * @param array $array data array
179: * @param integer $flags ArrayObject flags
180: */
181: public function __construct($array = array(), $flags = parent::ARRAY_AS_PROPS)
182: {
183: parent::__construct($array, $flags);
184: }
185:
186: /**
187: * @param string $index
188: * @returns mixed
189: *
190: * Workaround for http://bugs.php.net/bug.php?id=40442 (ZF-960).
191: */
192: public function offsetExists($index)
193: {
194: return array_key_exists($index, $this);
195: }
196:
197: }
198: