From 04cb7bc285127bee298de77dd1fea2cef8ff3406 Mon Sep 17 00:00:00 2001 From: Vasil Rangelov Date: Sun, 5 Feb 2012 18:49:02 +0200 Subject: [PATCH 1/3] Possible fix for GH #3 "Do not throw an exception if there are other autoloaders" --- src/PEAR2/Autoload.php | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/PEAR2/Autoload.php b/src/PEAR2/Autoload.php index e31fe5c..0bb2f2c 100644 --- a/src/PEAR2/Autoload.php +++ b/src/PEAR2/Autoload.php @@ -166,6 +166,9 @@ static function load($class) if (file_exists($path . DIRECTORY_SEPARATOR . $file)) { require $path . DIRECTORY_SEPARATOR . $file; if (!self::loadSuccessful($class)) { + if (count(spl_autoload_functions()) > 1) { + return false; + } throw new \Exception('Class ' . $class . ' was not present in ' . $path . DIRECTORY_SEPARATOR . $file . '") [PEAR2_Autoload-@PACKAGE_VERSION@]'); @@ -177,18 +180,20 @@ static function load($class) return true; } } - + if (count(spl_autoload_functions()) > 1) { + return false; + } $e = new \Exception('Class ' . $class . ' could not be loaded from ' . $file . ', file does not exist (registered paths="' . implode(PATH_SEPARATOR, self::$paths) . '") [PEAR2_Autoload-@PACKAGE_VERSION@]'); $trace = $e->getTrace(); if (isset($trace[2]) && isset($trace[2]['function']) && - in_array($trace[2]['function'], array('class_exists', 'interface_exists'))) { + in_array($trace[2]['function'], array('class_exists', 'interface_exists'))) { return false; } if (isset($trace[1]) && isset($trace[1]['function']) && - in_array($trace[1]['function'], array('class_exists', 'interface_exists'))) { + in_array($trace[1]['function'], array('class_exists', 'interface_exists'))) { return false; } throw $e; From 72055dab33bada7045819238477eeea87522b7ce Mon Sep 17 00:00:00 2001 From: Vasil Rangelov Date: Sun, 5 Feb 2012 19:14:48 +0200 Subject: [PATCH 2/3] Fixed most PHPCS issues. --- CREDITS | 3 +- package.xml | 10 ++++-- package_compatible.xml | 10 ++++-- src/PEAR2/Autoload.php | 78 ++++++++++++++++++++++++++++++++++-------- 4 files changed, 82 insertions(+), 19 deletions(-) diff --git a/CREDITS b/CREDITS index 25cf5e4..09b32c2 100644 --- a/CREDITS +++ b/CREDITS @@ -1,4 +1,5 @@ ;; maintainers of Autoload Gregory Beaver [cellog] (lead) Brett Bieber [saltybeagle] (lead) -Clay Loveless [clay] (contributor) \ No newline at end of file +Clay Loveless [clay] (contributor) +Vasil Rangelov [boen_robot] (contributor) \ No newline at end of file diff --git a/package.xml b/package.xml index 781f566..874ee19 100644 --- a/package.xml +++ b/package.xml @@ -37,8 +37,14 @@ To use: clay@php.net yes - 2011-08-08 - + + Vasil Rangelov + boen_robot + boen.robot@gmail.com + yes + + 2012-02-05 + 0.2.4 0.1.0 diff --git a/package_compatible.xml b/package_compatible.xml index ad17cb2..958090b 100644 --- a/package_compatible.xml +++ b/package_compatible.xml @@ -37,8 +37,14 @@ To use: clay@php.net yes - 2011-08-08 - + + Vasil Rangelov + boen_robot + boen.robot@gmail.com + yes + + 2012-02-05 + 0.2.4 0.1.0 diff --git a/src/PEAR2/Autoload.php b/src/PEAR2/Autoload.php index 0bb2f2c..8c00f04 100644 --- a/src/PEAR2/Autoload.php +++ b/src/PEAR2/Autoload.php @@ -1,6 +1,42 @@ + * @author Brett Bieber + * @copyright 2012 PEAR2 + * @license http://www.opensource.org/licenses/bsd-license.php New BSD License + * @version GIT: $Id$ + * @link http://pear2.php.net/PEAR2_Autoload + */ namespace PEAR2; if (!class_exists('\PEAR2\Autoload', false)) { + /** + * Standard Autoloader for PEAR2 + * + * PEAR2_Autoload is the standard method of class loading for development + * and low-volume web sites using PEAR2 packages. + * + * PHP version 5 + * + * @category PEAR2 + * @package PEAR2_Autoload + * @author Gregory Beaver + * @author Brett Bieber + * @copyright 2012 PEAR2 + * @license http://www.opensource.org/licenses/bsd-license.php + * New BSDLicense + * @version GIT: $Id$ + * @link http://pear2.php.net/PEAR2_Autoload + */ class Autoload { /** @@ -48,7 +84,8 @@ class Autoload /** * Initialize the PEAR2 autoloader * - * @param string $path Directory path to register + * @param string $path Directory path to register + * @param string $mapfile Path to a mapping file to register. * * @return void */ @@ -161,7 +198,8 @@ static function load($class) } } - $file = str_replace(array('_', '\\'), DIRECTORY_SEPARATOR, $class) . '.php'; + $file = str_replace(array('_', '\\'), DIRECTORY_SEPARATOR, $class) . + '.php'; foreach (self::$paths as $path) { if (file_exists($path . DIRECTORY_SEPARATOR . $file)) { require $path . DIRECTORY_SEPARATOR . $file; @@ -169,9 +207,11 @@ static function load($class) if (count(spl_autoload_functions()) > 1) { return false; } - throw new \Exception('Class ' . $class . ' was not present in ' . + throw new \Exception( + 'Class ' . $class . ' was not present in ' . $path . DIRECTORY_SEPARATOR . $file . - '") [PEAR2_Autoload-@PACKAGE_VERSION@]'); + '") [PEAR2_Autoload-@PACKAGE_VERSION@]' + ); } if (in_array($class, self::$unmapped)) { @@ -183,17 +223,22 @@ static function load($class) if (count(spl_autoload_functions()) > 1) { return false; } - $e = new \Exception('Class ' . $class . ' could not be loaded from ' . + $e = new \Exception( + 'Class ' . $class . ' could not be loaded from ' . $file . ', file does not exist (registered paths="' . implode(PATH_SEPARATOR, self::$paths) . - '") [PEAR2_Autoload-@PACKAGE_VERSION@]'); + '") [PEAR2_Autoload-@PACKAGE_VERSION@]' + ); $trace = $e->getTrace(); - if (isset($trace[2]) && isset($trace[2]['function']) && - in_array($trace[2]['function'], array('class_exists', 'interface_exists'))) { + $checkFunctions = array('class_exists', 'interface_exists'); + if (isset($trace[2]) && isset($trace[2]['function']) + && in_array($trace[2]['function'], $checkFunctions) + ) { return false; } - if (isset($trace[1]) && isset($trace[1]['function']) && - in_array($trace[1]['function'], array('class_exists', 'interface_exists'))) { + if (isset($trace[1]) && isset($trace[1]['function']) + && in_array($trace[1]['function'], $checkFunctions) + ) { return false; } throw $e; @@ -202,6 +247,8 @@ static function load($class) /** * Check if the requested class was loaded from the specified path * + * @param string $class The name of the class to check. + * * @return bool */ protected static function loadSuccessful($class) @@ -216,16 +263,19 @@ protected static function loadSuccessful($class) * If possible, update the classmap file with newly-discovered * mapping. * - * @param string $class Class name discovered - * + * @param string $class Class name discovered * @param string $origin File where class was found * + * @return void */ protected static function updateMap($class, $origin) { - if (is_writable(self::$mapfile) || is_writable(dirname(self::$mapfile))) { + if (is_writable(self::$mapfile) + || is_writable(dirname(self::$mapfile)) + ) { self::$map[$class] = $origin; - file_put_contents(self::$mapfile, + file_put_contents( + self::$mapfile, '<'."?php\n" . "// PEAR2\Autoload auto-generated classmap\n" . "return " . var_export(self::$map, true) . ';', From fa4f457a737246d360426b5932b4591c1c303f15 Mon Sep 17 00:00:00 2001 From: Vasil Rangelov Date: Mon, 6 Feb 2012 20:49:00 +0200 Subject: [PATCH 3/3] Performance optimization at loadSuccessful(). --- src/PEAR2/Autoload.php | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/PEAR2/Autoload.php b/src/PEAR2/Autoload.php index 8c00f04..e8a2b5d 100644 --- a/src/PEAR2/Autoload.php +++ b/src/PEAR2/Autoload.php @@ -253,10 +253,7 @@ static function load($class) */ protected static function loadSuccessful($class) { - if (!class_exists($class, false) && !interface_exists($class, false)) { - return false; - } - return true; + return class_exists($class, false) || interface_exists($class, false); } /**