Skip to content

Commit 6706b47

Browse files
committed
Cache can be forced by static setting under kernel
1 parent 86a3760 commit 6706b47

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

AsyncKernel.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,15 @@
3939
abstract class AsyncKernel extends Kernel implements CompilerPassInterface
4040
{
4141
private string $uid;
42+
private bool $forceCacheUsage = false;
43+
44+
/**
45+
* @return void
46+
*/
47+
public function forceCacheUsage()
48+
{
49+
$this->forceCacheUsage = true;
50+
}
4251

4352
/**
4453
* {@inheritdoc}
@@ -48,7 +57,7 @@ public function boot()
4857
if (!$this->booted) {
4958
$this->uid = $this->generateUID();
5059

51-
if (($_ENV['DRIFT_CACHE_ENABLED'] ?? '0') !== '1') {
60+
if (!$this->forceCacheUsage && ($_ENV['DRIFT_CACHE_ENABLED'] ?? '0') !== '1') {
5261
$fs = new Filesystem();
5362
// AsyncKernel loads the container only once when it loads. Storing it in the filesystem is not for cache purposes
5463
// but more for using the same loading process as Kernel class use.

Tests/Base/KernelCacheTest.php

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,25 @@ public function testWithoutCache()
3838
unset($_ENV['DRIFT_CACHE_ENABLED']);
3939
}
4040

41-
public function testWithCache()
41+
public function testWithCacheFromStaticSetting()
42+
{
43+
static::$kernel = static::getKernel();
44+
static::$kernel->forceCacheUsage();
45+
static::$kernel->boot();
46+
47+
$cacheDir = self::$kernel->getCacheDir();
48+
$firstKernelCacheCreationTime = lstat($cacheDir)['ctime'];
49+
sleep(1);
50+
51+
static::$kernel = static::getKernel();
52+
static::$kernel->forceCacheUsage();
53+
$cacheDir = self::$kernel->getCacheDir();
54+
$secondKernelCacheCreationTime = lstat($cacheDir)['ctime'];
55+
56+
$this->assertEquals($firstKernelCacheCreationTime, $secondKernelCacheCreationTime);
57+
}
58+
59+
public function testWithCacheWithEnv()
4260
{
4361
$_ENV['DRIFT_CACHE_ENABLED'] = '1';
4462
static::setUpBeforeClass();

0 commit comments

Comments
 (0)