-
Notifications
You must be signed in to change notification settings - Fork 94
Add result cache meta extension for DI container #421
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
Merged
ondrejmirtes
merged 7 commits into
phpstan:2.0.x
from
Wirone:codito/result-cache-meta-extension
Jan 21, 2025
+412
−37
Merged
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
d5c083f
Initial implementation of `SymfonyContainerResultCacheMetaExtension`
Wirone d19172c
Inject parameter/service maps directly instead of factories
Wirone c9f3147
Process parameters/services only if they were found in XML file
Wirone 43d493b
Add test for `SymfonyContainerResultCacheMetaExtension`
Wirone 858d747
Calculate hash properly also taking services' tags into consideration
Wirone 192e11e
Simplify tests by omitting I/O operations
Wirone ad83070
Use `var_export()` instead of `serialize()`
Wirone File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 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 | ||
{ | ||
$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', serialize(['parameters' => $parameters, 'services' => $services])); | ||
} | ||
|
||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
@@ -39,35 +41,38 @@ public function create(): ServiceMap | |
$services = []; | ||
/** @var Service[] $aliases */ | ||
$aliases = []; | ||
foreach ($xml->services->service as $def) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What are these changes for? Can you remove them? |
||
/** @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) { | ||
|
@@ -85,6 +90,8 @@ public function create(): ServiceMap | |
); | ||
} | ||
|
||
ksort($services); | ||
|
||
return new DefaultServiceMap($services); | ||
} | ||
|
||
|
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.