Skip to content

Commit

Permalink
Expose meta on ResourceCheckerConfigCache / ConfigCacheInterface
Browse files Browse the repository at this point in the history
When a developer adds meta to a config cache, it should also be possible to get back the meta.

I use these config caches for my own framework and want to retrieve a specific meta associated to
the cache.
  • Loading branch information
ruudk committed Nov 16, 2023
1 parent c9e7809 commit 3c02748
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 7 deletions.
5 changes: 5 additions & 0 deletions src/Symfony/Component/Config/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
=========

7.1
---

* Added method `getMeta()` to ResourceCheckerConfigCache and ConfigCacheInterface

7.0
---

Expand Down
7 changes: 7 additions & 0 deletions src/Symfony/Component/Config/ConfigCacheInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,11 @@ public function isFresh(): bool;
* @throws \RuntimeException When the cache file cannot be written
*/
public function write(string $content, array $metadata = null): void;

/*
* Returns the metadata stored for this cache.
*
* @return false|ResourceInterface[]
*/
// public function getMeta() : false | array;
}
24 changes: 17 additions & 7 deletions src/Symfony/Component/Config/ResourceCheckerConfigCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,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();

if (!is_file($metadata)) {
return false;
}

$meta = $this->safelyUnserialize($metadata);
$meta = $this->getMeta();

if (false === $meta) {
return false;
Expand Down Expand Up @@ -133,6 +127,22 @@ public function write(string $content, array $metadata = null): void
}
}

/**
* Returns the metadata stored for this cache.
*
* @return false|ResourceInterface[]
*/
public function getMeta(): false|array
{
$metadata = $this->getMetaFile();

if (!is_file($metadata)) {
return false;
}

return $this->safelyUnserialize($metadata);
}

/**
* Gets the meta file path.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,4 +148,14 @@ public function testCacheIsNotFreshIfNotExistsMetaFile()

$this->assertFalse($cache->isFresh());
}

public function testGetMeta()
{
$checker = $this->createMock(ResourceCheckerInterface::class);
$cache = new ResourceCheckerConfigCache($this->cacheFile, [$checker]);
$cache->write('foo', [new FileResource(__FILE__)]);

$this->assertCount(1, $cache->getMeta());
$this->assertEquals(new FileResource(__FILE__), $cache->getMeta()[0]);
}
}

0 comments on commit 3c02748

Please sign in to comment.