Skip to content

Commit c24009c

Browse files
committed
New core class autoloader
1 parent 51493d4 commit c24009c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+944
-819
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
--TEST--
2+
Basic autoload_call_class() function
3+
--CREDITS--
4+
Jean-Marc Fontaine <jean-marc.fontaine@alterway.fr>
5+
# Alter Way Contribution Day 2011
6+
--FILE--
7+
<?php
8+
function customAutoloader($class) {
9+
class TestClass {}
10+
}
11+
autoload_register_class('customAutoloader');
12+
13+
autoload_call_class('TestClass');
14+
var_dump(class_exists('TestClass', false));
15+
?>
16+
--EXPECT--
17+
bool(true)
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
--TEST--
2+
Test autoload_call_class() with invalid symbol name
3+
--FILE--
4+
<?php
5+
function customAutoloader($name) {
6+
var_dump($name);
7+
}
8+
autoload_register_class('customAutoloader');
9+
10+
try {
11+
autoload_call_class('12ayhs');
12+
} catch (\Throwable $e) {
13+
echo $e::class, ': ', $e->getMessage();
14+
}
15+
try {
16+
autoload_call_class('"');
17+
} catch (\Throwable $e) {
18+
echo $e::class, ': ', $e->getMessage();
19+
}
20+
try {
21+
autoload_call_class('');
22+
} catch (\Throwable $e) {
23+
echo $e::class, ': ', $e->getMessage();
24+
}
25+
try {
26+
autoload_call_class("al\no");
27+
} catch (\Throwable $e) {
28+
echo $e::class, ': ', $e->getMessage();
29+
}
30+
?>
31+
--EXPECT--
32+
string(6) "12ayhs"
33+
string(1) """
34+
string(0) ""
35+
string(4) "al
36+
o"

ext/spl/tests/spl_autoload_called_scope.phpt renamed to Zend/tests/autoloading/class/autoload_called_scope.phpt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
--TEST--
2-
SPL autoloader should not do anything magic with called scope
2+
Autoloader should not do anything magic with called scope
33
--FILE--
44
<?php
55

66
class Test {
77
public static function register() {
8-
spl_autoload_register([Test::class, 'autoload']);
8+
autoload_register_class([Test::class, 'autoload']);
99
}
1010

1111
public static function autoload($class) {
@@ -15,7 +15,7 @@ class Test {
1515

1616
class Test2 extends Test {
1717
public static function register() {
18-
spl_autoload_register([Test2::class, 'autoload']);
18+
autoload_register_class([Test2::class, 'autoload']);
1919
}
2020

2121
public static function runTest() {
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
--TEST--
2+
Dynamic autoload with invalid symbol name in variable
3+
--FILE--
4+
<?php
5+
function customAutoloader($name) {
6+
var_dump($name);
7+
}
8+
autoload_register_class('customAutoloader');
9+
$name = '12ayhs';
10+
11+
try {
12+
new $name;
13+
} catch (\Throwable $e) {
14+
echo $e::class, ': ', $e->getMessage(), \PHP_EOL;
15+
}
16+
$name = '"';
17+
try {
18+
new $name;
19+
} catch (\Throwable $e) {
20+
echo $e::class, ': ', $e->getMessage(), \PHP_EOL;
21+
}
22+
$name = '';
23+
try {
24+
new $name;
25+
} catch (\Throwable $e) {
26+
echo $e::class, ': ', $e->getMessage(), \PHP_EOL;
27+
}
28+
$name = "al\no";
29+
try {
30+
new $name;
31+
} catch (\Throwable $e) {
32+
echo $e::class, ': ', $e->getMessage(), \PHP_EOL;
33+
}
34+
?>
35+
--EXPECT--
36+
string(6) "12ayhs"
37+
Error: Class "12ayhs" not found
38+
Error: Class """ not found
39+
Error: Class "" not found
40+
Error: Class "al
41+
o" not found
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
--TEST--
2+
Autoloading with closures and invocables
3+
--FILE--
4+
<?php
5+
$closure = function ($name) {
6+
echo 'autoload(' . $name . ")\n";
7+
};
8+
9+
class Autoloader {
10+
public function __construct(private string $dir) {}
11+
public function __invoke($class) {
12+
echo ("Autoloader('{$this->dir}') called with $class\n");
13+
}
14+
}
15+
16+
class WorkingAutoloader {
17+
public function __invoke($class) {
18+
echo ("WorkingAutoloader() called with $class\n");
19+
eval("class $class { }");
20+
}
21+
}
22+
23+
$al1 = new Autoloader('d1');
24+
$al2 = new WorkingAutoloader('d2');
25+
26+
autoload_register_class($closure);
27+
autoload_register_class($al1);
28+
autoload_register_class($al2);
29+
30+
var_dump(autoload_list_class());
31+
32+
$x = new TestX;
33+
34+
autoload_unregister_class($closure);
35+
autoload_unregister_class($al1);
36+
37+
$y = new TestY;
38+
39+
?>
40+
--EXPECTF--
41+
array(3) {
42+
[0]=>
43+
object(Closure)#1 (4) {
44+
["name"]=>
45+
string(%d) "{closure:%s:%d}"
46+
["file"]=>
47+
string(%d) "%s"
48+
["line"]=>
49+
int(%d)
50+
["parameter"]=>
51+
array(1) {
52+
["$name"]=>
53+
string(10) "<required>"
54+
}
55+
}
56+
[1]=>
57+
object(Autoloader)#2 (1) {
58+
["dir":"Autoloader":private]=>
59+
string(2) "d1"
60+
}
61+
[2]=>
62+
object(WorkingAutoloader)#3 (0) {
63+
}
64+
}
65+
autoload(TestX)
66+
Autoloader('d1') called with TestX
67+
WorkingAutoloader() called with TestX
68+
WorkingAutoloader() called with TestY

