-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpreload.php
114 lines (98 loc) · 3.18 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
<?php
class Preloader
{
private static int $count = 0;
private array $paths;
private array $fileMap;
public function __construct(string ...$paths)
{
$this->paths = $paths;
$classMap = [];
$autoload = __DIR__ . '/vendor/composer/autoload_classmap.php';
if (file_exists($autoload)) {
$classMap = require $autoload;
}
$this->fileMap = array_flip($classMap);
}
public function paths(string ...$paths): Preloader
{
$this->paths = array_merge(
$this->paths,
$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;
if (!$class) {
return;
}
// Finally we require the path,
// causing all its dependencies to be loaded as well
@opcache_compile_file($path);
self::$count++;
echo "[Preloader] Preloaded `{$class}`" . PHP_EOL;
}
}
(new Preloader())
// ->paths(__DIR__ . '/vendor/elasticsearch')
// ->paths(__DIR__ . '/vendor/ezyang')
// ->paths(__DIR__ . '/vendor/erusev')
// ->paths(__DIR__ . '/vendor/graylog2')
// ->paths(__DIR__ . '/vendor/guzzlehttp')
// ->paths(__DIR__ . '/vendor/fideloper')
// ->paths(__DIR__ . '/vendor/filp')
// ->paths(__DIR__ . '/vendor/freelancehunt')
// ->paths(__DIR__ . '/vendor/jenssegers')
// ->paths(__DIR__ . '/vendor/symfony')
// ->paths(__DIR__ . '/vendor/laravel/passport')
// ->paths(__DIR__ . '/vendor/laravel/socialite')
// ->paths(__DIR__ . '/vendor/laravel/framework')
// ->paths(__DIR__ . '/vendor/laravelcollective')
// ->paths(__DIR__ . '/vendor/lcobucci')
// ->paths(__DIR__ . '/vendor/league')
// ->paths(__DIR__ . '/vendor/nesbot')
// ->paths(__DIR__ . '/vendor/predis')
// ->paths(__DIR__ . '/vendor/monolog')
// ->paths(__DIR__ . '/vendor/sentry')
// ->paths(__DIR__ . '/vendor/twig')
// ->paths(__DIR__ . '/vendor/vlucas')
// ->paths(__DIR__ . '/vendor/swiftmailer')
->load();