Skip to content

Commit 958ece4

Browse files
committed
feat: move autoload functionality to its own file
1 parent 19ea700 commit 958ece4

1 file changed

Lines changed: 19 additions & 61 deletions

File tree

Sources/Autoloader.php

Lines changed: 19 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -15,74 +15,32 @@
1515

1616
namespace SMF;
1717

18-
/*
19-
* An autoloader for certain classes.
20-
*
21-
* @param string $class The fully-qualified class name.
22-
*/
23-
spl_autoload_register(function ($class) {
24-
static $hook_value = '';
2518

26-
static $class_map = [
27-
// Some special cases.
28-
'ReCaptcha\\' => 'ReCaptcha/',
29-
'MatthiasMullie\\Minify\\' => 'minify/src/',
30-
'MatthiasMullie\\PathConverter\\' => 'minify/path-converter/src/',
31-
'ZxcvbnPhp\\' => 'ZxcvbnPhp/',
19+
$loader = require Config::$vendordir . '/autoload.php';
20+
$third_party_mappers = [];
3221

33-
// In general, the SMF namespace maps to $sourcedir.
34-
'SMF\\' => '',
35-
];
22+
// Ensure $sourcedir is set to something valid.
23+
if (class_exists(Config::class, false) && isset(Config::$sourcedir)) {
24+
$sourcedir = Config::$sourcedir;
25+
}
3626

37-
// Ensure $sourcedir is set to something valid.
38-
if (class_exists(Config::class, false) && isset(Config::$sourcedir)) {
39-
$sourcedir = Config::$sourcedir;
40-
}
27+
if (empty($sourcedir) || !is_dir($sourcedir)) {
28+
$sourcedir = __DIR__;
29+
}
4130

42-
if (empty($sourcedir) || !is_dir($sourcedir)) {
43-
$sourcedir = __DIR__;
31+
// Do any third-party scripts want in on the fun?
32+
if (!defined('SMF_INSTALLING') && class_exists(Config::class, false)) {
33+
if (!class_exists(IntegrationHook::class, false) && is_file($sourcedir . '/IntegrationHook.php')) {
34+
require_once $sourcedir . '/IntegrationHook.php';
4435
}
4536

46-
// Do any third-party scripts want in on the fun?
47-
if (!defined('SMF_INSTALLING') && class_exists(Config::class, false) && $hook_value !== (Config::$modSettings['integrate_autoload'] ?? '')) {
48-
if (!class_exists(IntegrationHook::class, false) && is_file($sourcedir . '/IntegrationHook.php')) {
49-
require_once $sourcedir . '/IntegrationHook.php';
50-
}
51-
52-
if (class_exists(IntegrationHook::class, false)) {
53-
$hook_value = Config::$modSettings['integrate_autoload'];
54-
IntegrationHook::call('integrate_autoload', [&$class_map]);
55-
}
37+
if (class_exists(IntegrationHook::class, false)) {
38+
IntegrationHook::call('integrate_autoload', [&$third_party_mappers]);
5639
}
40+
}
5741

58-
foreach ($class_map as $prefix => $dirname) {
59-
// Does the class use the namespace prefix?
60-
$len = strlen($prefix);
61-
62-
if (strncmp($prefix, $class, $len) !== 0) {
63-
continue;
64-
}
65-
66-
// Get the relative class name.
67-
$relative_class = substr($class, $len);
68-
69-
// Replace the namespace prefix with the base directory, replace namespace
70-
// separators with directory separators in the relative class name, append
71-
// with .php
72-
$filename = $dirname . strtr($relative_class, '\\', '/') . '.php';
73-
74-
// Failsafe: Never load a file named index.php.
75-
if (basename($filename) === 'index.php') {
76-
return;
77-
}
78-
79-
// If the file exists, require it.
80-
if (file_exists($filename = $sourcedir . '/' . $filename)) {
81-
require $filename;
82-
83-
return;
84-
}
85-
}
86-
});
42+
foreach ($third_party_mappers as $prefix => $dirname) {
43+
$loader->addPsr4($prefix, $dirname);
44+
}
8745

8846
?>

0 commit comments

Comments
 (0)