forked from phroggyy/laracon2019
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpreload.php
176 lines (140 loc) · 4.21 KB
/
preload.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
<?php
require_once __DIR__ . '/vendor/autoload.php';
/**
* Class Preloader
*
* Taken from (and modified):
* @see https://stitcher.io/blog/preloading-in-php-74
*/
class Preloader
{
private array $ignoredClasses = [];
private static int $count = 0;
private array $paths;
private array $fileMap;
private array $ignoredPaths = [];
public function __construct(string ...$paths)
{
$this->paths = $paths;
// We'll use composer's classmap
// to easily find which classes to autoload,
// based on their filename
$classMap = require __DIR__ . '/vendor/composer/autoload_classmap.php';
$this->fileMap = array_flip($classMap);
}
public function paths(string ...$paths): Preloader
{
$this->paths = array_merge(
$this->paths,
$paths
);
return $this;
}
public function ignoreClasses(string ...$names): Preloader
{
$this->ignoredClasses = array_merge(
$this->ignoredClasses,
$names
);
return $this;
}
public function ignorePaths(string ...$paths): Preloader
{
$this->ignoredPaths = array_merge(
$this->ignoredPaths,
$paths
);
return $this;
}
public function load(): void
{
// We'll loop over all registered paths
// and load them one by one
foreach ($this->paths as $path) {
$this->loadPath(rtrim($path, '/'));
}
$count = self::$count;
echo "[Preloader] Preloaded {$count} classes" . PHP_EOL;
}
private function loadPath(string $path): void
{
// If the current path is a directory,
// we'll load all files in it
if (is_dir($path)) {
$this->loadDir($path);
return;
}
// Otherwise we'll just load this one file
$this->loadFile($path);
}
private function loadDir(string $path): void
{
$handle = opendir($path);
// We'll loop over all files and directories
// in the current path,
// and load them one by one
while ($file = readdir($handle)) {
if (in_array($file, ['.', '..'])) {
continue;
}
$this->loadPath("{$path}/{$file}");
}
closedir($handle);
}
private function loadFile(string $path): void
{
// We resolve the classname from composer's autoload mapping
$class = $this->fileMap[$path] ?? null;
// And use it to make sure the class, or the file pattern, shouldn't be ignored
if ($this->shouldIgnoreClass($class) || $this->shouldIgnorePath($path)) {
return;
}
// Finally we require the path,
// causing all its dependencies to be loaded as well
require_once($path);
self::$count++;
echo "[Preloader] Preloaded `{$class}`" . PHP_EOL;
}
private function shouldIgnoreClass(?string $name): bool
{
if ($name === null) {
return true;
}
foreach ($this->ignoredClasses as $ignore) {
if (strpos($name, $ignore) === 0) {
return true;
}
}
return false;
}
private function shouldIgnorePath(string $path): bool
{
foreach ($this->ignoredPaths as $ignoredPath) {
$ignoredPath = preg_quote($ignoredPath, '/');
if (preg_match("/$ignoredPath/", $path) === 1) {
return true;
}
}
return false;
}
}
// we ignore the testing path since we composer install --no-dev which means phpunit isn't in our vendor dir
(new Preloader())
->paths(
__DIR__ . '/vendor/laravel/framework',
__DIR__ . '/app/Providers',
__DIR__ . '/app/Http/Middleware',
__DIR__ . '/app/Http/Controllers'
)
->ignorePaths(
'Illuminate/Foundation/Testing',
'Illuminate/Testing'
)
->ignoreClasses(
\Illuminate\Filesystem\Cache::class,
\Illuminate\Log\LogManager::class,
\Illuminate\Http\Testing\File::class,
\Illuminate\Http\UploadedFile::class,
\Illuminate\Support\Carbon::class
)
->load();