diff --git a/src/BrefKernel.php b/src/BrefKernel.php index f51789c..6e5b481 100644 --- a/src/BrefKernel.php +++ b/src/BrefKernel.php @@ -10,6 +10,13 @@ abstract class BrefKernel extends Kernel { + public function __construct(string $environment, bool $debug) + { + parent::__construct($environment, $debug); + + $this->configureCacheDir(); + } + public function isLambda(): bool { return getenv('LAMBDA_TASK_ROOT') !== false; @@ -135,6 +142,15 @@ private function ensureLogDir(string $writeLogDir): void $filesystem->mkdir($writeLogDir); } + private function configureCacheDir(): void + { + if (isset($_SERVER['APP_CACHE_DIR']) || ! $this->isLambda()) { + return; + } + + $_SERVER['APP_CACHE_DIR'] = '/tmp/cache'; + } + /** * This method logs to stderr. * diff --git a/tests/Unit/BrefKernelTest.php b/tests/Unit/BrefKernelTest.php index 8cf31e1..95791cc 100644 --- a/tests/Unit/BrefKernelTest.php +++ b/tests/Unit/BrefKernelTest.php @@ -7,6 +7,42 @@ class BrefKernelTest extends TestCase { + /** @var string|false */ + private $lambdaTaskRoot; + + /** @var string|false */ + private $appCacheDir; + + /** @var string|null */ + private $serverAppCacheDir; + + protected function setUp(): void + { + parent::setUp(); + + $this->lambdaTaskRoot = getenv('LAMBDA_TASK_ROOT'); + $this->appCacheDir = getenv('APP_CACHE_DIR'); + $this->serverAppCacheDir = $_SERVER['APP_CACHE_DIR'] ?? null; + + putenv('LAMBDA_TASK_ROOT'); + putenv('APP_CACHE_DIR'); + unset($_SERVER['APP_CACHE_DIR']); + } + + protected function tearDown(): void + { + $this->restoreEnvironmentVariable('LAMBDA_TASK_ROOT', $this->lambdaTaskRoot); + $this->restoreEnvironmentVariable('APP_CACHE_DIR', $this->appCacheDir); + + if ($this->serverAppCacheDir === null) { + unset($_SERVER['APP_CACHE_DIR']); + } else { + $_SERVER['APP_CACHE_DIR'] = $this->serverAppCacheDir; + } + + parent::tearDown(); + } + public function testIsLambda() { $kernel = new TestKernel; @@ -15,4 +51,52 @@ public function testIsLambda() putenv('LAMBDA_TASK_ROOT=/var/task'); self::assertTrue($kernel->isLambda()); } + + public function testItConfiguresSymfonyCacheDirOnLambda(): void + { + putenv('LAMBDA_TASK_ROOT=/var/task'); + + new TestKernel; + + self::assertSame('/tmp/cache', $_SERVER['APP_CACHE_DIR']); + } + + public function testItDoesNotConfigureSymfonyCacheDirOutsideLambda(): void + { + new TestKernel; + + self::assertArrayNotHasKey('APP_CACHE_DIR', $_SERVER); + } + + public function testItDoesNotOverrideSymfonyCacheDir(): void + { + putenv('LAMBDA_TASK_ROOT=/var/task'); + $_SERVER['APP_CACHE_DIR'] = '/custom/cache'; + + new TestKernel; + + self::assertSame('/custom/cache', $_SERVER['APP_CACHE_DIR']); + } + + public function testItUsesConfiguredSymfonyCacheDirEnvironmentVariable(): void + { + putenv('LAMBDA_TASK_ROOT=/var/task'); + $_SERVER['APP_CACHE_DIR'] = '/custom/cache'; + + new TestKernel; + + self::assertSame('/custom/cache', $_SERVER['APP_CACHE_DIR']); + } + + /** + * @param string|false $value + */ + private function restoreEnvironmentVariable(string $name, $value): void + { + if ($value === false) { + putenv($name); + } else { + putenv($name . '=' . $value); + } + } }