diff --git a/src/Symfony/Component/Config/CHANGELOG.md b/src/Symfony/Component/Config/CHANGELOG.md index 51e2d1fee567b..79709aaa78f85 100644 --- a/src/Symfony/Component/Config/CHANGELOG.md +++ b/src/Symfony/Component/Config/CHANGELOG.md @@ -1,6 +1,11 @@ CHANGELOG ========= +7.1 +--- + + * Allow custom meta location in `ResourceCheckerConfigCache` + 7.0 --- diff --git a/src/Symfony/Component/Config/ResourceCheckerConfigCache.php b/src/Symfony/Component/Config/ResourceCheckerConfigCache.php index 2366392871a91..593cc3ba4d674 100644 --- a/src/Symfony/Component/Config/ResourceCheckerConfigCache.php +++ b/src/Symfony/Component/Config/ResourceCheckerConfigCache.php @@ -26,11 +26,14 @@ class ResourceCheckerConfigCache implements ConfigCacheInterface /** * @param string $file The absolute cache path * @param iterable $resourceCheckers The ResourceCheckers to use for the freshness check + * @param string|null $metaFile The absolute path to the meta file, defaults to $file.meta if null */ public function __construct( private string $file, private iterable $resourceCheckers = [], + private ?string $metaFile = null, ) { + $this->metaFile = $metaFile ?? $file.'.meta'; } public function getPath(): string @@ -61,7 +64,7 @@ public function isFresh(): bool return true; // shortcut - if we don't have any checkers we don't need to bother with the meta file at all } - $metadata = $this->getMetaFile(); + $metadata = $this->metaFile; if (!is_file($metadata)) { return false; @@ -113,9 +116,9 @@ public function write(string $content, ?array $metadata = null): void } if (null !== $metadata) { - $filesystem->dumpFile($this->getMetaFile(), serialize($metadata)); + $filesystem->dumpFile($this->metaFile, serialize($metadata)); try { - $filesystem->chmod($this->getMetaFile(), $mode, $umask); + $filesystem->chmod($this->metaFile, $mode, $umask); } catch (IOException) { // discard chmod failure (some filesystem may not support it) } @@ -126,14 +129,6 @@ public function write(string $content, ?array $metadata = null): void } } - /** - * Gets the meta file path. - */ - private function getMetaFile(): string - { - return $this->file.'.meta'; - } - private function safelyUnserialize(string $file): mixed { $meta = false; diff --git a/src/Symfony/Component/Config/Tests/ResourceCheckerConfigCacheTest.php b/src/Symfony/Component/Config/Tests/ResourceCheckerConfigCacheTest.php index 37f30a49d4ec0..53b66b88e447f 100644 --- a/src/Symfony/Component/Config/Tests/ResourceCheckerConfigCacheTest.php +++ b/src/Symfony/Component/Config/Tests/ResourceCheckerConfigCacheTest.php @@ -21,14 +21,17 @@ class ResourceCheckerConfigCacheTest extends TestCase { private string $cacheFile; + private string $metaFile; + protected function setUp(): void { $this->cacheFile = tempnam(sys_get_temp_dir(), 'config_'); + $this->metaFile = tempnam(sys_get_temp_dir(), 'config_'); } protected function tearDown(): void { - $files = [$this->cacheFile, "{$this->cacheFile}.meta"]; + $files = [$this->cacheFile, "{$this->cacheFile}.meta", $this->metaFile]; foreach ($files as $file) { if (file_exists($file)) { @@ -148,4 +151,15 @@ public function testCacheIsNotFreshIfNotExistsMetaFile() $this->assertFalse($cache->isFresh()); } + + public function testCacheWithCustomMetaFile() + { + $this->assertStringEqualsFile($this->metaFile, ''); + + $checker = $this->createMock(ResourceCheckerInterface::class); + $cache = new ResourceCheckerConfigCache($this->cacheFile, [$checker], $this->metaFile); + $cache->write('foo', [new FileResource(__FILE__)]); + + $this->assertStringNotEqualsFile($this->metaFile, ''); + } }