diff --git a/src/PreloaderLister.php b/src/PreloaderLister.php index e7d568c..b0ab6fe 100644 --- a/src/PreloaderLister.php +++ b/src/PreloaderLister.php @@ -48,8 +48,11 @@ class PreloaderLister */ public function build() : array { + // Exclude "$PRELOAD$" phantom file + $scripts = $this->excludePreloadVariable($this->list); + // Exclude the files set by the developer - $scripts = $this->exclude($this->list); + $scripts = $this->exclude($scripts); // Sort the scripts by hit ratio $scripts = $this->sortScripts($scripts); @@ -64,6 +67,19 @@ public function build() : array return array_unique($scripts); } + /** + * Excludes de `$PRELOAD$` file key from the list. + * + * @param array $list + * @return array + */ + protected function excludePreloadVariable(array $list) + { + unset($list['$PRELOAD$']); + + return $list; + } + /** * Retrieve a sorted scripts list used by Opcache. * diff --git a/tests/PreloaderTest.php b/tests/PreloaderTest.php index ee35244..860552b 100644 --- a/tests/PreloaderTest.php +++ b/tests/PreloaderTest.php @@ -381,6 +381,28 @@ public function test_uses_require_instead_of_compile() $this->assertStringContainsString('require_once $file', $contents); } + public function test_excludes_phantom_preload_variable() + { + $this->list['$PRELOAD$'] = [ + 'full_path' => '$PRELOAD$', + 'hits' => 0, + 'memory_consumption' => 560, + 'last_used' => 'Thu Jan 1 01:00:00 1970', + 'last_used_timestamp' => 0, + 'timestamp' => 0 + ]; + + $this->mockOpcache(); + + $preloader = new Preloader(new PreloaderCompiler, new PreloaderLister, $this->opcache); + + $preloader->writeTo($this->preloaderPath); + + $contents = file_get_contents($this->preloaderPath); + + $this->assertStringNotContainsString('$PRELOAD$', $contents); + } + public function test_exception_when_autoload_doesnt_exists() { $this->expectException(LogicException::class);