Skip to content

[DependencyInjection] Fix XmlFileLoader not respecting when env for services #58299

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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()));
}
Expand Down Expand Up @@ -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();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* 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
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* 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
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* 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
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" ?>

<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services https://symfony.com/schema/dic/services/services-1.0.xsd">
<services>
<service id="Symfony\Component\DependencyInjection\Tests\Fixtures\RemoteCaller"
alias="Symfony\Component\DependencyInjection\Tests\Fixtures\RemoteCallerHttp"/>

<service id="Symfony\Component\DependencyInjection\Tests\Fixtures\RemoteCallerHttp"
class="Symfony\Component\DependencyInjection\Tests\Fixtures\RemoteCallerHttp"/>

<service id="Symfony\Component\DependencyInjection\Tests\Fixtures\RemoteCallerSocket"
class="Symfony\Component\DependencyInjection\Tests\Fixtures\RemoteCallerSocket"/>
</services>

<when env="dev">
<services>
<service id="Symfony\Component\DependencyInjection\Tests\Fixtures\RemoteCaller"
alias="Symfony\Component\DependencyInjection\Tests\Fixtures\RemoteCallerSocket"/>
</services>
</when>
</container>
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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));
}
}