-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add test for #1969 OPCache preloading
- Loading branch information
1 parent
83ee766
commit 0cf61e5
Showing
3 changed files
with
120 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
<?php | ||
|
||
require_once __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): Preloader | ||
{ | ||
$this->paths = array_merge( | ||
$this->paths, | ||
$paths | ||
); | ||
|
||
return $this; | ||
} | ||
|
||
public function ignore(string ...$names): Preloader | ||
{ | ||
$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; | ||
} | ||
|
||
require_once($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 (strpos($name, $ignore) === 0) { | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
} | ||
|
||
(new Preloader()) | ||
->paths( | ||
__DIR__ . '/src', | ||
) | ||
->load(); |