Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitfe74ff3

Browse files
bug#58299 [DependencyInjection] FixXmlFileLoader 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#58293 <!-- prefix each issue number with "Fix #", no need to create an issue if none exists, explain below instead -->| License | MITThis 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 (seehttps://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 followhttps://symfony.com/doc/current/contributing/code/conventions.html#writing-a-changelog-entry - Never break backward compatibility (seehttps://symfony.com/bc).-->Commits-------2ec78d9 [DependencyInjection] Fix `XmlFileLoader` not respecting when env for services
2 parents2be812d +2ec78d9 commitfe74ff3

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
}
145145
$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 ($instanceofas$service) {
151151
$this->setDefinition((string)$service->getAttribute('id'),$this->parseDefinition($service,$file,newDefinition()));
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
returnnewDefinition();
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+
namespaceSymfony\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+
namespaceSymfony\Component\DependencyInjection\Tests\Fixtures;
13+
14+
class RemoteCallerHttpimplements 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+
namespaceSymfony\Component\DependencyInjection\Tests\Fixtures;
13+
14+
class RemoteCallerSocketimplements 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+
<containerxmlns="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+
<serviceid="Symfony\Component\DependencyInjection\Tests\Fixtures\RemoteCaller"
8+
alias="Symfony\Component\DependencyInjection\Tests\Fixtures\RemoteCallerHttp"/>
9+
10+
<serviceid="Symfony\Component\DependencyInjection\Tests\Fixtures\RemoteCallerHttp"
11+
class="Symfony\Component\DependencyInjection\Tests\Fixtures\RemoteCallerHttp"/>
12+
13+
<serviceid="Symfony\Component\DependencyInjection\Tests\Fixtures\RemoteCallerSocket"
14+
class="Symfony\Component\DependencyInjection\Tests\Fixtures\RemoteCallerSocket"/>
15+
</services>
16+
17+
<whenenv="dev">
18+
<services>
19+
<serviceid="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
useSymfony\Component\DependencyInjection\Tests\Fixtures\FooWithAbstractArgument;
4545
useSymfony\Component\DependencyInjection\Tests\Fixtures\NamedArgumentsDummy;
4646
useSymfony\Component\DependencyInjection\Tests\Fixtures\Prototype;
47+
useSymfony\Component\DependencyInjection\Tests\Fixtures\RemoteCaller;
48+
useSymfony\Component\DependencyInjection\Tests\Fixtures\RemoteCallerHttp;
49+
useSymfony\Component\DependencyInjection\Tests\Fixtures\RemoteCallerSocket;
4750
useSymfony\Component\ExpressionLanguage\Expression;
4851

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

11681171
$this->assertSame(['foo' =>234,'bar' =>345],$container->getParameterBag()->all());
11691172
}
1173+
1174+
publicfunctiontestLoadServicesWithEnvironment()
1175+
{
1176+
$container =newContainerBuilder();
1177+
1178+
$loader =newXmlFileLoader($container,newFileLocator(self::$fixturesPath.'/xml'),'prod');
1179+
$loader->load('when-env-services.xml');
1180+
1181+
self::assertInstanceOf(RemoteCallerHttp::class,$container->get(RemoteCaller::class));
1182+
1183+
$loader =newXmlFileLoader($container,newFileLocator(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)

[8]ページ先頭

©2009-2025 Movatter.jp