Zend/tests/bug42798.phpt renamed to Zend/tests/autoloading/class/bug42798.phpt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
--TEST--
2-
Bug #42798 (_autoload() not triggered for classes used in method signature)
2+
Bug #42798 (Autoloading not triggered for classes used in method signature)
33
--FILE--
44
<?php
5-
spl_autoload_register(function ($className) {
5+
autoload_register_class(function ($className) {
66
print "$className\n";
77
exit();
88
});

Zend/tests/bug46665.phpt renamed to Zend/tests/autoloading/class/bug46665.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ Bug #46665 (Triggering autoload with a variable classname causes truncated autol
33
--FILE--
44
<?php
55

6-
spl_autoload_register(function ($class) {
6+
autoload_register_class(function ($class) {
77
var_dump($class);
88
require __DIR__ .'/bug46665_autoload.inc';
99
});

ext/spl/tests/bug65006.phpt renamed to Zend/tests/autoloading/class/bug65006.phpt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
11
--TEST--
2-
Bug #65006: spl_autoload_register fails with multiple callables using self, same method
2+
Bug #65006: autoload_register_class fails with multiple callables using self, same method
33
--FILE--
44
<?php
55

66
class first {
77
public static function init() {
8-
spl_autoload_register(array('self','load'));
8+
autoload_register_class(array('self','load'));
99
}
1010
public static function load($class) {}
1111
}
1212

1313
class second {
1414
public static function init() {
15-
spl_autoload_register(array('self','load'));
15+
autoload_register_class(array('self','load'));
1616
}
1717
public static function load($class){}
1818
}
1919

2020
first::init();
2121
second::init();
22-
var_dump(spl_autoload_functions());
22+
var_dump(autoload_list_class());
2323

2424
?>
2525
--EXPECTF--
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
--TEST--
2+
Bug #71204 (segfault if clean autoloaders while autoloading)
3+
--FILE--
4+
<?php
5+
6+
autoload_register_class(function ($name) {
7+
autoload_unregister_class("autoload_unregister_class");
8+
});
9+
10+
autoload_register_class(function ($name) {
11+
});
12+
13+
new A();
14+
?>
15+
--EXPECTF--
16+
Fatal error: Uncaught Error: Class "A" not found in %s:%d
17+
Stack trace:
18+
#0 {main}
19+
thrown in %sbug71204.php on line %d

ext/spl/tests/bug73896.phpt renamed to Zend/tests/autoloading/class/bug73896.phpt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,24 @@
11
--TEST--
2-
Bug #73896 (spl_autoload() crashes when calls magic _call())
2+
Bug #73896 (autoload_register_class() crashes when calls magic __call())
33
--FILE--
44
<?php
55
class Registrator {
6-
public static function call($callable, array $args) {
6+
public static function call($callable, array $args) {
77
return call_user_func_array($callable, [$args]);
88
}
99
}
1010

1111
class teLoader {
1212
public function __construct() {
13-
Registrator::call('spl_autoload_register', [$this, 'autoload']);
13+
Registrator::call('autoload_register_class', [$this, 'autoload']);
1414
}
1515

1616
public function __call($method, $args) {
1717
$this->doSomething();
1818
}
1919

2020
protected function autoload($class) {
21+
// TODO Should this actually be called?
2122
die("Protected autoload() called!\n");
2223
}
2324

ext/spl/tests/spl_autoload_011.phpt renamed to Zend/tests/autoloading/class/destructor_call.phpt

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
--TEST--
2-
SPL: spl_autoload() and object freed
3-
--INI--
4-
include_path=.
2+
Destructor call of autoloader when object freed
53
--FILE--
64
<?php
75
class A {
@@ -17,7 +15,7 @@ class A {
1715
$a = new A;
1816
$a->var = 2;
1917

20-
spl_autoload_register(array($a, 'autoload'));
18+
autoload_register_class(array($a, 'autoload'));
2119
unset($a);
2220

2321
var_dump(class_exists("C", true));

ext/spl/tests/bug74372.phpt renamed to Zend/tests/autoloading/class/emit-parse-error-in-autoloader.phpt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
--TEST--
2-
Bug #74372: autoloading file with syntax error uses next autoloader, may hide parse error
2+
Parse errors should be thrown if occuring from an autoloader
33
--FILE--
44
<?php
55

6-
spl_autoload_register(function($class) {
6+
autoload_register_class(function($class) {
77
eval("ha ha ha");
88
});
9-
spl_autoload_register(function($class) {
9+
autoload_register_class(function($class) {
1010
echo "Don't call me.\n";
1111
});
1212

ext/spl/tests/spl_autoload_003.phpt renamed to Zend/tests/autoloading/class/exceptions_during_autoloading001.phpt

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,28 @@
11
--TEST--
2-
SPL: spl_autoload() and friends
3-
--INI--
4-
include_path=.
2+
Exception thrown from within autoloading function
53
--FILE--
64
<?php
75

8-
function TestFunc1($classname)
9-
{
6+
function TestFunc1($classname) {
107
echo __METHOD__ . "($classname)\n";
118
}
129

13-
function TestFunc2($classname)
14-
{
10+
function TestFunc2($classname) {
1511
echo __METHOD__ . "($classname)\n";
1612
throw new Exception("Class $classname missing");
1713
}
1814

19-
function TestFunc3($classname)
20-
{
15+
function TestFunc3($classname) {
2116
echo __METHOD__ . "($classname)\n";
2217
}
2318

24-
spl_autoload_register("TestFunc1");
25-
spl_autoload_register("TestFunc2");
26-
spl_autoload_register("TestFunc3");
19+
autoload_register_class("TestFunc1");
20+
autoload_register_class("TestFunc2");
21+
autoload_register_class("TestFunc3");
2722

28-
try
29-
{
23+
try {
3024
var_dump(class_exists("TestClass", true));
31-
}
32-
catch(Exception $e)
33-
{
25+
} catch(Exception $e) {
3426
echo 'Exception: ' . $e->getMessage() . "\n";
3527
}
3628

0 commit comments

Comments
 (0)