File indexing completed on 2024-12-22 05:37:17
0001 <?php 0002 /** 0003 * Zend Framework 0004 * 0005 * LICENSE 0006 * 0007 * This source file is subject to the new BSD license that is bundled 0008 * with this package in the file LICENSE.txt. 0009 * It is also available through the world-wide-web at this URL: 0010 * http://framework.zend.com/license/new-bsd 0011 * If you did not receive a copy of the license and are unable to 0012 * obtain it through the world-wide-web, please send an email 0013 * to license@zend.com so we can send you a copy immediately. 0014 * 0015 * @category Zend 0016 * @package Zend_Registry 0017 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) 0018 * @license http://framework.zend.com/license/new-bsd New BSD License 0019 * @version $Id$ 0020 */ 0021 0022 /** 0023 * Generic storage class helps to manage global data. 0024 * 0025 * @category Zend 0026 * @package Zend_Registry 0027 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) 0028 * @license http://framework.zend.com/license/new-bsd New BSD License 0029 */ 0030 class Zend_Registry extends ArrayObject 0031 { 0032 /** 0033 * Class name of the singleton registry object. 0034 * @var string 0035 */ 0036 private static $_registryClassName = 'Zend_Registry'; 0037 0038 /** 0039 * Registry object provides storage for shared objects. 0040 * @var Zend_Registry 0041 */ 0042 private static $_registry = null; 0043 0044 /** 0045 * Retrieves the default registry instance. 0046 * 0047 * @return Zend_Registry 0048 */ 0049 public static function getInstance() 0050 { 0051 if (self::$_registry === null) { 0052 self::init(); 0053 } 0054 0055 return self::$_registry; 0056 } 0057 0058 /** 0059 * Set the default registry instance to a specified instance. 0060 * 0061 * @param Zend_Registry $registry An object instance of type Zend_Registry, 0062 * or a subclass. 0063 * @return void 0064 * @throws Zend_Exception if registry is already initialized. 0065 */ 0066 public static function setInstance(Zend_Registry $registry) 0067 { 0068 if (self::$_registry !== null) { 0069 // require_once 'Zend/Exception.php'; 0070 throw new Zend_Exception('Registry is already initialized'); 0071 } 0072 0073 self::setClassName(get_class($registry)); 0074 self::$_registry = $registry; 0075 } 0076 0077 /** 0078 * Initialize the default registry instance. 0079 * 0080 * @return void 0081 */ 0082 protected static function init() 0083 { 0084 self::setInstance(new self::$_registryClassName()); 0085 } 0086 0087 /** 0088 * Set the class name to use for the default registry instance. 0089 * Does not affect the currently initialized instance, it only applies 0090 * for the next time you instantiate. 0091 * 0092 * @param string $registryClassName 0093 * @return void 0094 * @throws Zend_Exception if the registry is initialized or if the 0095 * class name is not valid. 0096 */ 0097 public static function setClassName($registryClassName = 'Zend_Registry') 0098 { 0099 if (self::$_registry !== null) { 0100 // require_once 'Zend/Exception.php'; 0101 throw new Zend_Exception('Registry is already initialized'); 0102 } 0103 0104 if (!is_string($registryClassName)) { 0105 // require_once 'Zend/Exception.php'; 0106 throw new Zend_Exception("Argument is not a class name"); 0107 } 0108 0109 /** 0110 * @see Zend_Loader 0111 */ 0112 if (!class_exists($registryClassName)) { 0113 // require_once 'Zend/Loader.php'; 0114 Zend_Loader::loadClass($registryClassName); 0115 } 0116 0117 self::$_registryClassName = $registryClassName; 0118 } 0119 0120 /** 0121 * Unset the default registry instance. 0122 * Primarily used in tearDown() in unit tests. 0123 * @returns void 0124 */ 0125 public static function _unsetInstance() 0126 { 0127 self::$_registry = null; 0128 } 0129 0130 /** 0131 * getter method, basically same as offsetGet(). 0132 * 0133 * This method can be called from an object of type Zend_Registry, or it 0134 * can be called statically. In the latter case, it uses the default 0135 * static instance stored in the class. 0136 * 0137 * @param string $index - get the value associated with $index 0138 * @return mixed 0139 * @throws Zend_Exception if no entry is registered for $index. 0140 */ 0141 public static function get($index) 0142 { 0143 $instance = self::getInstance(); 0144 0145 if (!$instance->offsetExists($index)) { 0146 // require_once 'Zend/Exception.php'; 0147 throw new Zend_Exception("No entry is registered for key '$index'"); 0148 } 0149 0150 return $instance->offsetGet($index); 0151 } 0152 0153 /** 0154 * setter method, basically same as offsetSet(). 0155 * 0156 * This method can be called from an object of type Zend_Registry, or it 0157 * can be called statically. In the latter case, it uses the default 0158 * static instance stored in the class. 0159 * 0160 * @param string $index The location in the ArrayObject in which to store 0161 * the value. 0162 * @param mixed $value The object to store in the ArrayObject. 0163 * @return void 0164 */ 0165 public static function set($index, $value) 0166 { 0167 $instance = self::getInstance(); 0168 $instance->offsetSet($index, $value); 0169 } 0170 0171 /** 0172 * Returns TRUE if the $index is a named value in the registry, 0173 * or FALSE if $index was not found in the registry. 0174 * 0175 * @param string $index 0176 * @return boolean 0177 */ 0178 public static function isRegistered($index) 0179 { 0180 if (self::$_registry === null) { 0181 return false; 0182 } 0183 return self::$_registry->offsetExists($index); 0184 } 0185 0186 /** 0187 * Constructs a parent ArrayObject with default 0188 * ARRAY_AS_PROPS to allow acces as an object 0189 * 0190 * @param array $array data array 0191 * @param integer $flags ArrayObject flags 0192 */ 0193 public function __construct($array = array(), $flags = parent::ARRAY_AS_PROPS) 0194 { 0195 parent::__construct($array, $flags); 0196 } 0197 0198 /** 0199 * @param string $index 0200 * @returns mixed 0201 * 0202 * Workaround for http://bugs.php.net/bug.php?id=40442 (ZF-960). 0203 */ 0204 public function offsetExists($index) 0205 { 0206 return array_key_exists($index, $this); 0207 } 0208 0209 }