From da1b60198e298f89e2976dbcad31bcdf0e81e7f6 Mon Sep 17 00:00:00 2001 From: Julien Deniau Date: Tue, 5 Mar 2019 09:57:12 +0100 Subject: [PATCH] Fix issue while loading a container When the URI of the container contains escaped characters like `%2F`, `simplexml_load_file` throw an error. --- src/Symfony/XmlServiceMapFactory.php | 9 +++++++-- tests/Symfony/ServiceMapTest.php | 8 ++++++++ tests/Symfony/containers/bugfix%2Fcontainer.xml | 13 +++++++++++++ 3 files changed, 28 insertions(+), 2 deletions(-) create mode 100644 tests/Symfony/containers/bugfix%2Fcontainer.xml diff --git a/src/Symfony/XmlServiceMapFactory.php b/src/Symfony/XmlServiceMapFactory.php index 858ea52a..de40d695 100644 --- a/src/Symfony/XmlServiceMapFactory.php +++ b/src/Symfony/XmlServiceMapFactory.php @@ -2,7 +2,7 @@ namespace PHPStan\Symfony; -use function simplexml_load_file; +use function simplexml_load_string; use function sprintf; use function strpos; use function substr; @@ -20,7 +20,12 @@ public function __construct(string $containerXml) public function create(): ServiceMap { - $xml = @simplexml_load_file($this->containerXml); + $fileContents = file_get_contents($this->containerXml); + if ($fileContents === false) { + throw new XmlContainerNotExistsException(sprintf('Container %s does not exist or cannot be parsed', $this->containerXml)); + } + + $xml = @simplexml_load_string($fileContents); if ($xml === false) { throw new XmlContainerNotExistsException(sprintf('Container %s does not exist or cannot be parsed', $this->containerXml)); } diff --git a/tests/Symfony/ServiceMapTest.php b/tests/Symfony/ServiceMapTest.php index 13ad2d9e..5bbbe412 100644 --- a/tests/Symfony/ServiceMapTest.php +++ b/tests/Symfony/ServiceMapTest.php @@ -17,6 +17,14 @@ public function testGetService(string $id, callable $validator): void $validator($factory->create()->getService($id)); } + public function testGetContainerEscapedPath(): void + { + $factory = new XmlServiceMapFactory(__DIR__ . '/containers/bugfix%2Fcontainer.xml'); + $serviceMap = $factory->create(); + + self::assertNotNull($serviceMap->getService('withClass')); + } + public function getServiceProvider(): Iterator { yield [ diff --git a/tests/Symfony/containers/bugfix%2Fcontainer.xml b/tests/Symfony/containers/bugfix%2Fcontainer.xml new file mode 100644 index 00000000..8c8fbd55 --- /dev/null +++ b/tests/Symfony/containers/bugfix%2Fcontainer.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + +