Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 4c4e3bf

Browse files
author
Bradley Zeggelaar
committed
[DependencyInjection] Fix XmlFileLoader not respecting when env for services
1 parent 60a39d6 commit 4c4e3bf

File tree

7 files changed

+94
-4
lines changed

7 files changed

+94
-4
lines changed

src/Symfony/Component/DependencyInjection/CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ CHANGELOG
33

44
5.4
55
---
6+
* Fix `XmlFileLoader` not respecting when env for services
67
* Add `$defaultIndexMethod` and `$defaultPriorityMethod` to `TaggedIterator` and `TaggedLocator` attributes
78
* Add `service_closure()` to the PHP-DSL
89
* Add support for autoconfigurable attributes on methods, properties and parameters

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

+4-4
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 ($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

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) Bradley Zeggelaar <[email protected]>
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+
}
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) Bradley Zeggelaar <[email protected]>
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+
}
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) Bradley Zeggelaar <[email protected]>
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+
}
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

+18
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)