Package home | Report new bug | New search | Development Roadmap Status: Open | Feedback | All | Closed Since Version 0.5.0

Bug #18129 interfere with other autoloaders
Submitted: 2010-12-20 20:40 UTC
From: matthieu Assigned: till
Status: Closed Package: File_IMC
PHP Version: 5.3.3 OS: linux
Roadmaps: 0.4.2    
Subscription  


 [2010-12-20 20:40 UTC] matthieu (Matthieu Robillard)
Description: ------------ Using File_IMC in an application wich already has autoloaders causes problems. In symfony for instance we get this error message : Warning: require(File/IMC/Exception.php): failed to open stream: No such file or directory in /var/www/.../sf/lib/vendor/File_IMC/File/IMC.php on line 138 Fatal error: require(): Failed opening required 'File/IMC/Exception.php' (include_path='/var/www/.../sf/lib/vendor:.:/usr/share/php:/usr/share/pear') in /var/www/.../sf/lib/vendor/File_IMC/File/IMC.php on line 138 This patch solves the problem : --- IMC.php +++ (clipboard) @@ -134,9 +134,16 @@ * @return boolean */ public static function autoload($className) - { - return require str_replace('_', '/', $className) . '.php'; - } + { + //Don't interfere with other autoloaders + if (0 !== strpos($className, 'File_IMC')) return false; + + $path = dirname(__FILE__).'/../'.str_replace('_', '/', $className).'.php'; + + if (!file_exists($path)) return false; + + require_once $path; + } /** * Builder factory @@ -164,15 +171,15 @@ $fileName = 'File/IMC/Build/'. $format . '.php'; $className = 'File_IMC_Build_'. $format; - if (!class_exists($className, false)) { - @include_once $fileName; - } - - if (!class_exists($className, false)) { - throw new File_IMC_Exception( - 'No builder driver exists for format: ' . $format, - self::ERROR_INVALID_DRIVER); - } + //if (!class_exists($className, false)) { + // @include_once $fileName; + //} + // + //if (!class_exists($className, false)) { + // throw new File_IMC_Exception( + // 'No builder driver exists for format: ' . $format, + // self::ERROR_INVALID_DRIVER); + //} if ($version !== null) { $class = new $className($version); @@ -207,15 +214,15 @@ $fileName = 'File/IMC/Parse/'. $format . '.php'; $className = 'File_IMC_Parse_'. $format; - if (!class_exists($className, false)) { - @include_once $fileName; - } - - if (!class_exists($className, false)) { - throw new File_IMC_Exception( - 'No parser driver exists for format: ' . $format, - self::ERROR_INVALID_DRIVER); - } + //if (!class_exists($className, false)) { + // @include_once $fileName; + //} + // + //if (!class_exists($className, false)) { + // throw new File_IMC_Exception( + // 'No parser driver exists for format: ' . $format, + // self::ERROR_INVALID_DRIVER); + //} return new $className; } }

Comments

 [2010-12-20 20:52 UTC] till (Till Klampaeckel)
I think instead of require, it should use include. Then the error is not hard. The strpos() etc. is not really necessary since autoloaders are usually chained. Which means that it should just use the next one in the chain if it fails. I'll certainly take a look in a bit.
 [2010-12-20 20:52 UTC] till (Till Klampaeckel)
-Assigned To: +Assigned To: till
 [2010-12-20 23:02 UTC] matthieu (Matthieu Robillard)
Ok For info I just copied the autoload() I found in the SwiftMailer included with Symfony.
 [2011-03-11 22:21 UTC] till (Till Klampaeckel)
-Status: Assigned +Status: Closed -Roadmap Versions: +Roadmap Versions: 0.4.2
This bug has been fixed in SVN. If this was a documentation problem, the fix will appear on pear.php.net by the end of next Sunday (CET). If this was a problem with the pear.php.net website, the change should be live shortly. Otherwise, the fix will appear in the package's next release. Thank you for the report and for helping us make PEAR better. Thanks again for reporting. I didn't use your patch but committed a fix regardless.