/
Autoloader.php
181 lines (155 loc) · 5.48 KB
/
Autoloader.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
<?php
namespace Laminas\ZendFrameworkBridge;
use ArrayObject;
use Composer\Autoload\ClassLoader;
use RuntimeException;
use function array_values;
use function class_alias;
use function class_exists;
use function explode;
use function file_exists;
use function getenv;
use function interface_exists;
use function spl_autoload_register;
use function strlen;
use function strtr;
use function substr;
use function trait_exists;
/**
* Alias legacy Zend Framework project classes/interfaces/traits to Laminas equivalents.
*/
class Autoloader
{
private const UPSTREAM_COMPOSER_VENDOR_DIRECTORY = __DIR__ . '/../../..';
private const LOCAL_COMPOSER_VENDOR_DIRECTORY = __DIR__ . '/../vendor';
/**
* Attach autoloaders for managing legacy ZF artifacts.
*
* We attach two autoloaders:
*
* - The first is _prepended_ to handle new classes and add aliases for
* legacy classes. PHP expects any interfaces implemented, classes
* extended, or traits used when declaring class_alias() to exist and/or
* be autoloadable already at the time of declaration. If not, it will
* raise a fatal error. This autoloader helps mitigate errors in such
* situations.
*
* - The second is _appended_ in order to create aliases for legacy
* classes.
*/
public static function load()
{
$loaded = new ArrayObject([]);
$classLoader = self::getClassLoader();
if ($classLoader === null) {
return;
}
spl_autoload_register(self::createPrependAutoloader(
RewriteRules::namespaceReverse(),
$classLoader,
$loaded
), true, true);
spl_autoload_register(self::createAppendAutoloader(
RewriteRules::namespaceRewrite(),
$loaded
));
}
private static function getClassLoader(): ?ClassLoader
{
$composerVendorDirectory = getenv('COMPOSER_VENDOR_DIR');
if (is_string($composerVendorDirectory)) {
return self::getClassLoaderFromVendorDirectory($composerVendorDirectory);
}
return self::getClassLoaderFromVendorDirectory(self::UPSTREAM_COMPOSER_VENDOR_DIRECTORY)
?? self::getClassLoaderFromVendorDirectory(self::LOCAL_COMPOSER_VENDOR_DIRECTORY);
}
/**
* @return callable
*/
private static function createPrependAutoloader(array $namespaces, ClassLoader $classLoader, ArrayObject $loaded)
{
/**
* @param string $class Class name to autoload
* @return void
*/
return static function ($class) use ($namespaces, $classLoader, $loaded) {
if (isset($loaded[$class])) {
return;
}
$segments = explode('\\', $class);
$i = 0;
$check = '';
while (isset($segments[$i + 1], $namespaces[$check . $segments[$i] . '\\'])) {
$check .= $segments[$i] . '\\';
++$i;
}
if ($check === '') {
return;
}
if ($classLoader->loadClass($class)) {
$legacy = $namespaces[$check]
. strtr(substr($class, strlen($check)), [
'ApiTools' => 'Apigility',
'Mezzio' => 'Expressive',
'Laminas' => 'Zend',
]);
class_alias($class, $legacy);
}
};
}
/**
* @return callable
*/
private static function createAppendAutoloader(array $namespaces, ArrayObject $loaded)
{
/**
* @param string $class Class name to autoload
* @return void
*/
return static function ($class) use ($namespaces, $loaded) {
$segments = explode('\\', $class);
if ($segments[0] === 'ZendService' && isset($segments[1])) {
$segments[0] .= '\\' . $segments[1];
unset($segments[1]);
$segments = array_values($segments);
}
$i = 0;
$check = '';
// We are checking segments of the namespace to match quicker
while (isset($segments[$i + 1], $namespaces[$check . $segments[$i] . '\\'])) {
$check .= $segments[$i] . '\\';
++$i;
}
if ($check === '') {
return;
}
$alias = $namespaces[$check]
. strtr(substr($class, strlen($check)), [
'Apigility' => 'ApiTools',
'Expressive' => 'Mezzio',
'Zend' => 'Laminas',
'AbstractZendServer' => 'AbstractZendServer',
'ZendServerDisk' => 'ZendServerDisk',
'ZendServerShm' => 'ZendServerShm',
'ZendMonitor' => 'ZendMonitor',
]);
$loaded[$alias] = true;
if (class_exists($alias) || interface_exists($alias) || trait_exists($alias)) {
class_alias($alias, $class);
}
};
}
private static function getClassLoaderFromVendorDirectory(string $composerVendorDirectory): ?ClassLoader
{
$filename = rtrim($composerVendorDirectory, '/') . '/autoload.php';
if (!file_exists($filename)) {
return null;
}
/** @psalm-suppress MixedAssignment */
$loader = include $filename;
if (!$loader instanceof ClassLoader) {
return null;
}
return $loader;
}
}