0
0
mirror of https://github.com/ezyang/htmlpurifier.git synced 2024-11-09 15:28:40 +00:00

- Gracefully error out of obscure bug involving non-static autoloaders

- Test multiple autoloaders
- Remove local URL test; doesn't work for other people

git-svn-id: http://htmlpurifier.org/svnroot/htmlpurifier/trunk@1565 48356398-32a2-884e-a903-53898d9a118a
This commit is contained in:
Edward Z. Yang 2008-02-18 02:33:50 +00:00
parent a4abc45505
commit 21cf2c94d4
5 changed files with 68 additions and 5 deletions

View File

@ -5,7 +5,8 @@
* Convenience file that registers autoload handler for HTML Purifier.
*/
if (function_exists('spl_autoload_register')) {
if (function_exists('spl_autoload_register') && function_exists('spl_autoload_unregister')) {
// We need unregister for our pre-registering functionality
HTMLPurifier_Bootstrap::registerAutoload();
if (function_exists('__autoload')) {
// Be polite and ensure that userland autoload gets retained

View File

@ -62,8 +62,30 @@ class HTMLPurifier_Bootstrap
$autoload = array('HTMLPurifier_Bootstrap', 'autoload');
if ( ($funcs = spl_autoload_functions()) === false ) {
spl_autoload_register($autoload);
} else {
foreach ($funcs as $func) spl_autoload_unregister($func);
} elseif (function_exists('spl_autoload_unregister')) {
$compat = version_compare(PHP_VERSION, '5.1.2', '<=') &&
version_compare(PHP_VERSION, '5.1.0', '>=');
foreach ($funcs as $func) {
if (is_array($func)) {
// :TRICKY: There are some compatibility issues and some
// places where we need to error out
$reflector = new ReflectionMethod($func[0], $func[1]);
if (!$reflector->isStatic()) {
throw new Exception('
HTML Purifier autoloader registrar is not compatible
with non-static object methods due to PHP Bug #44144;
Please do not use HTMLPurifier.autoload.php (or any
file that includes this file); instead, place the code:
spl_autoload_register(array(\'HTMLPurifier_Bootstrap\', \'autoload\'))
after your own autoloaders.
');
}
// Suprisingly, spl_autoload_register supports the
// Class::staticMethod callback format, although call_user_func doesn't
if ($compat) $func = implode('::', $func);
}
spl_autoload_unregister($func);
}
spl_autoload_register($autoload);
foreach ($funcs as $func) spl_autoload_register($func);
}

View File

@ -12,6 +12,16 @@ function my_autoload($class) {
eval("class $class {}");
return true;
}
class MyClass {
public static function myAutoload($class) {
if ($class == 'Foo') {
echo "Special autoloading Foo..." . PHP_EOL;
eval("class $class {}");
}
}
}
spl_autoload_register(array('MyClass', 'myAutoload'));
spl_autoload_register('my_autoload');
require '../library/HTMLPurifier.auto.php';
@ -20,9 +30,11 @@ $config = HTMLPurifier_Config::createDefault();
$purifier = new HTMLPurifier($config);
echo $purifier->purify('<b>Salsa!') . PHP_EOL;
// purposely invoke older autoload
// purposely invoke older autoloads
$foo = new Foo();
$bar = new Bar();
--EXPECT--
<b>Salsa!</b>
Special autoloading Foo...
Autoloading Bar...

View File

@ -0,0 +1,29 @@
--TEST--
Error when registering autoload with non-static autoload already on SPL stack
--SKIPIF--
<?php
if (!function_exists('spl_autoload_register')) {
echo "skip - spl_autoload_register() not available";
}
--FILE--
<?php
class NotStatic
{
public function autoload($class) {
echo "Autoloading... $class" . PHP_EOL;
eval("class $class {}");
}
}
$obj = new NotStatic();
spl_autoload_register(array($obj, 'autoload'));
try {
require '../library/HTMLPurifier.auto.php';
} catch (Exception $e) {
echo 'Caught error gracefully';
assert('strpos($e->getMessage(), "44144") !== false');
}
--EXPECT--
Caught error gracefully

View File

@ -106,7 +106,6 @@ foreach ($versions_to_test as $version) {
// This is the HTML Purifier website's test XML file. We could
// add more websites, i.e. more configurations to test.
$test->addTestCase(new RemoteTestCase('http://localhost/htmlpurifier/tests/?xml=1', 'http://localhost/htmlpurifier/tests/?xml=1&dry=1'));
$test->addTestCase(new RemoteTestCase('http://htmlpurifier.org/dev/tests/?xml=1', 'http://htmlpurifier.org/dev/tests/?xml=1&dry=1'));
if ($AC['xml']) {