8000 bug #38221 [Cache] Allow cache tags to be objects implementing __toSt… · symfony/symfony@7917612 · GitHub
[go: up one dir, main page]

Skip to content

Commit 7917612

Browse files
committed
bug #38221 [Cache] Allow cache tags to be objects implementing __toString() (lstrojny)
This PR was squashed before being merged into the 4.4 branch. Discussion ---------- [Cache] Allow cache tags to be objects implementing __toString() | Q | A | ------------- | --- | Branch? | 4.4 | Bug fix? | maybe | New feature? | maybe | Deprecations? | no | Tickets | | License | MIT | Doc PR | `\Symfony\Contracts\Cache\CacheInterface::get(string $key, …)` implicitly converts objects with `__toString` while `CacheItem::tag()` will throw an exception. That’s a bit of a sharp edge. Commits ------- c2c03e0 [Cache] Allow cache tags to be objects implementing __toString()
2 parents 2f67bf3 + c2c03e0 commit 7917612

File tree

3 files changed

+37
-3
lines changed

3 files changed

+37
-3
lines changed

src/Symfony/Component/Cache/CacheItem.php

-2Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,9 +119,10 @@ public function tag($tags): ItemInterface
119119
$tags = [$tags];
120120
}
121121
foreach ($tags as $tag) {
122-
if (!\is_string($tag)) {
123-
throw new InvalidArgumentException(sprintf('Cache tag must be string, "%s" given.', \is_object($tag) ? \get_class($tag) : \gettype($tag)));
122+
if (!\is_string($tag) && !(\is_object($tag) && method_exists($tag, '__toString'))) {
123+
throw new InvalidArgumentException(sprintf('Cache tag must be string or object that implements __toString(), "%s" given.', \is_object($tag) ? \get_class($tag) : \gettype($tag)));
124124
}
125+
$tag = (string) $tag;
125126
if (isset($this->newMetadata[self::METADATA_TAGS][$tag])) {
126127
continue;
127128
}

src/Symfony/Component/Cache/Tests/CacheItemTest.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use PHPUnit\Framework\TestCase;
1515
use Symfony\Component\Cache\CacheItem;
16+
use Symfony\Component\Cache\Tests\Fixtures\StringableTag;
1617

1718
class CacheItemTest extends TestCase
1819
{
@@ -61,9 +62,11 @@ public function testTag()
6162

6263
$this->assertSame($item, $item->tag('foo'));
6364
$this->assertSame($item, $item->tag(['bar', 'baz']));
65+
$this->assertSame($item, $item->tag(new StringableTag('qux')));
66+
$this->assertSame($item, $item->tag([new StringableTag('quux'), new StringableTag('quuux')]));
6467

6568
(\Closure::bind(function () use ($item) {
66-
$this->assertSame(['foo' => 'foo', 'bar' => 'bar', 'baz' => 'baz'], $item->newMetadata[CacheItem::METADATA_TAGS]);
69+
$this->assertSame(['foo' => 'foo', 'bar' => 'bar', 'baz' => 'baz', 'qux' => 'qux', 'quux' => 'quux', 'quuux' => 'quuux'], $item->newMetadata[CacheItem::METADATA_TAGS]);
6770
}, $this, CacheItem::class))();
6871
}
6972

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Cache\Tests\Fixtures;
13+
14+
class StringableTag
15+
{
16+
/**
17+
* @var string
18+
*/
19+
private $tag;
20+
21+
public function __construct(string $tag)
22+
{
23+
$this->tag = $tag;
24+
}
25+
26+
public function __toString(): string
27+
{
28+
return $this->tag;
29+
}
30+
}

0 commit comments

Comments
 (0)
0