Skip to content

Commit 64342e6

Browse files
committed
bug #38063 [FrameworkBundle] generate preload.php in src/ to make opcache.preload predictable (nicolas-grekas)
This PR was merged into the 4.4 branch. Discussion ---------- [FrameworkBundle] generate preload.php in src/ to make opcache.preload predictable | Q | A | ------------- | --- | Branch? | 4.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | - | License | MIT | Doc PR | - As seen in symfonycorp/cloud-templates#15, having the path of the preload file vary by env+debug makes configuring PHP.ini settings impossible. This PR dump a new `preload.php` file in `src/` when `cache:clear` is called. This makes the path predictable. This is submitted as a bugfix because the current behavior is barely usable without this change. Commits ------- 54c965c7d0 [FrameworkBundle] generate preload.php in src/ to make opcache.preload predictable
2 parents 135f964 + 980947b commit 64342e6

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)