From c2c03e050fad47a77afbe0437e2e76b58f581a19 Mon Sep 17 00:00:00 2001 From: Lars Strojny Date: Thu, 17 Sep 2020 11:19:24 +0200 Subject: [PATCH] [Cache] Allow cache tags to be objects implementing __toString() --- src/Symfony/Component/Cache/CacheItem.php | 5 ++-- .../Component/Cache/Tests/CacheItemTest.php | 5 +++- .../Cache/Tests/Fixtures/StringableTag.php | 30 +++++++++++++++++++ 3 files changed, 37 insertions(+), 3 deletions(-) create mode 100644 src/Symfony/Component/Cache/Tests/Fixtures/StringableTag.php diff --git a/src/Symfony/Component/Cache/CacheItem.php b/src/Symfony/Component/Cache/CacheItem.php index 7245fe8207..3de8ddc81c 100644 --- a/src/Symfony/Component/Cache/CacheItem.php +++ b/src/Symfony/Component/Cache/CacheItem.php @@ -119,9 +119,10 @@ final class CacheItem implements ItemInterface $tags = [$tags]; } foreach ($tags as $tag) { - if (!\is_string($tag)) { - throw new InvalidArgumentException(sprintf('Cache tag must be string, "%s" given.', \is_object($tag) ? \get_class($tag) : \gettype($tag))); + if (!\is_string($tag) && !(\is_object($tag) && method_exists($tag, '__toString'))) { + throw new InvalidArgumentException(sprintf('Cache tag must be string or object that implements __toString(), "%s" given.', \is_object($tag) ? \get_class($tag) : \gettype($tag))); } + $tag = (string) $tag; if (isset($this->newMetadata[self::METADATA_TAGS][$tag])) { continue; } diff --git a/src/Symfony/Component/Cache/Tests/CacheItemTest.php b/src/Symfony/Component/Cache/Tests/CacheItemTest.php index 3b756f571f..01914e4a36 100644 --- a/src/Symfony/Component/Cache/Tests/CacheItemTest.php +++ b/src/Symfony/Component/Cache/Tests/CacheItemTest.php @@ -13,6 +13,7 @@ namespace Symfony\Component\Cache\Tests; use PHPUnit\Framework\TestCase; use Symfony\Component\Cache\CacheItem; +use Symfony\Component\Cache\Tests\Fixtures\StringableTag; class CacheItemTest extends TestCase { @@ -61,9 +62,11 @@ class CacheItemTest extends TestCase $this->assertSame($item, $item->tag('foo')); $this->assertSame($item, $item->tag(['bar', 'baz'])); + $this->assertSame($item, $item->tag(new StringableTag('qux'))); + $this->assertSame($item, $item->tag([new StringableTag('quux'), new StringableTag('quuux')])); (\Closure::bind(function () use ($item) { - $this->assertSame(['foo' => 'foo', 'bar' => 'bar', 'baz' => 'baz'], $item->newMetadata[CacheItem::METADATA_TAGS]); + $this->assertSame(['foo' => 'foo', 'bar' => 'bar', 'baz' => 'baz', 'qux' => 'qux', 'quux' => 'quux', 'quuux' => 'quuux'], $item->newMetadata[CacheItem::METADATA_TAGS]); }, $this, CacheItem::class))(); } diff --git a/src/Symfony/Component/Cache/Tests/Fixtures/StringableTag.php b/src/Symfony/Component/Cache/Tests/Fixtures/StringableTag.php new file mode 100644 index 0000000000..caaf55c026 --- /dev/null +++ b/src/Symfony/Component/Cache/Tests/Fixtures/StringableTag.php @@ -0,0 +1,30 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Cache\Tests\Fixtures; + +class StringableTag +{ + /** + * @var string + */ + private $tag; + + public function __construct(string $tag) + { + $this->tag = $tag; + } + + public function __toString(): string + { + return $this->tag; + } +}