3 * The Zend Framework is covered by the new BSD Licence.
4 * This File is part of irBot and subject to the GPLv3
6 * Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
7 * Copyright (c) 2008 Bellière Ludovic
9 * This program is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation, either version 3 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24 * Generic storage class helps to manage global data. + Zend_Loader
27 * @package Zend_Registry
28 * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
29 * @copyright Copyright (c) 2008 Bellière Ludovic
30 * @license http://framework.zend.com/license/new-bsd New BSD License
31 * @license http://www.gnu.org/licenses/gpl-3.0.html
33 class Zend_Registry
extends ArrayObject
36 * Class name of the singleton registry object.
39 private static $_registryClassName = 'Zend_Registry';
42 * Registry object provides storage for shared objects.
45 private static $_registry = null;
48 * Retrieves the default registry instance.
50 * @return Zend_Registry
52 public static function getInstance()
54 if (self
::$_registry === null) {
58 return self
::$_registry;
62 * Set the default registry instance to a specified instance.
64 * @param Zend_Registry $registry An object instance of type Zend_Registry,
67 * @throws Exception if registry is already initialized.
69 public static function setInstance(Zend_Registry
$registry)
71 if (self
::$_registry !== null) {
72 throw new Exception('Registry is already initialized');
75 self
::setClassName(get_class($registry));
76 self
::$_registry = $registry;
80 * Initialize the default registry instance.
84 protected static function init()
86 self
::setInstance(new self
::$_registryClassName());
90 * Set the class name to use for the default registry instance.
91 * Does not affect the currently initialized instance, it only applies
92 * for the next time you instantiate.
94 * @param string $registryClassName
96 * @throws Exception if the registry is initialized or if the
97 * class name is not valid.
99 public static function setClassName($registryClassName = 'Zend_Registry')
101 if (self
::$_registry !== null) {
102 throw new Exception('Registry is already initialized');
105 if (!is_string($registryClassName)) {
106 throw new Exception("Argument is not a class name");
109 self
::loadClass($registryClassName);
111 self
::$_registryClassName = $registryClassName;
115 * Unset the default registry instance.
116 * Primarily used in tearDown() in unit tests.
119 public static function _unsetInstance()
121 self
::$_registry = null;
125 * getter method, basically same as offsetGet().
127 * This method can be called from an object of type Zend_Registry, or it
128 * can be called statically. In the latter case, it uses the default
129 * static instance stored in the class.
131 * @param string $index - get the value associated with $index
133 * @throws Exception if no entry is registerd for $index.
135 public static function get($index)
137 $instance = self
::getInstance();
139 if (!$instance->offsetExists($index)) {
140 throw new Exception("No entry is registered for key '$index'");
143 return $instance->offsetGet($index);
147 * setter method, basically same as offsetSet().
149 * This method can be called from an object of type Zend_Registry, or it
150 * can be called statically. In the latter case, it uses the default
151 * static instance stored in the class.
153 * @param string $index The location in the ArrayObject in which to store
155 * @param mixed $value The object to store in the ArrayObject.
158 public static function set($index, $value)
160 $instance = self
::getInstance();
161 $instance->offsetSet($index, $value);
165 * Returns TRUE if the $index is a named value in the registry,
166 * or FALSE if $index was not found in the registry.
168 * @param string $index
171 public static function isRegistered($index)
173 if (self
::$_registry === null) {
176 return self
::$_registry->offsetExists($index);
180 * @param string $index
183 * Workaround for http://bugs.php.net/bug.php?id=40442 (ZF-960).
185 public function offsetExists($index)
187 return array_key_exists($index, $this);
191 * Loads a class from a PHP file. The filename must be formatted
194 * If $dirs is a string or an array, it will search the directories
195 * in the order supplied, and attempt to load the first matching file.
197 * If $dirs is null, it will split the class name at underscores to
198 * generate a path hierarchy (e.g., "Zend_Example_Class" will map
199 * to "Zend/Example/Class.php").
201 * If the file was not found in the $dirs, or if no $dirs were specified,
202 * it will attempt to load it from PHP's include_path.
204 * @param string $class - The full class name of a Zend component.
205 * @param string|array $dirs - OPTIONAL Either a path or an array of paths
210 public static function loadClass($class, $dirs = null)
212 if (class_exists($class, false) ||
interface_exists($class, false)) {
216 if ((null !== $dirs) && !is_string($dirs) && !is_array($dirs)) {
217 throw new Exception('Directory argument must be a string or an array');
220 // autodiscover the path from the class name
221 $file = str_replace('_', DIRECTORY_SEPARATOR
, $class) . '.php';
223 // use the autodiscovered path
224 $dirPath = dirname($file);
225 if (is_string($dirs)) {
226 $dirs = explode(PATH_SEPARATOR
, $dirs);
228 foreach ($dirs as $key => $dir) {
230 $dirs[$key] = $dirPath;
232 $dir = rtrim($dir, '\\/');
233 $dirs[$key] = $dir . DIRECTORY_SEPARATOR
. $dirPath;
236 $file = basename($file);
237 self
::loadFile($file, $dirs, true);
239 self
::_securityCheck($file);
243 if (!class_exists($class, false) && !interface_exists($class, false)) {
244 throw new Exception("File \"$file\" was loaded but class \"$class\" was not found in the file");
249 * Loads a PHP file. This is a wrapper for PHP's include() function.
251 * $filename must be the complete filename, including any
252 * extension such as ".php". Note that a security check is performed that
253 * does not permit extended characters in the filename. This method is
254 * intended for loading Zend Framework files.
256 * If $dirs is a string or an array, it will search the directories
257 * in the order supplied, and attempt to load the first matching file.
259 * If the file was not found in the $dirs, or if no $dirs were specified,
260 * it will attempt to load it from PHP's include_path.
262 * If $once is TRUE, it will use include_once() instead of include().
264 * @param string $filename
265 * @param string|array $dirs - OPTIONAL either a path or array of paths
267 * @param boolean $once
269 * @throws Zend_Exception
271 public static function loadFile($filename, $dirs = null, $once = false)
273 self
::_securityCheck($filename);
276 * Search in provided directories, as well as include_path
279 if (!empty($dirs) && (is_array($dirs) ||
is_string($dirs))) {
280 if (is_array($dirs)) {
281 $dirs = implode(PATH_SEPARATOR
, $dirs);
283 $incPath = get_include_path();
284 set_include_path($dirs . PATH_SEPARATOR
. $incPath);
288 * Try finding for the plain filename in the include_path.
291 include_once $filename;
297 * If searching in directories, reset include_path
300 set_include_path($incPath);
307 * Returns TRUE if the $filename is readable, or FALSE otherwise.
308 * This function uses the PHP include_path, where PHP's is_readable()
311 * @param string $filename
314 public static function isReadable($filename)
316 if (!$fh = @fopen
($filename, 'r', true)) {
324 * spl_autoload() suitable implementation for supporting class autoloading.
326 * Attach to spl_autoload() using the following:
328 * spl_autoload_register(array('Zend_Loader', 'autoload'));
331 * @param string $class
332 * @return string|false Class name on success; false on failure
334 public static function autoload($class)
337 self
::loadClass($class);
339 } catch (Exception
$e) {
345 * Register {@link autoload()} with spl_autoload()
347 * @param string $class (optional)
348 * @param boolean $enabled (optional)
350 * @throws Exception if spl_autoload() is not found
351 * or if the specified class does not have an autoload() method.
353 public static function registerAutoload($class = 'Zend_Registry', $enabled = true)
355 if (!function_exists('spl_autoload_register')) {
356 throw new Exception('spl_autoload does not exist in this PHP installation');
359 self
::loadClass($class);
360 $methods = get_class_methods($class);
361 if (!in_array('autoload', (array) $methods)) {
362 throw new Exception("The class \"$class\" does not have an autoload() method");
365 if ($enabled === true) {
366 spl_autoload_register(array($class, 'autoload'));
368 spl_autoload_unregister(array($class, 'autoload'));
373 * Ensure that filename does not contain exploits
375 * @param string $filename
377 * @throws Zend_Exception
379 protected static function _securityCheck($filename)
384 if (preg_match('/[^a-z0-9\\/\\\\_.-]/i', $filename)) {
385 throw new Exception('Security check: Illegal character in filename');
390 * Attempt to include() the file.
392 * include() is not prefixed with the @ operator because if
393 * the file is loaded and contains a parse error, execution
394 * will halt silently and this is difficult to debug.
396 * Always set display_errors = Off on production servers!
398 * @param string $filespec
399 * @param boolean $once
401 * @deprecated Since 1.5.0; use loadFile() instead
403 protected static function _includeFile($filespec, $once = false)
406 return include_once $filespec;
408 return include $filespec ;