From 0e7c0d918f3879213a894420007454121413bd39 Mon Sep 17 00:00:00 2001 From: Jordi Boggiano Date: Thu, 9 Feb 2012 15:42:47 +0100 Subject: [PATCH] Add useIncludePath support to ClassLoader --- src/Composer/Autoload/ClassLoader.php | 41 +++++++++++++++++++++++---- 1 file changed, 35 insertions(+), 6 deletions(-) diff --git a/src/Composer/Autoload/ClassLoader.php b/src/Composer/Autoload/ClassLoader.php index aa4669e34..773561118 100644 --- a/src/Composer/Autoload/ClassLoader.php +++ b/src/Composer/Autoload/ClassLoader.php @@ -26,6 +26,9 @@ namespace Composer\Autoload; * // activate the autoloader * $loader->register(); * + * // to enable searching the include path (eg. for PEAR packages) + * $loader->setUseIncludePath(true); + * * In this example, if you try to use a class in the Symfony\Component * namespace or one of its children (Symfony\Component\Console for instance), * the autoloader will first look for the class under the component/ @@ -41,6 +44,7 @@ class ClassLoader { private $prefixes = array(); private $fallbackDirs = array(); + private $useIncludePath = false; public function getPrefixes() { @@ -74,6 +78,27 @@ class ClassLoader } } + /** + * Turns on searching the include for class files. + * + * @param Boolean $useIncludePath + */ + public function setUseIncludePath($useIncludePath) + { + $this->useIncludePath = $useIncludePath; + } + + /** + * Can be used to check if the autoloader uses the include path to check + * for classes. + * + * @return Boolean + */ + public function getUseIncludePath() + { + return $this->useIncludePath; + } + /** * Registers this instance as an autoloader. * @@ -121,7 +146,7 @@ class ClassLoader if (false !== $pos = strrpos($class, '\\')) { // namespaced class name - $classPath = DIRECTORY_SEPARATOR . str_replace('\\', DIRECTORY_SEPARATOR, substr($class, 0, $pos)); + $classPath = str_replace('\\', DIRECTORY_SEPARATOR, substr($class, 0, $pos)) . DIRECTORY_SEPARATOR; $className = substr($class, $pos + 1); } else { // PEAR-like class name @@ -129,22 +154,26 @@ class ClassLoader $className = $class; } - $classPath .= DIRECTORY_SEPARATOR . str_replace('_', DIRECTORY_SEPARATOR, $className) . '.php'; + $classPath .= str_replace('_', DIRECTORY_SEPARATOR, $className) . '.php'; foreach ($this->prefixes as $prefix => $dirs) { foreach ($dirs as $dir) { if (0 === strpos($class, $prefix)) { - if (file_exists($dir . $classPath)) { - return $dir . $classPath; + if (file_exists($dir . DIRECTORY_SEPARATOR . $classPath)) { + return $dir . DIRECTORY_SEPARATOR . $classPath; } } } } foreach ($this->fallbackDirs as $dir) { - if (file_exists($dir . $classPath)) { - return $dir . $classPath; + if (file_exists($dir . DIRECTORY_SEPARATOR . $classPath)) { + return $dir . DIRECTORY_SEPARATOR . $classPath; } } + + if ($this->useIncludePath && $file = stream_resolve_include_path($classPath)) { + return $file; + } } }