Skip to content

Commit

Permalink
Calculate hash properly also taking services' tags into consideration
Browse files Browse the repository at this point in the history
  • Loading branch information
Wirone committed Jan 8, 2025
1 parent a561077 commit 5f8002d
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 8 deletions.
29 changes: 21 additions & 8 deletions src/Symfony/SymfonyContainerResultCacheMetaExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use function array_map;
use function hash;
use function serialize;
use function sort;

final class SymfonyContainerResultCacheMetaExtension implements ResultCacheMetaExtension
{
Expand All @@ -29,20 +30,32 @@ public function getHash(): string
{
return hash('sha256', serialize([
'parameters' => array_map(
static fn (ParameterDefinition $parameter) => [
static fn (ParameterDefinition $parameter): array => [
'name' => $parameter->getKey(),
'value' => $parameter->getValue(),
],
$this->parameterMap->getParameters(),
),
'services' => array_map(
static fn (ServiceDefinition $service) => [
'id' => $service->getId(),
'class' => $service->getClass(),
'public' => $service->isPublic() ? 'yes' : 'no',
'synthetic' => $service->isSynthetic() ? 'yes' : 'no',
'alias' => $service->getAlias(),
],
static function (ServiceDefinition $service): array {
$serviceTags = array_map(
static fn (ServiceTag $tag) => [
'name' => $tag->getName(),
'attributes' => $tag->getAttributes(),
],
$service->getTags(),
);
sort($serviceTags);

return [
'id' => $service->getId(),
'class' => $service->getClass(),
'public' => $service->isPublic() ? 'yes' : 'no',
'synthetic' => $service->isSynthetic() ? 'yes' : 'no',
'alias' => $service->getAlias(),
'tags' => $serviceTags,
];
},
$this->serviceMap->getServices(),
),
]));
Expand Down
83 changes: 83 additions & 0 deletions tests/Symfony/SymfonyContainerResultCacheMetaExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,89 @@ public static function provideContainerHashIsCalculatedCorrectlyCases(): iterabl
XML,
];

yield 'service tag attributes changes' => [
[
<<<'XML'
<container>
<services>
<service id="Foo" class="Foo">
<tag name="foo.bar" baz="bar"/>
<tag name="foo.baz" baz="baz"/>
</service>
</services>
</container>
XML,
<<<'XML'
<container>
<services>
<service id="Foo" class="Foo">
<tag name="foo.baz" baz="baz"/>
<tag name="foo.bar" baz="bar"/>
</service>
</services>
</container>
XML,
],
<<<'XML'
<container>
<services>
<service id="Foo" class="Foo">
<tag name="foo.bar" baz="bar"/>
<tag name="foo.baz" baz="buzz"/>
</service>
</services>
</container>
XML,
];

yield 'service tag added' => [
[
<<<'XML'
<container>
<services>
<service id="Foo" class="Foo">
<tag name="foo.bar" baz="bar"/>
</service>
</services>
</container>
XML,
],
<<<'XML'
<container>
<services>
<service id="Foo" class="Foo">
<tag name="foo.bar" baz="bar"/>
<tag name="foo.baz" baz="baz"/>
</service>
</services>
</container>
XML,
];

yield 'service tag removed' => [
[
<<<'XML'
<container>
<services>
<service id="Foo" class="Foo">
<tag name="foo.bar" baz="bar"/>
<tag name="foo.baz" baz="baz"/>
</service>
</services>
</container>
XML,
],
<<<'XML'
<container>
<services>
<service id="Foo" class="Foo">
<tag name="foo.bar" baz="bar"/>
</service>
</services>
</container>
XML,
];

yield 'new service added' => [
[
<<<'XML'
Expand Down

0 comments on commit 5f8002d

Please sign in to comment.