fix a change name bug
[irbot.git] / sources / Registry.php
blob8bcd5e19744d8bb65c04dafce44e51d7410d2dee
1 <?php
2 /**
3 * The Zend Framework is covered by the new BSD Licence.
4 * This File is part of irBot and subject to the GPLv3
5 *
6 * Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
7 * Copyright (c) 2008 Bellière Ludovic
8 *
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/>.
22 * @category Zend
23 * @package Zend_Registry
24 * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
25 * @copyright Copyright (c) 2008 Bellière Ludovic
26 * @license http://framework.zend.com/license/new-bsd New BSD License
27 * @license http://www.gnu.org/licenses/gpl-3.0.html
30 /**
31 * Generic storage class helps to manage global data. + Zend_Loader
33 * @category Zend
34 * @package Zend_Registry
35 * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
36 * @copyright Copyright (c) 2008 Bellière Ludovic
37 * @license http://framework.zend.com/license/new-bsd New BSD License
38 * @license http://www.gnu.org/licenses/gpl-3.0.html
40 class Zend_Registry extends ArrayObject
42 /**
43 * Class name of the singleton registry object.
44 * @var string
46 private static $_registryClassName = 'Zend_Registry';
48 /**
49 * Registry object provides storage for shared objects.
50 * @var Zend_Registry
52 private static $_registry = null;
54 /**
55 * Retrieves the default registry instance.
57 * @return Zend_Registry
59 public static function getInstance()
61 if (self::$_registry === null) {
62 self::init();
65 return self::$_registry;
68 /**
69 * Set the default registry instance to a specified instance.
71 * @param Zend_Registry $registry An object instance of type Zend_Registry,
72 * or a subclass.
73 * @return void
74 * @throws Exception if registry is already initialized.
76 public static function setInstance(Zend_Registry $registry)
78 if (self::$_registry !== null) {
79 throw new Exception('Registry is already initialized');
82 self::setClassName(get_class($registry));
83 self::$_registry = $registry;
86 /**
87 * Initialize the default registry instance.
89 * @return void
91 protected static function init()
93 self::setInstance(new self::$_registryClassName());
96 /**
97 * Set the class name to use for the default registry instance.
98 * Does not affect the currently initialized instance, it only applies
99 * for the next time you instantiate.
101 * @param string $registryClassName
102 * @return void
103 * @throws Exception if the registry is initialized or if the
104 * class name is not valid.
106 public static function setClassName($registryClassName = 'Zend_Registry')
108 if (self::$_registry !== null) {
109 throw new Exception('Registry is already initialized');
112 if (!is_string($registryClassName)) {
113 throw new Exception("Argument is not a class name");
116 self::loadClass($registryClassName);
118 self::$_registryClassName = $registryClassName;
122 * Unset the default registry instance.
123 * Primarily used in tearDown() in unit tests.
124 * @returns void
126 public static function _unsetInstance()
128 self::$_registry = null;
132 * getter method, basically same as offsetGet().
134 * This method can be called from an object of type Zend_Registry, or it
135 * can be called statically. In the latter case, it uses the default
136 * static instance stored in the class.
138 * @param string $index - get the value associated with $index
139 * @return mixed
140 * @throws Exception if no entry is registerd for $index.
142 public static function get($index)
144 $instance = self::getInstance();
146 if (!$instance->offsetExists($index)) {
147 throw new Exception("No entry is registered for key '$index'");
150 return $instance->offsetGet($index);
154 * setter method, basically same as offsetSet().
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.
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
165 public static function set($index, $value)
167 $instance = self::getInstance();
168 $instance->offsetSet($index, $value);
172 * Returns TRUE if the $index is a named value in the registry,
173 * or FALSE if $index was not found in the registry.
175 * @param string $index
176 * @return boolean
178 public static function isRegistered($index)
180 if (self::$_registry === null) {
181 return false;
183 return self::$_registry->offsetExists($index);
187 * @param string $index
188 * @returns mixed
190 * Workaround for http://bugs.php.net/bug.php?id=40442 (ZF-960).
192 public function offsetExists($index)
194 return array_key_exists($index, $this);
198 * Loads a class from a PHP file. The filename must be formatted
199 * as "$class.php".
201 * If $dirs is a string or an array, it will search the directories
202 * in the order supplied, and attempt to load the first matching file.
204 * If $dirs is null, it will split the class name at underscores to
205 * generate a path hierarchy (e.g., "Zend_Example_Class" will map
206 * to "Zend/Example/Class.php").
208 * If the file was not found in the $dirs, or if no $dirs were specified,
209 * it will attempt to load it from PHP's include_path.
211 * @param string $class - The full class name of a Zend component.
212 * @param string|array $dirs - OPTIONAL Either a path or an array of paths
213 * to search.
214 * @return void
215 * @throws Exception
217 public static function loadClass($class, $dirs = null)
219 if (class_exists($class, false) || interface_exists($class, false)) {
220 return;
223 if ((null !== $dirs) && !is_string($dirs) && !is_array($dirs)) {
224 throw new Exception('Directory argument must be a string or an array');
227 // autodiscover the path from the class name
228 $file = str_replace('_', DIRECTORY_SEPARATOR, $class) . '.php';
229 if (!empty($dirs)) {
230 // use the autodiscovered path
231 $dirPath = dirname($file);
232 if (is_string($dirs)) {
233 $dirs = explode(PATH_SEPARATOR, $dirs);
235 foreach ($dirs as $key => $dir) {
236 if ($dir == '.') {
237 $dirs[$key] = $dirPath;
238 } else {
239 $dir = rtrim($dir, '\\/');
240 $dirs[$key] = $dir . DIRECTORY_SEPARATOR . $dirPath;
243 $file = basename($file);
244 self::loadFile($file, $dirs, true);
245 } else {
246 self::_securityCheck($file);
247 include_once $file;
250 if (!class_exists($class, false) && !interface_exists($class, false)) {
251 throw new Exception("File \"$file\" was loaded but class \"$class\" was not found in the file");
256 * Loads a PHP file. This is a wrapper for PHP's include() function.
258 * $filename must be the complete filename, including any
259 * extension such as ".php". Note that a security check is performed that
260 * does not permit extended characters in the filename. This method is
261 * intended for loading Zend Framework files.
263 * If $dirs is a string or an array, it will search the directories
264 * in the order supplied, and attempt to load the first matching file.
266 * If the file was not found in the $dirs, or if no $dirs were specified,
267 * it will attempt to load it from PHP's include_path.
269 * If $once is TRUE, it will use include_once() instead of include().
271 * @param string $filename
272 * @param string|array $dirs - OPTIONAL either a path or array of paths
273 * to search.
274 * @param boolean $once
275 * @return boolean
276 * @throws Zend_Exception
278 public static function loadFile($filename, $dirs = null, $once = false)
280 self::_securityCheck($filename);
283 * Search in provided directories, as well as include_path
285 $incPath = false;
286 if (!empty($dirs) && (is_array($dirs) || is_string($dirs))) {
287 if (is_array($dirs)) {
288 $dirs = implode(PATH_SEPARATOR, $dirs);
290 $incPath = get_include_path();
291 set_include_path($dirs . PATH_SEPARATOR . $incPath);
295 * Try finding for the plain filename in the include_path.
297 if ($once) {
298 include_once $filename;
299 } else {
300 include $filename;
304 * If searching in directories, reset include_path
306 if ($incPath) {
307 set_include_path($incPath);
310 return true;
314 * Returns TRUE if the $filename is readable, or FALSE otherwise.
315 * This function uses the PHP include_path, where PHP's is_readable()
316 * does not.
318 * @param string $filename
319 * @return boolean
321 public static function isReadable($filename)
323 if (!$fh = @fopen($filename, 'r', true)) {
324 return false;
327 return true;
331 * spl_autoload() suitable implementation for supporting class autoloading.
333 * Attach to spl_autoload() using the following:
334 * <code>
335 * spl_autoload_register(array('Zend_Loader', 'autoload'));
336 * </code>
338 * @param string $class
339 * @return string|false Class name on success; false on failure
341 public static function autoload($class)
343 try {
344 self::loadClass($class);
345 return $class;
346 } catch (Exception $e) {
347 return false;
352 * Register {@link autoload()} with spl_autoload()
354 * @param string $class (optional)
355 * @param boolean $enabled (optional)
356 * @return void
357 * @throws Exception if spl_autoload() is not found
358 * or if the specified class does not have an autoload() method.
360 public static function registerAutoload($class = 'Zend_Registry', $enabled = true)
362 if (!function_exists('spl_autoload_register')) {
363 throw new Exception('spl_autoload does not exist in this PHP installation');
366 self::loadClass($class);
367 $methods = get_class_methods($class);
368 if (!in_array('autoload', (array) $methods)) {
369 throw new Exception("The class \"$class\" does not have an autoload() method");
372 if ($enabled === true) {
373 spl_autoload_register(array($class, 'autoload'));
374 } else {
375 spl_autoload_unregister(array($class, 'autoload'));
380 * Ensure that filename does not contain exploits
382 * @param string $filename
383 * @return void
384 * @throws Zend_Exception
386 protected static function _securityCheck($filename)
389 * Security check
391 if (preg_match('/[^a-z0-9\\/\\\\_.-]/i', $filename)) {
392 throw new Exception('Security check: Illegal character in filename');
397 * Attempt to include() the file.
399 * include() is not prefixed with the @ operator because if
400 * the file is loaded and contains a parse error, execution
401 * will halt silently and this is difficult to debug.
403 * Always set display_errors = Off on production servers!
405 * @param string $filespec
406 * @param boolean $once
407 * @return boolean
408 * @deprecated Since 1.5.0; use loadFile() instead
410 protected static function _includeFile($filespec, $once = false)
412 if ($once) {
413 return include_once $filespec;
414 } else {
415 return include $filespec ;