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.
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
23 * Static methods for loading classes and files.
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
33 * Loads a class from a PHP file. The filename must be formatted
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
50 * @throws Zend_Exception
52 public static function loadClass($class, $dirs = null)
54 if (class_exists($class, false) ||
interface_exists($class, false)) {
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, '\\');
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';
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) {
85 $dirs[$key] = $dirPath;
87 $dir = rtrim($dir, '\\/');
88 $dirs[$key] = $dir . DIRECTORY_SEPARATOR
. $dirPath;
91 $file = basename($file);
92 self
::loadFile($file, $dirs, true);
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
122 * @param boolean $once
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
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.
146 include_once $filename;
152 * If searching in directories, reset include_path
155 set_include_path($incPath);
162 * Returns TRUE if the $filename is readable, or FALSE otherwise.
163 * This function uses the PHP include_path, where PHP's is_readable()
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
174 public static function isReadable($filename)
176 if (is_readable($filename)) {
177 // Return early if the filename is readable without needing the
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
190 foreach (self
::explodeIncludePath() as $path) {
192 if (is_readable($filename)) {
197 $file = $path . '/' . $filename;
198 if (is_readable($file)) {
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
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);
226 $paths = explode(PATH_SEPARATOR
, $path);
232 * spl_autoload() suitable implementation for supporting class autoloading.
234 * Attach to spl_autoload() using the following:
236 * spl_autoload_register(array('Zend_Loader', 'autoload'));
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
);
247 @self
::loadClass($class);
249 } catch (Exception
$e) {
255 * Register {@link autoload()} with spl_autoload()
257 * @deprecated Since 1.8.0
258 * @param string $class (optional)
259 * @param boolean $enabled (optional)
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');
282 $autoloader->pushAutoloader($callback);
284 $autoloader->removeAutoloader($callback);
290 * Ensure that filename does not contain exploits
292 * @param string $filename
294 * @throws Zend_Exception
296 protected static function _securityCheck($filename)
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
319 * @deprecated Since 1.5.0; use loadFile() instead
321 protected static function _includeFile($filespec, $once = false)
324 return include_once $filespec;
326 return include $filespec ;