From c0941ce4b7e96c2e90d7a817a4f14c6fdb26b6a0 Mon Sep 17 00:00:00 2001 From: Michael M Slusarz Date: Sat, 8 Nov 2008 23:58:39 -0700 Subject: [PATCH] Add Horde_Autoloader. --- framework/Autoloader/lib/Horde/Autoloader.php | 103 ++++++++++++++++++++++++++ framework/Autoloader/package.xml | 61 +++++++++++++++ 2 files changed, 164 insertions(+) create mode 100644 framework/Autoloader/lib/Horde/Autoloader.php create mode 100644 framework/Autoloader/package.xml diff --git a/framework/Autoloader/lib/Horde/Autoloader.php b/framework/Autoloader/lib/Horde/Autoloader.php new file mode 100644 index 000000000..1d3dd1748 --- /dev/null +++ b/framework/Autoloader/lib/Horde/Autoloader.php @@ -0,0 +1,103 @@ + 'Horde/'); + + /** + * Autoload implementation automatically registered with + * spl_autoload_register. + * + * We ignore E_WARNINGS when trying to include files so that if our + * autoloader doesn't find a file, we pass on to the next autoloader (if + * any) or to the PHP class not found error. We don't want to suppress all + * errors, though, or else we'll end up silencing parse errors or + * redefined class name errors, making debugging especially difficult. + * + * @param string $class Class name to load (or interface). + */ + public static function loadClass($class) + { + foreach (self::$_classPatterns as $pattern => $replace) { + $file = $class; + + if (!is_null($replace)) { + $file = preg_replace($pattern, $replace, $file); + } + + if (!is_null($replace) || preg_match($pattern, $file)) { + $file = str_replace(array('::', '_'), '/', $file) . '.php'; + $oldErrorReporting = error_reporting(E_ALL ^ E_WARNING); + $res = include_once $file; + error_reporting($oldErrorReporting); + if ($res) { + return true; + } + } + } + } + + /** + * Add a new path to the include_path we're loading from. + * + * @param string $path The directory to add. + * @param boolean $prepend Add to the beginning of the stack? + * + * @return string The new include_path. + */ + public static function addClassPath($path, $prepend = true) + { + $include_path = get_include_path(); + if ($include_path == $path + || strpos($include_path, PATH_SEPARATOR . $path) + || strpos($include_path, $path . PATH_SEPARATOR) !== false) { + // The path is already present in our stack; don't re-add it. + return $include_path; + } + + if ($prepend) { + $include_path = $path . PATH_SEPARATOR . $include_path; + } else { + $include_path .= PATH_SEPARATOR . $path; + } + set_include_path($include_path); + + return $include_path; + } + + /** + * Add a new class pattern. + * + * @param string $pattern The class pattern to add. + * @param string $replace The substitution pattern. + */ + public static function addClassPattern($pattern, $replace = null) + { + self::$_classPatterns[$pattern] = $replace; + } + +} + +/* Register the autoloader in a way to play well with as many configurations + * as possible. */ +if (function_exists('spl_autoload_register')) { + spl_autoload_register(array('Horde_Autoloader', 'loadClass')); + if (function_exists('__autoload')) { + spl_autoload_register('__autoload'); + } +} elseif (!function_exists('__autoload')) { + function __autoload($class) + { + return Horde_Autoloader::loadClass($class); + } +} diff --git a/framework/Autoloader/package.xml b/framework/Autoloader/package.xml new file mode 100644 index 000000000..2b7538e91 --- /dev/null +++ b/framework/Autoloader/package.xml @@ -0,0 +1,61 @@ + + + Autoloader + pear.horde.org + Horde Class Autoloader + Autoload implementation and class loading manager for Horde + + + Chuck Hagenbuch + chuck + chuck@horde.org + yes + + + Jan Schneider + jan + jan@horde.org + yes + + 2008-09-02 + + + 0.2.0 + 0.2.0 + + + beta + beta + + LGPL + Rename to Horde_Autoloader. + + + + + + + + + + + + + + 5.2 + + + 1.5 + + + + + + + + + + -- 2.11.0