forked from HDInnovations/UNIT3D-Community-Edition
-
Notifications
You must be signed in to change notification settings - Fork 0
/
preload.php
149 lines (120 loc) · 3.38 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
<?php
opcache_compile_file(__DIR__.'/vendor/autoload.php');
class Preloader
{
private array $ignores = [];
private static int $count = 0;
private array $paths;
private array $fileMap;
public function __construct(string ...$paths)
{
$this->paths = $paths;
$classMap = require __DIR__.'/vendor/composer/autoload_classmap.php';
$this->fileMap = \array_flip($classMap);
}
public function paths(string ...$paths): self
{
$this->paths = \array_merge(
$this->paths,
$paths
);
return $this;
}
public function ignore(string ...$names): self
{
$this->ignores = \array_merge(
$this->ignores,
$names
);
return $this;
}
public function load(): void
{
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 (\is_dir($path)) {
$this->loadDir($path);
return;
}
$this->loadFile($path);
}
private function loadDir(string $path): void
{
$handle = \opendir($path);
while ($file = \readdir($handle)) {
if (\in_array($file, ['.', '..'])) {
continue;
}
$this->loadPath("{$path}/{$file}");
}
\closedir($handle);
}
private function loadFile(string $path): void
{
$class = $this->fileMap[$path] ?? null;
if ($this->shouldIgnore($class)) {
return;
}
\opcache_compile_file($path);
self::$count++;
echo "[Preloader] Preloaded `{$class}`".PHP_EOL;
}
private function shouldIgnore(?string $name): bool
{
if ($name === null) {
return true;
}
foreach ($this->ignores as $ignore) {
if (\str_starts_with($name, $ignore)) {
return true;
}
}
return false;
}
}
(new Preloader())
->paths(
__DIR__.'/vendor/psr',
__DIR__.'/vendor/monolog',
__DIR__.'/vendor/doctrine',
__DIR__.'/vendor/guzzlehttp',
__DIR__.'/vendor/ramsey/uuid',
__DIR__.'/vendor/ramsey/collection',
__DIR__.'/vendor/vlucas/phpdotenv',
__DIR__.'/vendor/symfony',
__DIR__.'/vendor/laravel',
__DIR__.'/app/Http/Controllers/AnnounceController.php',
__DIR__.'/app/Jobs/ProcessAnnounce.php',
)
->ignore(
'Laravel\Telescope',
'Laravel\Tinker',
'Illuminate\Queue',
'Illuminate\Contracts\Queue',
'Illuminate\View',
'Illuminate\Contracts\View',
'Illuminate\Foundation\Console',
'Illuminate\Notification',
'Illuminate\Contracts\Notifications',
'Illuminate\Bus',
'Illuminate\Session',
'Illuminate\Contracts\Session',
'Illuminate\Console',
'Illuminate\Testing',
'Illuminate\Http\Testing',
'Illuminate\Support\Testing',
'Illuminate\Cookie',
'Illuminate\Contracts\Cookie',
'Illuminate\Broadcasting',
'Illuminate\Contracts\Broadcasting',
'Illuminate\Mail',
'Illuminate\Carbon',
'Illuminate\Contracts\Mail',
)
->load();