Skip to content

Commit

Permalink
Add result cache meta extension for DI container
Browse files Browse the repository at this point in the history
  • Loading branch information
Wirone authored Jan 21, 2025
1 parent 7417f3a commit 65f02c7
Show file tree
Hide file tree
Showing 6 changed files with 412 additions and 37 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"require": {
"php": "^7.4 || ^8.0",
"ext-simplexml": "*",
"phpstan/phpstan": "^2.0"
"phpstan/phpstan": "^2.1.2"
},
"conflict": {
"symfony/framework-bundle": "<3.0"
Expand Down
5 changes: 5 additions & 0 deletions extension.neon
Original file line number Diff line number Diff line change
Expand Up @@ -363,3 +363,8 @@ services:
-
factory: PHPStan\Type\Symfony\ExtensionGetConfigurationReturnTypeExtension
tags: [phpstan.broker.dynamicMethodReturnTypeExtension]

-
class: PHPStan\Symfony\SymfonyContainerResultCacheMetaExtension
tags:
- phpstan.resultCacheMetaExtension
62 changes: 62 additions & 0 deletions src/Symfony/SymfonyContainerResultCacheMetaExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php declare(strict_types = 1);

namespace PHPStan\Symfony;

use PHPStan\Analyser\ResultCache\ResultCacheMetaExtension;
use function array_map;
use function hash;
use function ksort;
use function sort;
use function var_export;

final class SymfonyContainerResultCacheMetaExtension implements ResultCacheMetaExtension
{

private ParameterMap $parameterMap;

private ServiceMap $serviceMap;

public function __construct(ParameterMap $parameterMap, ServiceMap $serviceMap)
{
$this->parameterMap = $parameterMap;
$this->serviceMap = $serviceMap;
}

public function getKey(): string
{
return 'symfonyDiContainer';
}

public function getHash(): string
{
$services = $parameters = [];

foreach ($this->parameterMap->getParameters() as $parameter) {
$parameters[$parameter->getKey()] = $parameter->getValue();
}
ksort($parameters);

foreach ($this->serviceMap->getServices() as $service) {
$serviceTags = array_map(
static fn (ServiceTag $tag) => [
'name' => $tag->getName(),
'attributes' => $tag->getAttributes(),
],
$service->getTags(),
);
sort($serviceTags);

$services[$service->getId()] = [
'class' => $service->getClass(),
'public' => $service->isPublic() ? 'yes' : 'no',
'synthetic' => $service->isSynthetic() ? 'yes' : 'no',
'alias' => $service->getAlias(),
'tags' => $serviceTags,
];
}
ksort($services);

return hash('sha256', var_export(['parameters' => $parameters, 'services' => $services], true));
}

}
23 changes: 15 additions & 8 deletions src/Symfony/XmlParameterMapFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
use PHPStan\ShouldNotHappenException;
use SimpleXMLElement;
use function base64_decode;
use function count;
use function file_get_contents;
use function is_numeric;
use function ksort;
use function simplexml_load_string;
use function sprintf;
use function strpos;
Expand Down Expand Up @@ -40,18 +42,23 @@ public function create(): ParameterMap

/** @var Parameter[] $parameters */
$parameters = [];
foreach ($xml->parameters->parameter as $def) {
/** @var SimpleXMLElement $attrs */
$attrs = $def->attributes();

$parameter = new Parameter(
(string) $attrs->key,
$this->getNodeValue($def),
);
if (count($xml->parameters) > 0) {
foreach ($xml->parameters->parameter as $def) {
/** @var SimpleXMLElement $attrs */
$attrs = $def->attributes();

$parameters[$parameter->getKey()] = $parameter;
$parameter = new Parameter(
(string) $attrs->key,
$this->getNodeValue($def),
);

$parameters[$parameter->getKey()] = $parameter;
}
}

ksort($parameters);

return new DefaultParameterMap($parameters);
}

Expand Down
63 changes: 35 additions & 28 deletions src/Symfony/XmlServiceMapFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
namespace PHPStan\Symfony;

use SimpleXMLElement;
use function count;
use function file_get_contents;
use function ksort;
use function simplexml_load_string;
use function sprintf;
use function strpos;
Expand Down Expand Up @@ -39,35 +41,38 @@ public function create(): ServiceMap
$services = [];
/** @var Service[] $aliases */
$aliases = [];
foreach ($xml->services->service as $def) {
/** @var SimpleXMLElement $attrs */
$attrs = $def->attributes();
if (!isset($attrs->id)) {
continue;
}

$serviceTags = [];
foreach ($def->tag as $tag) {
$tagAttrs = ((array) $tag->attributes())['@attributes'] ?? [];
$tagName = $tagAttrs['name'];
unset($tagAttrs['name']);

$serviceTags[] = new ServiceTag($tagName, $tagAttrs);
}

$service = new Service(
$this->cleanServiceId((string) $attrs->id),
isset($attrs->class) ? (string) $attrs->class : null,
isset($attrs->public) && (string) $attrs->public === 'true',
isset($attrs->synthetic) && (string) $attrs->synthetic === 'true',
isset($attrs->alias) ? $this->cleanServiceId((string) $attrs->alias) : null,
$serviceTags,
);

if ($service->getAlias() !== null) {
$aliases[] = $service;
} else {
$services[$service->getId()] = $service;
if (count($xml->services) > 0) {
foreach ($xml->services->service as $def) {
/** @var SimpleXMLElement $attrs */
$attrs = $def->attributes();
if (!isset($attrs->id)) {
continue;
}

$serviceTags = [];
foreach ($def->tag as $tag) {
$tagAttrs = ((array) $tag->attributes())['@attributes'] ?? [];
$tagName = $tagAttrs['name'];
unset($tagAttrs['name']);

$serviceTags[] = new ServiceTag($tagName, $tagAttrs);
}

$service = new Service(
$this->cleanServiceId((string) $attrs->id),
isset($attrs->class) ? (string) $attrs->class : null,
isset($attrs->public) && (string) $attrs->public === 'true',
isset($attrs->synthetic) && (string) $attrs->synthetic === 'true',
isset($attrs->alias) ? $this->cleanServiceId((string) $attrs->alias) : null,
$serviceTags,
);

if ($service->getAlias() !== null) {
$aliases[] = $service;
} else {
$services[$service->getId()] = $service;
}
}
}
foreach ($aliases as $service) {
Expand All @@ -85,6 +90,8 @@ public function create(): ServiceMap
);
}

ksort($services);

return new DefaultServiceMap($services);
}

Expand Down
Loading

0 comments on commit 65f02c7

Please # to comment.