-
Notifications
You must be signed in to change notification settings - Fork 95
Dynamically load XML container via PHP #163
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
Closed
+199
−44
Closed
Changes from all commits
Commits
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,55 @@ | ||
| <?php declare(strict_types = 1); | ||
|
|
||
| namespace PHPStan\Symfony; | ||
|
|
||
| use PHPStan\ShouldNotHappenException; | ||
| use SimpleXMLElement; | ||
|
|
||
| final class XmlContainerResolver | ||
| { | ||
|
|
||
| /** @var string|null */ | ||
| private $containerXmlPath; | ||
|
|
||
| /** @var SimpleXMLElement|null */ | ||
| private $container; | ||
|
|
||
| public function __construct(?string $containerXmlPath) | ||
| { | ||
| $this->containerXmlPath = $containerXmlPath; | ||
| } | ||
|
|
||
| public function getContainer(): ?SimpleXMLElement | ||
| { | ||
| if ($this->containerXmlPath === null) { | ||
| return null; | ||
| } | ||
|
|
||
| if ($this->container !== null) { | ||
| return $this->container; | ||
| } | ||
|
|
||
| if (pathinfo($this->containerXmlPath, PATHINFO_EXTENSION) === 'php') { | ||
| $fileContents = require $this->containerXmlPath; | ||
|
|
||
| if (!is_string($fileContents)) { | ||
| throw new ShouldNotHappenException(); | ||
| } | ||
| } else { | ||
| $fileContents = file_get_contents($this->containerXmlPath); | ||
| if ($fileContents === false) { | ||
| throw new XmlContainerNotExistsException(sprintf('Container %s does not exist', $this->containerXmlPath)); | ||
| } | ||
| } | ||
|
|
||
| $container = @simplexml_load_string($fileContents); | ||
| if ($container === false) { | ||
| throw new XmlContainerNotExistsException(sprintf('Container %s cannot be parsed', $this->containerXmlPath)); | ||
| } | ||
|
|
||
| $this->container = $container; | ||
|
|
||
| return $this->container; | ||
| } | ||
|
|
||
| } |
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
79 changes: 79 additions & 0 deletions
79
tests/Rules/Symfony/ContainerInterfacePrivateServicePhpLoaderRuleTest.php
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,79 @@ | ||
| <?php declare(strict_types = 1); | ||
|
|
||
| namespace PHPStan\Rules\Symfony; | ||
|
|
||
| use PHPStan\Rules\Rule; | ||
| use PHPStan\Symfony\XmlContainerResolver; | ||
| use PHPStan\Symfony\XmlServiceMapFactory; | ||
| use PHPStan\Testing\RuleTestCase; | ||
|
|
||
| /** | ||
| * @extends RuleTestCase<ContainerInterfacePrivateServiceRule> | ||
| */ | ||
| final class ContainerInterfacePrivateServicePhpLoaderRuleTest extends RuleTestCase | ||
| { | ||
|
|
||
| protected function getRule(): Rule | ||
| { | ||
| return new ContainerInterfacePrivateServiceRule((new XmlServiceMapFactory(new XmlContainerResolver(__DIR__ . '/container_loader.php')))->create()); | ||
| } | ||
|
|
||
| public function testGetPrivateService(): void | ||
| { | ||
| if (!class_exists('Symfony\Bundle\FrameworkBundle\Controller\Controller')) { | ||
| self::markTestSkipped(); | ||
| } | ||
| $this->analyse( | ||
| [ | ||
| __DIR__ . '/ExampleController.php', | ||
| ], | ||
| [ | ||
| [ | ||
| 'Service "private" is private.', | ||
| 12, | ||
| ], | ||
| ] | ||
| ); | ||
| } | ||
|
|
||
| public function testGetPrivateServiceInLegacyServiceSubscriber(): void | ||
| { | ||
| if (!interface_exists('Symfony\\Component\\DependencyInjection\\ServiceSubscriberInterface')) { | ||
| self::markTestSkipped('The test needs Symfony\Component\DependencyInjection\ServiceSubscriberInterface class.'); | ||
| } | ||
|
|
||
| if (!class_exists('Symfony\Bundle\FrameworkBundle\Controller\Controller')) { | ||
| self::markTestSkipped(); | ||
| } | ||
|
|
||
| $this->analyse( | ||
| [ | ||
| __DIR__ . '/ExampleLegacyServiceSubscriber.php', | ||
| __DIR__ . '/ExampleLegacyServiceSubscriberFromAbstractController.php', | ||
| __DIR__ . '/ExampleLegacyServiceSubscriberFromLegacyController.php', | ||
| ], | ||
| [] | ||
| ); | ||
| } | ||
|
|
||
| public function testGetPrivateServiceInServiceSubscriber(): void | ||
| { | ||
| if (!interface_exists('Symfony\Contracts\Service\ServiceSubscriberInterface')) { | ||
| self::markTestSkipped('The test needs Symfony\Contracts\Service\ServiceSubscriberInterface class.'); | ||
| } | ||
|
|
||
| if (!class_exists('Symfony\Bundle\FrameworkBundle\Controller\Controller')) { | ||
| self::markTestSkipped(); | ||
| } | ||
|
|
||
| $this->analyse( | ||
| [ | ||
| __DIR__ . '/ExampleServiceSubscriber.php', | ||
| __DIR__ . '/ExampleServiceSubscriberFromAbstractController.php', | ||
| __DIR__ . '/ExampleServiceSubscriberFromLegacyController.php', | ||
| ], | ||
| [] | ||
| ); | ||
| } | ||
|
|
||
| } |
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
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,3 @@ | ||
| <?php declare(strict_types = 1); | ||
|
|
||
| return file_get_contents(__DIR__ . '/container.xml'); |
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a bit too magic for my taste. It's called
container_xml_path. There should be a different config key for this scenario.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If both config keys are filled with a value, the extension should throw an exception so that the user knows they should provide either XML or PHP, but not both.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, not sure if that makes it more clear.
The
container_xml_pathis just points to a PHP file that returns the XML.How would you call it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe we should make it more general and instead have a file that returns the whole container (which we can turn into the XML ourselves for our own purposes).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And then it could be called
container_loaderThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Before doing it like this, I tried to create a new
ServiceContainerServiceMapFactoryandServiceContainerParameterMapFactorythat would actually load the real container via a separatecontainer_loaderfile.The problem is, the container is compiled, and does not contain the data we need.