-
Notifications
You must be signed in to change notification settings - Fork 4
/
StaticConstructorLoader.php
178 lines (150 loc) · 4.93 KB
/
StaticConstructorLoader.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
<?php
declare(strict_types=1);
namespace Dbalabka\StaticConstructorLoader;
use Composer\Autoload\ClassLoader;
use Dbalabka\StaticConstructorLoader\Exception\StaticConstructorLoaderException;
/**
* Decorates the Composer autoloader to statically initialize the class.
* This is a very lightweight workaround which is described in https://wiki.php.net/rfc/static_class_constructor
*
* @author Dmitrijs Balabka <[email protected]>
*/
final class StaticConstructorLoader extends ClassLoader /* extending for an contract */
{
/**
* @var ClassLoader
*/
private $classLoader;
public function __construct(ClassLoader $classLoader)
{
$this->classLoader = $classLoader;
// find Composer autoloader
$loaders = spl_autoload_functions();
$otherLoaders = [];
$composerLoader = null;
foreach ($loaders as $loader) {
if (is_array($loader)) {
if ($loader[0] === $classLoader) {
$composerLoader = $loader;
break;
}
if ($loader[0] instanceof self) {
throw new StaticConstructorLoaderException(sprintf('%s already registered', self::class));
}
}
$otherLoaders[] = $loader;
}
if (!$composerLoader) {
throw new StaticConstructorLoaderException(sprintf('%s was not found in registered autoloaders', ClassLoader::class));
}
// unregister Composer autoloader and all preceding autoloaders
array_map('spl_autoload_unregister', array_merge($otherLoaders, [$composerLoader]));
// restore the original queue order
$loadersToRestore = array_merge([[$this, 'loadClass']], array_reverse($otherLoaders));
$flagTrue = array_fill(0, count($loadersToRestore), true);
array_map('spl_autoload_register', $loadersToRestore, $flagTrue, $flagTrue);
}
public function loadClass($className): ?bool
{
$result = $this->classLoader->loadClass($className);
if ($result === true && $className !== StaticConstructorInterface::class && is_a($className, StaticConstructorInterface::class, true)) {
$className::__constructStatic();
}
return $result;
}
/** @codeCoverageIgnore */
public function getPrefixes()
{
return $this->classLoader->getPrefixes();
}
/** @codeCoverageIgnore */
public function getPrefixesPsr4()
{
return $this->classLoader->getPrefixesPsr4();
}
/** @codeCoverageIgnore */
public function getFallbackDirs()
{
return $this->classLoader->getFallbackDirs();
}
/** @codeCoverageIgnore */
public function getFallbackDirsPsr4()
{
return $this->classLoader->getFallbackDirsPsr4();
}
/** @codeCoverageIgnore */
public function getClassMap()
{
return $this->classLoader->getClassMap();
}
/** @codeCoverageIgnore */
public function addClassMap(array $classMap)
{
$this->classLoader->addClassMap($classMap);
}
/** @codeCoverageIgnore */
public function add($prefix, $paths, $prepend = false)
{
$this->classLoader->add($prefix, $paths, $prepend);
}
/** @codeCoverageIgnore */
public function addPsr4($prefix, $paths, $prepend = false)
{
$this->classLoader->addPsr4($prefix, $paths, $prepend);
}
/** @codeCoverageIgnore */
public function set($prefix, $paths)
{
$this->classLoader->set($prefix, $paths);
}
/** @codeCoverageIgnore */
public function setPsr4($prefix, $paths)
{
$this->classLoader->setPsr4($prefix, $paths);
}
/** @codeCoverageIgnore */
public function setUseIncludePath($useIncludePath)
{
$this->classLoader->setUseIncludePath($useIncludePath);
}
/** @codeCoverageIgnore */
public function getUseIncludePath()
{
return $this->classLoader->getUseIncludePath();
}
/** @codeCoverageIgnore */
public function setClassMapAuthoritative($classMapAuthoritative)
{
$this->classLoader->setClassMapAuthoritative($classMapAuthoritative);
}
/** @codeCoverageIgnore */
public function isClassMapAuthoritative()
{
return $this->classLoader->isClassMapAuthoritative();
}
/** @codeCoverageIgnore */
public function setApcuPrefix($apcuPrefix)
{
$this->classLoader->setApcuPrefix($apcuPrefix);
}
/** @codeCoverageIgnore */
public function getApcuPrefix()
{
return $this->classLoader->getApcuPrefix();
}
/** @codeCoverageIgnore */
public function register($prepend = false)
{
$this->classLoader->register($prepend);
}
/** @codeCoverageIgnore */
public function unregister()
{
$this->classLoader->unregister();
}
/** @codeCoverageIgnore */
public function findFile($class)
{
return $this->classLoader->findFile($class);
}
}