From 2ec78d9090926c0fc29ff2b694a102d81c16cbf4 Mon Sep 17 00:00:00 2001 From: Bradley Zeggelaar Date: Tue, 17 Sep 2024 22:52:21 +0200 Subject: [PATCH] [DependencyInjection] Fix `XmlFileLoader` not respecting when env for services --- .../Loader/XmlFileLoader.php | 8 +++---- .../Tests/Fixtures/RemoteCaller.php | 16 +++++++++++++ .../Tests/Fixtures/RemoteCallerHttp.php | 16 +++++++++++++ .../Tests/Fixtures/RemoteCallerSocket.php | 16 +++++++++++++ .../Tests/Fixtures/xml/when-env-services.xml | 23 +++++++++++++++++++ .../Tests/Loader/XmlFileLoaderTest.php | 18 +++++++++++++++ 6 files changed, 93 insertions(+), 4 deletions(-) create mode 100644 src/Symfony/Component/DependencyInjection/Tests/Fixtures/RemoteCaller.php create mode 100644 src/Symfony/Component/DependencyInjection/Tests/Fixtures/RemoteCallerHttp.php create mode 100644 src/Symfony/Component/DependencyInjection/Tests/Fixtures/RemoteCallerSocket.php create mode 100644 src/Symfony/Component/DependencyInjection/Tests/Fixtures/xml/when-env-services.xml diff --git a/src/Symfony/Component/DependencyInjection/Loader/XmlFileLoader.php b/src/Symfony/Component/DependencyInjection/Loader/XmlFileLoader.php index c8ecaec19affd..54103ef0ed212 100644 --- a/src/Symfony/Component/DependencyInjection/Loader/XmlFileLoader.php +++ b/src/Symfony/Component/DependencyInjection/Loader/XmlFileLoader.php @@ -123,7 +123,7 @@ private function parseImports(\DOMDocument $xml, string $file, ?\DOMNode $root = $xpath = new \DOMXPath($xml); $xpath->registerNamespace('container', self::NS); - if (false === $imports = $xpath->query('.//container:imports/container:import', $root)) { + if (false === $imports = $xpath->query('./container:imports/container:import', $root)) { return; } @@ -139,14 +139,14 @@ private function parseDefinitions(\DOMDocument $xml, string $file, Definition $d $xpath = new \DOMXPath($xml); $xpath->registerNamespace('container', self::NS); - if (false === $services = $xpath->query('.//container:services/container:service|.//container:services/container:prototype|.//container:services/container:stack', $root)) { + if (false === $services = $xpath->query('./container:services/container:service|./container:services/container:prototype|./container:services/container:stack', $root)) { return; } $this->setCurrentDir(\dirname($file)); $this->instanceof = []; $this->isLoadingInstanceof = true; - $instanceof = $xpath->query('.//container:services/container:instanceof', $root); + $instanceof = $xpath->query('./container:services/container:instanceof', $root); foreach ($instanceof as $service) { $this->setDefinition((string) $service->getAttribute('id'), $this->parseDefinition($service, $file, new Definition())); } @@ -197,7 +197,7 @@ private function getServiceDefaults(\DOMDocument $xml, string $file, ?\DOMNode $ $xpath = new \DOMXPath($xml); $xpath->registerNamespace('container', self::NS); - if (null === $defaultsNode = $xpath->query('.//container:services/container:defaults', $root)->item(0)) { + if (null === $defaultsNode = $xpath->query('./container:services/container:defaults', $root)->item(0)) { return new Definition(); } diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/RemoteCaller.php b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/RemoteCaller.php new file mode 100644 index 0000000000000..c5b8e86b2e0e9 --- /dev/null +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/RemoteCaller.php @@ -0,0 +1,16 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\DependencyInjection\Tests\Fixtures; + +interface RemoteCaller +{ +} diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/RemoteCallerHttp.php b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/RemoteCallerHttp.php new file mode 100644 index 0000000000000..4b3872a8edc75 --- /dev/null +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/RemoteCallerHttp.php @@ -0,0 +1,16 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\DependencyInjection\Tests\Fixtures; + +class RemoteCallerHttp implements RemoteCaller +{ +} diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/RemoteCallerSocket.php b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/RemoteCallerSocket.php new file mode 100644 index 0000000000000..9bef1a635d7e4 --- /dev/null +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/RemoteCallerSocket.php @@ -0,0 +1,16 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\DependencyInjection\Tests\Fixtures; + +class RemoteCallerSocket implements RemoteCaller +{ +} diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/xml/when-env-services.xml b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/xml/when-env-services.xml new file mode 100644 index 0000000000000..2a0885b64ff17 --- /dev/null +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/xml/when-env-services.xml @@ -0,0 +1,23 @@ + + + + + + + + + + + + + + + + + diff --git a/src/Symfony/Component/DependencyInjection/Tests/Loader/XmlFileLoaderTest.php b/src/Symfony/Component/DependencyInjection/Tests/Loader/XmlFileLoaderTest.php index 8b0f50e2904fb..cb919d8f8a35b 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Loader/XmlFileLoaderTest.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Loader/XmlFileLoaderTest.php @@ -44,6 +44,9 @@ use Symfony\Component\DependencyInjection\Tests\Fixtures\FooWithAbstractArgument; use Symfony\Component\DependencyInjection\Tests\Fixtures\NamedArgumentsDummy; use Symfony\Component\DependencyInjection\Tests\Fixtures\Prototype; +use Symfony\Component\DependencyInjection\Tests\Fixtures\RemoteCaller; +use Symfony\Component\DependencyInjection\Tests\Fixtures\RemoteCallerHttp; +use Symfony\Component\DependencyInjection\Tests\Fixtures\RemoteCallerSocket; use Symfony\Component\ExpressionLanguage\Expression; class XmlFileLoaderTest extends TestCase @@ -1167,4 +1170,19 @@ public function testWhenEnv() $this->assertSame(['foo' => 234, 'bar' => 345], $container->getParameterBag()->all()); } + + public function testLoadServicesWithEnvironment() + { + $container = new ContainerBuilder(); + + $loader = new XmlFileLoader($container, new FileLocator(self::$fixturesPath.'/xml'), 'prod'); + $loader->load('when-env-services.xml'); + + self::assertInstanceOf(RemoteCallerHttp::class, $container->get(RemoteCaller::class)); + + $loader = new XmlFileLoader($container, new FileLocator(self::$fixturesPath.'/xml'), 'dev'); + $loader->load('when-env-services.xml'); + + self::assertInstanceOf(RemoteCallerSocket::class, $container->get(RemoteCaller::class)); + } }