[ZF-10089] Zend_Log
[zend/radio.git] / library / Zend / Loader.php
blobd69af1235c3368beea9bbaaa3350a6490c7cf22e
1 <?php
2 /**
3 * Zend Framework
5 * LICENSE
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.
15 * @category Zend
16 * @package Zend_Loader
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$
22 /**
23 * Static methods for loading classes and files.
25 * @category Zend
26 * @package Zend_Loader
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
30 class Zend_Loader
32 /**
33 * Loads a class from a PHP file. The filename must be formatted
34 * as "$class.php".
36 * If $dirs is a string or an array, it will search the directories
37 * in the order supplied, and attempt to load the first matching file.
39 * If $dirs is null, it will split the class name at underscores to
40 * generate a path hierarchy (e.g., "Zend_Example_Class" will map
41 * to "Zend/Example/Class.php").
43 * If the file was not found in the $dirs, or if no $dirs were specified,
44 * it will attempt to load it from PHP's include_path.
46 * @param string $class - The full class name of a Zend component.
47 * @param string|array $dirs - OPTIONAL Either a path or an array of paths
48 * to search.
49 * @return void
50 * @throws Zend_Exception
52 public static function loadClass($class, $dirs = null)
54 if (class_exists($class, false) || interface_exists($class, false)) {
55 return;
58 if ((null !== $dirs) && !is_string($dirs) && !is_array($dirs)) {
59 require_once 'Zend/Exception.php';
60 throw new Zend_Exception('Directory argument must be a string or an array');
63 // Autodiscover the path from the class name
64 // Implementation is PHP namespace-aware, and based on
65 // Framework Interop Group reference implementation:
66 // http://groups.google.com/group/php-standards/web/psr-0-final-proposal
67 $className = ltrim($class, '\\');
68 $file = '';
69 $namespace = '';
70 if ($lastNsPos = strripos($className, '\\')) {
71 $namespace = substr($className, 0, $lastNsPos);
72 $className = substr($className, $lastNsPos + 1);
73 $file = str_replace('\\', DIRECTORY_SEPARATOR, $namespace) . DIRECTORY_SEPARATOR;
75 $file .= str_replace('_', DIRECTORY_SEPARATOR, $className) . '.php';
77 if (!empty($dirs)) {
78 // use the autodiscovered path
79 $dirPath = dirname($file);
80 if (is_string($dirs)) {
81 $dirs = explode(PATH_SEPARATOR, $dirs);
83 foreach ($dirs as $key => $dir) {
84 if ($dir == '.') {
85 $dirs[$key] = $dirPath;
86 } else {
87 $dir = rtrim($dir, '\\/');
88 $dirs[$key] = $dir . DIRECTORY_SEPARATOR . $dirPath;
91 $file = basename($file);
92 self::loadFile($file, $dirs, true);
93 } else {
94 self::loadFile($file, null, true);
97 if (!class_exists($class, false) && !interface_exists($class, false)) {
98 require_once 'Zend/Exception.php';
99 throw new Zend_Exception("File \"$file\" does not exist or class \"$class\" was not found in the file");
104 * Loads a PHP file. This is a wrapper for PHP's include() function.
106 * $filename must be the complete filename, including any
107 * extension such as ".php". Note that a security check is performed that
108 * does not permit extended characters in the filename. This method is
109 * intended for loading Zend Framework files.
111 * If $dirs is a string or an array, it will search the directories
112 * in the order supplied, and attempt to load the first matching file.
114 * If the file was not found in the $dirs, or if no $dirs were specified,
115 * it will attempt to load it from PHP's include_path.
117 * If $once is TRUE, it will use include_once() instead of include().
119 * @param string $filename
120 * @param string|array $dirs - OPTIONAL either a path or array of paths
121 * to search.
122 * @param boolean $once
123 * @return boolean
124 * @throws Zend_Exception
126 public static function loadFile($filename, $dirs = null, $once = false)
128 self::_securityCheck($filename);
131 * Search in provided directories, as well as include_path
133 $incPath = false;
134 if (!empty($dirs) && (is_array($dirs) || is_string($dirs))) {
135 if (is_array($dirs)) {
136 $dirs = implode(PATH_SEPARATOR, $dirs);
138 $incPath = get_include_path();
139 set_include_path($dirs . PATH_SEPARATOR . $incPath);
143 * Try finding for the plain filename in the include_path.
145 if ($once) {
146 include_once $filename;
147 } else {
148 include $filename;
152 * If searching in directories, reset include_path
154 if ($incPath) {
155 set_include_path($incPath);
158 return true;
162 * Returns TRUE if the $filename is readable, or FALSE otherwise.
163 * This function uses the PHP include_path, where PHP's is_readable()
164 * does not.
166 * Note from ZF-2900:
167 * If you use custom error handler, please check whether return value
168 * from error_reporting() is zero or not.
169 * At mark of fopen() can not suppress warning if the handler is used.
171 * @param string $filename
172 * @return boolean
174 public static function isReadable($filename)
176 if (is_readable($filename)) {
177 // Return early if the filename is readable without needing the
178 // include_path
179 return true;
182 if (strtoupper(substr(PHP_OS, 0, 3)) == 'WIN'
183 && preg_match('/^[a-z]:/i', $filename)
185 // If on windows, and path provided is clearly an absolute path,
186 // return false immediately
187 return false;
190 foreach (self::explodeIncludePath() as $path) {
191 if ($path == '.') {
192 if (is_readable($filename)) {
193 return true;
195 continue;
197 $file = $path . '/' . $filename;
198 if (is_readable($file)) {
199 return true;
202 return false;
206 * Explode an include path into an array
208 * If no path provided, uses current include_path. Works around issues that
209 * occur when the path includes stream schemas.
211 * @param string|null $path
212 * @return array
214 public static function explodeIncludePath($path = null)
216 if (null === $path) {
217 $path = get_include_path();
220 if (PATH_SEPARATOR == ':') {
221 // On *nix systems, include_paths which include paths with a stream
222 // schema cannot be safely explode'd, so we have to be a bit more
223 // intelligent in the approach.
224 $paths = preg_split('#:(?!//)#', $path);
225 } else {
226 $paths = explode(PATH_SEPARATOR, $path);
228 return $paths;
232 * spl_autoload() suitable implementation for supporting class autoloading.
234 * Attach to spl_autoload() using the following:
235 * <code>
236 * spl_autoload_register(array('Zend_Loader', 'autoload'));
237 * </code>
239 * @deprecated Since 1.8.0
240 * @param string $class
241 * @return string|false Class name on success; false on failure
243 public static function autoload($class)
245 trigger_error(__CLASS__ . '::' . __METHOD__ . ' is deprecated as of 1.8.0 and will be removed with 2.0.0; use Zend_Loader_Autoloader instead', E_USER_NOTICE);
246 try {
247 @self::loadClass($class);
248 return $class;
249 } catch (Exception $e) {
250 return false;
255 * Register {@link autoload()} with spl_autoload()
257 * @deprecated Since 1.8.0
258 * @param string $class (optional)
259 * @param boolean $enabled (optional)
260 * @return void
261 * @throws Zend_Exception if spl_autoload() is not found
262 * or if the specified class does not have an autoload() method.
264 public static function registerAutoload($class = 'Zend_Loader', $enabled = true)
266 trigger_error(__CLASS__ . '::' . __METHOD__ . ' is deprecated as of 1.8.0 and will be removed with 2.0.0; use Zend_Loader_Autoloader instead', E_USER_NOTICE);
267 require_once 'Zend/Loader/Autoloader.php';
268 $autoloader = Zend_Loader_Autoloader::getInstance();
269 $autoloader->setFallbackAutoloader(true);
271 if ('Zend_Loader' != $class) {
272 self::loadClass($class);
273 $methods = get_class_methods($class);
274 if (!in_array('autoload', (array) $methods)) {
275 require_once 'Zend/Exception.php';
276 throw new Zend_Exception("The class \"$class\" does not have an autoload() method");
279 $callback = array($class, 'autoload');
281 if ($enabled) {
282 $autoloader->pushAutoloader($callback);
283 } else {
284 $autoloader->removeAutoloader($callback);
290 * Ensure that filename does not contain exploits
292 * @param string $filename
293 * @return void
294 * @throws Zend_Exception
296 protected static function _securityCheck($filename)
299 * Security check
301 if (preg_match('/[^a-z0-9\\/\\\\_.:-]/i', $filename)) {
302 require_once 'Zend/Exception.php';
303 throw new Zend_Exception('Security check: Illegal character in filename');
308 * Attempt to include() the file.
310 * include() is not prefixed with the @ operator because if
311 * the file is loaded and contains a parse error, execution
312 * will halt silently and this is difficult to debug.
314 * Always set display_errors = Off on production servers!
316 * @param string $filespec
317 * @param boolean $once
318 * @return boolean
319 * @deprecated Since 1.5.0; use loadFile() instead
321 protected static function _includeFile($filespec, $once = false)
323 if ($once) {
324 return include_once $filespec;
325 } else {
326 return include $filespec ;