8000 Allow custom meta location in `ResourceCheckerConfigCache` · symfony/symfony@505b24a · GitHub
[go: up one dir, main page]

Skip to content

Commit 505b24a

Browse files
committed
Allow custom meta location in ResourceCheckerConfigCache
This makes it possible to put the meta file in a different location. One use case can be to write a file to `src` while keeping the meta file in `var`.
1 parent c9e7809 commit 505b24a

File tree

3 files changed

+28
-13
lines changed

3 files changed

+28
-13
lines changed

src/Symfony/Component/Config/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
7.1
5+
---
6+
7+
* Allow custom meta location in `ResourceCheckerConfigCache`
8+
49
7.0
510
---
611

src/Symfony/Component/Config/ResourceCheckerConfigCache.php

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ class ResourceCheckerConfigCache implements ConfigCacheInterface
2525
{
2626
private string $file;
2727

28+
private string $metaFile;
29+
2830
/**
2931
* @var iterable<mixed, ResourceCheckerInterface>
3032
*/
@@ -33,11 +35,13 @@ class ResourceCheckerConfigCache implements ConfigCacheInterface
3335
/**
3436
* @param string $file The absolute cache path
3537
* @param iterable<mixed, ResourceCheckerInterface> $resourceCheckers The ResourceCheckers to use for the freshness check
38+
* @param string|null $metaFile The absolute path to the meta file, defaults to $file.meta if null
3639
*/
37-
public function __construct(string $file, iterable $resourceCheckers = [])
40+
public function __construct(string $file, iterable $resourceCheckers = [], string $metaFile = null)
3841
{
3942
$this->file = $file;
4043
$this->resourceCheckers = $resourceCheckers;
44+
$this->metaFile = $metaFile ?? $file.'.meta';
4145
}
4246

4347
public function getPath(): string
@@ -68,7 +72,7 @@ public function isFresh(): bool
6872
return true; // shortcut - if we don't have any checkers we don't need to bother with the meta file at all
6973
}
7074

71-
$metadata = $this->getMetaFile();
75+
$metadata = $this->metaFile;
7276

7377
if (!is_file($metadata)) {
7478
return false;
@@ -120,9 +124,9 @@ public function write(string $content, array $metadata = null): void
120124
}
121125

122126
if (null !== $metadata) {
123-
$filesystem->dumpFile($this->getMetaFile(), serialize($metadata));
127+
$filesystem->dumpFile($this->metaFile, serialize($metadata));
124128
try {
125-
$filesystem->chmod($this->getMetaFile(), $mode, $umask);
129+
$filesystem->chmod($this->metaFile, $mode, $umask);
126130
} catch (IOException) {
127131
// discard chmod failure (some filesystem may not support it)
128132
}
@@ -133,14 +137,6 @@ public function write(string $content, array $metadata = null): void
133137
}
134138
}
135139

136-
/**
137-
* Gets the meta file path.
138-
*/
139-
private function getMetaFile(): string
140-
{
141-
return $this->file.'.meta';
142-
}
143-
144140
private function safelyUnserialize(string $file): mixed
145141
{
146142
$meta = false;

src/Symfony/Component/Config/Tests/ResourceCheckerConfigCacheTest.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,17 @@ class ResourceCheckerConfigCacheTest extends TestCase
2121
{
2222
private string $cacheFile;
2323

24+
private string $metaFile;
25+
2426
protected function setUp(): void
2527
{
2628
$this->cacheFile = tempnam(sys_get_temp_dir(), 'config_');
29+
$this->metaFile = tempnam(sys_get_temp_dir(), 'config_');
2730
}
2831

2932
protected function tearDown(): void
3033
{
31-
$files = [$this->cacheFile, "{$this->cacheFile}.meta"];
34+
$files = [$this->cacheFile, "{$this->cacheFile}.meta", $this->metaFile];
3235

3336
foreach ($files as $file) {
3437
if (file_exists($file)) {
@@ -148,4 +151,15 @@ public function testCacheIsNotFreshIfNotExistsMetaFile()
148151

149152
$this->assertFalse($cache->isFresh());
150153
}
154+
155+
public function testCacheWithCustomMetaFile()
156+
{
157+
$this->assertStringEqualsFile($this->metaFile, '');
158+
159+
$checker = $this->createMock(ResourceCheckerInterface::class);
160+
$cache = new ResourceCheckerConfigCache($this->cacheFile, [$checker], $this->metaFile);
161+
$cache->write('foo', [new FileResource(__FILE__)]);
162+
163+
$this->assertStringNotEqualsFile($this->metaFile, '');
164+
}
151165
}

0 commit comments

Comments
 (0)
0