Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add result cache meta extension for DI container #421

Open
wants to merge 5 commits into
base: 2.0.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -360,3 +360,8 @@ services:
-
factory: PHPStan\Type\Symfony\ExtensionGetConfigurationReturnTypeExtension
tags: [phpstan.broker.dynamicMethodReturnTypeExtension]

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

namespace PHPStan\Symfony;

use PHPStan\Analyser\ResultCache\ResultCacheMetaExtension;
use function array_map;
use function hash;
use function serialize;
use function sort;

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
{
return hash('sha256', serialize([
Wirone marked this conversation as resolved.
Show resolved Hide resolved
'parameters' => array_map(
static fn (ParameterDefinition $parameter): array => [
'name' => $parameter->getKey(),
'value' => $parameter->getValue(),
],
$this->parameterMap->getParameters(),
),
'services' => array_map(
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(),
),
]));
}

}
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
Loading