Skip to content

Commit 980947b

Browse files
[FrameworkBundle] generate preload.php in src/ to make opcache.preload predictable
1 parent c822ce3 commit 980947b

File tree

3 files changed

+22
-0
lines changed

3 files changed

+22
-0
lines changed

Command/CacheClearCommand.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,20 @@ protected function execute(InputInterface $input, OutputInterface $output): int
170170
}
171171
}
172172

173+
$kernelDir = \dirname((new \ReflectionObject($kernel))->getFileName());
174+
$preloadFile = $fs->makePathRelative(\dirname($containerFile, 2), $kernelDir);
175+
$preloadFile .= substr_replace(basename($containerFile), '.preload', -4, 0);
176+
$preloadFile = var_export('/'.$preloadFile, true);
177+
@file_put_contents($kernelDir.'/preload.php', <<<EOPHP
178+
<?php
179+
180+
if (file_exists(__DIR__.$preloadFile)) {
181+
require __DIR__.$preloadFile;
182+
}
183+
184+
EOPHP
185+
);
186+
173187
if ($output->isVerbose()) {
174188
$io->comment('Finished');
175189
}

Tests/Command/CacheClearCommand/CacheClearCommandTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ protected function setUp(): void
3838
protected function tearDown(): void
3939
{
4040
$this->fs->remove($this->kernel->getProjectDir());
41+
$this->fs->remove(__DIR__.'/Fixture/preload.php');
4142
}
4243

4344
public function testCacheIsFreshAfterCacheClearedWithWarmup()
@@ -82,5 +83,7 @@ public function testCacheIsFreshAfterCacheClearedWithWarmup()
8283
$containerRef = new \ReflectionClass(require $containerFile);
8384
$containerFile = str_replace('tes_'.\DIRECTORY_SEPARATOR, 'test'.\DIRECTORY_SEPARATOR, $containerRef->getFileName());
8485
$this->assertMatchesRegularExpression(sprintf('/\'kernel.container_class\'\s*=>\s*\'%s\'/', $containerClass), file_get_contents($containerFile), 'kernel.container_class is properly set on the dumped container');
86+
87+
$this->assertFileEquals(__DIR__.'/Fixture/preload.php.expected', __DIR__.'/Fixture/preload.php');
8588
}
8689
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
3+
if (file_exists(__DIR__.'/test/var/cache/test/FixtureSymfony_Bundle_FrameworkBundle_Tests_Command_CacheClearCommand_Fixture_TestAppKernelTestDebugContainer.preload.php')) {
4+
require __DIR__.'/test/var/cache/test/FixtureSymfony_Bundle_FrameworkBundle_Tests_Command_CacheClearCommand_Fixture_TestAppKernelTestDebugContainer.preload.php';
5+
}

0 commit comments

Comments
 (0)