8000 bug #58299 [DependencyInjection] Fix `XmlFileLoader` not respecting w… · devloop42/symfony@fe74ff3 · GitHub
[go: up one dir, main page]

Skip to content

Commit fe74ff3

Browse files
bug symfony#58299 [DependencyInjection] Fix XmlFileLoader not respecting when env for services (Bradley Zeggelaar)
This PR was merged into the 5.4 branch. Discussion ---------- [DependencyInjection] Fix `XmlFileLoader` not respecting when env for services | Q | A | ------------- | --- | Branch? | 5.4 <!-- see below --> | Bug fix? | yes | New feature? | no <!-- please update src/**/CHANGELOG.md files --> | Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files --> | Issues | Fix symfony#58293 <!-- prefix each issue number with "Fix #", no need to create an issue if none exists, explain below instead --> | License | MIT This PR resolves a bug inside the `XmlFileLoader` that causes the loading of all services inside a `<when env="..." />` elements even when not in the defined environment. My knowledge about xPath is very limited, but it seems like the parser interpets `.//` as `//` (anywhere) while most like `./` (relative) was meant, due to the passing of an `$root` element. Based on the added test and passing of existing test this change seems like sufficient to respect the `<when env="...." />` <!-- Replace this notice by a description of your feature/bugfix. This will help reviewers and should be a good start for the documentation. Additionally (see https://symfony.com/releases): - Always add tests and ensure they pass. - Bug fixes must be submitted against the lowest maintained branch where they apply (lowest branches are regularly merged to upper ones so they get the fixes too). - Features and deprecations must be submitted against the latest branch. - For new features, provide some code snippets to help understand usage. - Changelog entry should follow https://symfony.com/doc/current/contributing/code/conventions.html#writing-a-changelog-entry - Never break backward compatibility (see https://symfony.com/bc). --> Commits ------- 2ec78d9 [DependencyInjection] Fix `XmlFileLoader` not respecting when env for services
2 parents 2be812d + 2ec78d9 commit fe74ff3

File tree

6 files changed

+93
-4
lines changed

6 files changed

+93
-4
lines changed

src/Symfony/Component/DependencyInjection/Loader/XmlFileLoader.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ private function parseImports(\DOMDocument $xml, string $file, ?\DOMNode $root =
123123
$xpath = new \DOMXPath($xml);
124124
$xpath->registerNamespace('container', self::NS);
125125

126-
if (false === $imports = $xpath->query('.//container:imports/container:import', $root)) {
126+
if (false === $imports = $xpath->query('./container:imports/container:import', $root)) {
127127
return;
128128
}
129129

@@ -139,14 +139,14 @@ private function parseDefinitions(\DOMDocument $xml, string $file, Definition $d
139139
$xpath = new \DOMXPath($xml);
140140
$xpath->registerNamespace('container', self::NS);
141141

142-
if (false === $services = $xpath->query('.//container:services/container:service|.//container:services/container:prototype|.//container:services/container:stack', $root)) {
142+
if (false === $services = $xpath->query('./container:services/container:service|./container:services/container:prototype|./container:services/container:stack', $root)) {
143143
return;
144144
}
1 10000 45145
$this->setCurrentDir(\dirname($file));
146146

147147
$this->instanceof = [];
148148
$this->isLoadingInstanceof = true;
149-
$instanceof = $xpath->query('.//container:services/container:instanceof', $root);
149+
$instanceof = $xpath->query('./container:services/container:instanceof', $root);
150150
foreach ($instanceof as $service) {
151151
$this->setDefinition((string) $service->getAttribute('id'), $this->parseDefinition($service, $file, new Definition()));
152152
}
@@ -197,7 +197,7 @@ private function getServiceDefaults(\DOMDocument $xml, string $file, ?\DOMNode $
197197
$xpath = new \DOMXPath($xml);
198198
$xpath->registerNamespace('container', self::NS);
199199

200-
if (null === $defaultsNode = $xpath->query('.//container:services/container:defaults', $root)->item(0)) {
200+
if (null === $defaultsNode = $xpath->query('./container:services/container:defaults', $root)->item(0)) {
201201
return new Definition();
202202
}
203203

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\DependencyInjection\Tests\Fixtures;
13+
14+
interface RemoteCaller
15+
{
16+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\DependencyInjection\Tests\Fixtures;
13+
14+
class RemoteCallerHttp implements RemoteCaller
15+
{
16+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\DependencyInjection\Tests\Fixtures;
13+
14+
class RemoteCallerSocket implements RemoteCaller
15+
{
16+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?xml version="1.0" ?>
2+
3+
<container xmlns="http://symfony.com/schema/dic/services"
4+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5+
xsi:schemaLocation="http://symfony.com/schema/dic/services https://symfony.com/schema/dic/services/services-1.0.xsd">
6+
<services>
7+
<service id="Symfony\Component\DependencyInjection\Tests\Fixtures\RemoteCaller"
8+
alias="Symfony\Component\DependencyInjection\Tests\Fixtures\RemoteCallerHttp"/>
9+
10+
<service id="Symfony\Component\DependencyInjection\Tests\Fixtures\RemoteCallerHttp"
11+
class="Symfony\Component\DependencyInjection\Tests\Fixtures\RemoteCallerHttp"/>
12+
13+
<service id="Symfony\Component\DependencyInjection\Tests\Fixtures\RemoteCallerSocket"
14+
class="Symfony\Component\DependencyInjection\Tests\Fixtures\RemoteCallerSocket"/>
15+
</services>
16+
17+
<when env="dev">
18+
<services>
19+
<service id="Symfony\Component\DependencyInjection\Tests\Fixtures\RemoteCaller"
20+
alias="Symfony\Component\DependencyInjection\Tests\Fixtures\RemoteCallerSocket"/>
21+
</services>
22+
</when>
23+
</container>

src/Symfony/Component/DependencyInjection/Tests/Loader/XmlFileLoaderTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@
4444
use Symfony\Component\DependencyInjection\Tests\Fixtures\FooWithAbstractArgument;
4545
use Symfony\Component\DependencyInjection\Tests\Fixtures\NamedArgumentsDummy;
4646
use Symfony\Component\DependencyInjection\Tests\Fixtures\Prototype;
47+
use Symfony\Component\DependencyInjection\Tests\Fixtures\RemoteCaller;
48+
use Symfony\Component\DependencyInjection\Tests\Fixtures\RemoteCallerHttp;
49+
use Symfony\Component\DependencyInjection\Tests\Fixtures\RemoteCallerSocket;
4750
use Symfony\Component\ExpressionLanguage\Expression;
4851

4952
class XmlFileLoaderTest extends TestCase
@@ -1167,4 +1170,19 @@ public function testWhenEnv()
11671170

11681171
$this->assertSame(['foo' => 234, 'bar' => 345], $container->getParameterBag()->all());
11691172
}
1173+
1174+
public function testLoadServicesWithEnvironment()
1175+
{
1176+
$container = new ContainerBuilder();
1177+
1178+
$loader = new XmlFileLoader($container, new FileLocator(self::$fixturesPath.'/xml'), 'prod');
1179+
$loader->load('when-env-services.xml');
1180+
1181+
self::assertInstanceOf(RemoteCallerHttp::class, $container->get(RemoteCaller::class));
1182+
1183+
$loader = new XmlFileLoader($container, new FileLocator(self::$fixturesPath.'/xml'), 'dev');
1184+
$loader->load('when-env-services.xml');
1185+
1186+
self::assertInstanceOf(RemoteCallerSocket::class, $container->get(RemoteCaller::class));
1187+
}
11701188
}

0 commit comments

Comments
 (0)
0