$dir) { $dir = rtrim($dir, '\\/'); $dirs[$key] = $dir . DIRECTORY_SEPARATOR . $dirPath; } } $file = basename($path) . '.php'; } else { $file = $class . '.php'; } self::loadFile($file, $dirs, true); if (!class_exists($class, false) && !interface_exists($class, false)) { throw new Zend_Exception("File \"$file\" was loaded but class \"$class\" was not found in the file"); } } /** * Loads a PHP file. This is a wrapper for PHP's include() function. * * $filename must be the complete filename, including any * extension such as ".php". Note that a security check is performed that * does not permit extended characters in the filename. This method is * intended for loading Zend Framework files. * * If $dirs is a string or an array, it will search the directories * in the order supplied, and attempt to load the first matching file. * * If the file was not found in the $dirs, or if no $dirs were specified, * it will attempt to load it from PHP's include_path. * * If $once is TRUE, it will use include_once() instead of include(). * * @param string $filename * @param string|array $dirs - OPTIONAL either a path or array of paths * to search. * @param boolean $once * @return mixed * @throws Zend_Exception */ public static function loadFile($filename, $dirs = null, $once = false) { // security check if (preg_match('/[^a-z0-9\-_.]/i', $filename)) { throw new Zend_Exception('Security check: Illegal character in filename'); } /** * Determine if the file is readable, either within just the include_path * or within the $dirs search list. */ $filespec = $filename; if (empty($dirs)) { $dirs = null; } if ($dirs === null) { $found = self::isReadable($filespec); } else { foreach ((array)$dirs as $dir) { $filespec = rtrim($dir, '\\/') . DIRECTORY_SEPARATOR . $filename; $found = self::isReadable($filespec); if ($found) { break; } } } /** * Throw an exception if the file could not be located */ if (!$found) { throw new Zend_Exception("File \"$filespec\" was not found"); } /** * Attempt to include() the file. * * include() is not prefixed with the @ operator because if * the file is loaded and contains a parse error, execution * will halt silently and this is difficult to debug. * * Always set display_errors = Off on production servers! */ if ($once) { return include_once $filespec; } else { return include $filespec ; } } /** * Returns TRUE if the $filename is readable, or FALSE otherwise. * This function uses the PHP include_path, where PHP's is_readable() * does not. * * @param string $filename * @return boolean */ public static function isReadable($filename) { if (is_readable($filename)) { return true; } $path = get_include_path(); $dirs = explode(PATH_SEPARATOR, $path); foreach ($dirs as $dir) { // No need to check against current dir -- already checked if ('.' == $dir) { continue; } if (is_readable($dir . DIRECTORY_SEPARATOR . $filename)) { return true; } } return false; } /** * spl_autoload() suitable implementation for supporting class autoloading. * * Attach to spl_autoload() using the following: * * spl_autoload_register(array('Zend_Loader', 'autoload')); * * * @param string $class * @return string|false Class name on success; false on failure */ public static function autoload($class) { try { self::loadClass($class); return $class; } catch (Exception $e) { return false; } } }