From 8bebfc297b4c3e75e78302a09479305a691fe79c Mon Sep 17 00:00:00 2001 From: Michael M Slusarz Date: Sat, 30 Jan 2010 18:41:42 -0700 Subject: [PATCH] Add ability to define callback to run when class is autoloaded. --- framework/Autoloader/lib/Horde/Autoloader.php | 63 +++++++++++++++++++++------ framework/Autoloader/package.xml | 3 +- 2 files changed, 51 insertions(+), 15 deletions(-) diff --git a/framework/Autoloader/lib/Horde/Autoloader.php b/framework/Autoloader/lib/Horde/Autoloader.php index 00c8ab224..08e42cc78 100644 --- a/framework/Autoloader/lib/Horde/Autoloader.php +++ b/framework/Autoloader/lib/Horde/Autoloader.php @@ -23,6 +23,13 @@ class Horde_Autoloader protected static $_includeCache = null; /** + * Callbacks. + * + * @var array + */ + protected static $_callbacks = array(); + + /** * Autoload implementation automatically registered with * spl_autoload_register. * @@ -50,7 +57,7 @@ class Horde_Autoloader str_replace(array('\\', '_'), '/', substr($class, $matches[0][1] + strlen($matches[0][0]))); } - if (self::_loadClass($relativePath)) { + if (self::_loadClass($class, $relativePath)) { return true; } } @@ -58,38 +65,51 @@ class Horde_Autoloader /* Do a final search in the include path. */ $relativePath = str_replace(array('\\', '_'), '/', $class); - return self::_loadClass($relativePath); + return self::_loadClass($class, $relativePath); } /** * Try to load the class from each path in the include_path. * - * @param string $relativePath + * @param string $class The loaded classname. + * @param string $relativePath TODO */ - protected static function _loadClass($relativePath) + protected static function _loadClass($class, $relativePath) { + $result = false; + if (substr($relativePath, 0, 1) == '/') { - return self::_loadClassUsingAbsolutePath($relativePath); + $result = self::_loadClassUsingAbsolutePath($relativePath); + } else { + if (is_null(self::$_includeCache)) { + self::_cacheIncludePath(); + } + + foreach (self::$_includeCache as $includePath) { + if (self::_loadClassUsingAbsolutePath($includePath . DIRECTORY_SEPARATOR . $relativePath)) { + $result = true; + break; + } + } } - if (is_null(self::$_includeCache)) { - self::_cacheIncludePath(); + if (!$result) { + return false; } - foreach (self::$_includeCache as $includePath) { - if (self::_loadClassUsingAbsolutePath($includePath . DIRECTORY_SEPARATOR . $relativePath)) { - return true; - } + $class = strtolower($class); + if (isset(self::$_callbacks[$class])) { + call_user_func(self::$_callbacks[$class]); } - return false; + return true; } /** * Include a PHP file using an absolute path, suppressing E_WARNING and * E_DEPRECATED errors, but letting fatal/parse errors come through. * - * @param string $absolutePath + * @param string $absolutePath TODO * * @TODO Remove E_DEPRECATED masking */ @@ -161,7 +181,21 @@ class Horde_Autoloader self::$_classPatterns[] = array($pattern, $replace); } - static function _cacheIncludePath() + /** + * Add a callback to run when a class is loaded through loadClass(). + * + * @param string $class The classname. + * @param mixed $callback The callback to run when the class is loaded. + */ + public static function addCallback($class, $callback) + { + self::$_callbacks[strtolower($class)] = $callback; + } + + /** + * TODO + */ + protected static function _cacheIncludePath() { self::$_includeCache = array(); foreach (explode(PATH_SEPARATOR, get_include_path()) as $path) { @@ -172,6 +206,7 @@ class Horde_Autoloader } } } + } /* Register the autoloader in a way to play well with as many configurations diff --git a/framework/Autoloader/package.xml b/framework/Autoloader/package.xml index 1ad69133a..538283cef 100644 --- a/framework/Autoloader/package.xml +++ b/framework/Autoloader/package.xml @@ -31,7 +31,8 @@ http://pear.php.net/dtd/package-2.0.xsd"> beta LGPL - * Bug #7856: Allow underscores in pathname. + * Add ability to define callback to run when class is loaded. + * Bug #7856: Allow underscores in pathname. * Rename to Horde_Autoloader. -- 2.11.0