Skip to content

Commit

Permalink
Add test for #1969 OPCache preloading
Browse files Browse the repository at this point in the history
  • Loading branch information
kylekatarnls committed Dec 20, 2019
1 parent 83ee766 commit 0cf61e5
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .travis.yml
Expand Up @@ -20,6 +20,7 @@ env:
- coverage=false
- phpstan=false
- laravel=false
- preload=false

sudo: false

Expand All @@ -33,6 +34,8 @@ before_install:
- if [[ $laravel = 'true' ]]; then printf "\n" | pecl install -f redis; fi
- if [[ $laravel = 'true' ]]; then travis_retry composer self-update; fi
- if [[ $laravel = 'true' ]]; then mysql -e 'CREATE DATABASE forge;'; fi
- if [[ $preload = 'true' ]]; then cp tests/preload.php ~/preload.php; fi
- if [[ $preload = 'true' ]]; then echo "opcache.preload = ~/preload.php" >> ~/.phpenv/versions/$(phpenv version-name)/etc/php.ini; fi

install:
- composer remove --no-update friendsofphp/php-cs-fixer
Expand Down Expand Up @@ -104,6 +107,8 @@ matrix:
- coverage=true
- php: 7.4
env: setup=lowest
- php: 7.4
env: preload=true
- php: 7.4
- php: nightly
env: setup=nightly
Expand Down
1 change: 1 addition & 0 deletions phpstan.neon
Expand Up @@ -11,3 +11,4 @@ parameters:
- '*/tests/Laravel/*.php'
- '*/tests/Cli/*.php'
- '*/tests/CarbonPeriod/Fixtures/filters.php'
- '*/tests/preload.php'
114 changes: 114 additions & 0 deletions tests/preload.php
@@ -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();

0 comments on commit 0cf61e5

Please sign in to comment.