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

Skip to content

Commit f59ab88

Browse files
author
Hugo Hamon
committed
[DependencyInjection] make the service container builder register the definition of its related service container service in order to make compiler passes be able to reference the special service_container service.
1 parent 64ec5c5 commit f59ab88

21 files changed

+73
-16
lines changed

src/Symfony/Component/DependencyInjection/ContainerBuilder.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Component\DependencyInjection;
1313

14+
use Psr\Container\ContainerInterface as PsrContainerInterface;
1415
use Symfony\Component\DependencyInjection\Argument\ClosureProxyArgument;
1516
use Symfony\Component\DependencyInjection\Argument\IteratorArgument;
1617
use Symfony\Component\DependencyInjection\Argument\RewindableGenerator;
@@ -38,6 +39,7 @@
3839
use Symfony\Component\DependencyInjection\LazyProxy\Instantiator\RealServiceInstantiator;
3940
use Symfony\Component\DependencyInjection\LazyProxy\InheritanceProxyHelper;
4041
use Symfony\Component\DependencyInjection\LazyProxy\InheritanceProxyInterface;
42+
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
4143
use Symfony\Component\ExpressionLanguage\Expression;
4244
use Symfony\Component\ExpressionLanguage\ExpressionFunctionProviderInterface;
4345

@@ -117,6 +119,16 @@ class ContainerBuilder extends Container implements TaggedContainerInterface
117119
*/
118120
private $vendors;
119121

122+
public function __construct(ParameterBagInterface $parameterBag = null)
123+
{
124+
parent::__construct($parameterBag);
125+
126+
$this->setDefinition('service_container', (new Definition(ContainerInterface::class))->setSynthetic(true));
127+
$this->setAlias(PsrContainerInterface::class, new Alias('service_container', false));
128+
$this->setAlias(ContainerInterface::class, new Alias('service_container', false));
129+
$this->setAlias(Container::class, new Alias('service_container', false));
130+
}
131+
120132
/**
121133
* Sets the track resources flag.
122134
*

src/Symfony/Component/DependencyInjection/Tests/ContainerBuilderTest.php

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
require_once __DIR__.'/Fixtures/includes/ProjectExtension.php';
1616

1717
use PHPUnit\Framework\TestCase;
18+
use Psr\Container\ContainerInterface as PsrContainerInterface;
1819
use Symfony\Component\Config\Resource\ComposerResource;
1920
use Symfony\Component\Config\Resource\ResourceInterface;
2021
use Symfony\Component\Config\Resource\DirectoryResource;
@@ -25,6 +26,7 @@
2526
use Symfony\Component\DependencyInjection\Argument\ServiceLocatorArgument;
2627
use Symfony\Component\DependencyInjection\ChildDefinition;
2728
use Symfony\Component\DependencyInjection\Compiler\PassConfig;
29+
use Symfony\Component\DependencyInjection\Container;
2830
use Symfony\Component\DependencyInjection\ContainerBuilder;
2931
use Symfony\Component\DependencyInjection\ContainerInterface;
3032
use Symfony\Component\DependencyInjection\Definition;
@@ -42,6 +44,22 @@
4244

4345
class ContainerBuilderTest extends TestCase
4446
{
47+
public function testDefaultRegisteredDefinitions()
48+
{
49+
$builder = new ContainerBuilder();
50+
51+
$this->assertCount(1, $builder->getDefinitions());
52+
$this->assertTrue($builder->hasDefinition('service_container'));
53+
54+
$definition = $builder->getDefinition('service_container');
55+
$this->assertInstanceOf(Definition::class, $definition);
56+
$this->assertTrue($definition->isSynthetic());
57+
$this->assertSame(ContainerInterface::class, $definition->getClass());
58+
$this->assertTrue($builder->hasDefinition(PsrContainerInterface::class));
59+
$this->assertTrue($builder->hasDefinition(ContainerInterface::class));
60+
$this->assertTrue($builder->hasDefinition(Container::class));
61+
}
62+
4563
public function testDefinitions()
4664
{
4765
$builder = new ContainerBuilder();
@@ -203,7 +221,7 @@ public function testGetServiceIds()
203221
$builder->register('foo', 'stdClass');
204222
$builder->bar = $bar = new \stdClass();
205223
$builder->register('bar', 'stdClass');
206-
$this->assertEquals(array('foo', 'bar', 'service_container'), $builder->getServiceIds(), '->getServiceIds() returns all defined service ids');
224+
$this->assertEquals(array('service_container', 'foo', 'bar'), $builder->getServiceIds(), '->getServiceIds() returns all defined service ids');
207225
}
208226

209227
public function testAliases()
@@ -538,7 +556,7 @@ public function testMerge()
538556
$config->setDefinition('baz', new Definition('BazClass'));
539557
$config->setAlias('alias_for_foo', 'foo');
540558
$container->merge($config);
541-
$this->assertEquals(array('foo', 'bar', 'baz'), array_keys($container->getDefinitions()), '->merge() merges definitions already defined ones');
559+
$this->assertEquals(array('service_container', 'foo', 'bar', 'baz'), array_keys($container->getDefinitions()), '->merge() merges definitions already defined ones');
542560

543561
$aliases = $container->getAliases();
544562
$this->assertTrue(isset($aliases['alias_for_foo']));

src/Symfony/Component/DependencyInjection/Tests/Dumper/XmlDumperTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ public function testDumpAnonymousServices()
7070
$this->assertEquals('<?xml version="1.0" encoding="utf-8"?>
7171
<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 http://symfony.com/schema/dic/services/services-1.0.xsd">
7272
<services>
73+
<service id="service_container" class="Symfony\Component\DependencyInjection\ContainerInterface" synthetic="true"/>
7374
<service id="foo" class="FooClass">
7475
<argument type="service">
7576
<service class="BarClass">
@@ -91,6 +92,7 @@ public function testDumpEntities()
9192
$this->assertEquals("<?xml version=\"1.0\" encoding=\"utf-8\"?>
9293
<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 http://symfony.com/schema/dic/services/services-1.0.xsd\">
9394
<services>
95+
<service id=\"service_container\" class=\"Symfony\Component\DependencyInjection\ContainerInterface\" synthetic=\"true\"/>
9496
<service id=\"foo\" class=\"FooClass\Foo\">
9597
<tag name=\"foo&quot;bar\bar\" foo=\"foo&quot;barřž€\"/>
9698
<argument>foo&lt;&gt;&amp;bar</argument>
@@ -117,13 +119,15 @@ public function provideDecoratedServicesData()
117119
array("<?xml version=\"1.0\" encoding=\"utf-8\"?>
118120
<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 http://symfony.com/schema/dic/services/services-1.0.xsd\">
119121
<services>
122+
<service id=\"service_container\" class=\"Symfony\Component\DependencyInjection\ContainerInterface\" synthetic=\"true\"/>
120123
<service id=\"foo\" class=\"FooClass\Foo\" decorates=\"bar\" decoration-inner-name=\"bar.woozy\"/>
121124
</services>
122125
</container>
123126
", include $fixturesPath.'/containers/container15.php'),
124127
array("<?xml version=\"1.0\" encoding=\"utf-8\"?>
125128
<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 http://symfony.com/schema/dic/services/services-1.0.xsd\">
126129
<services>
130+
<service id=\"service_container\" class=\"Symfony\Component\DependencyInjection\ContainerInterface\" synthetic=\"true\"/>
127131
<service id=\"foo\" class=\"FooClass\Foo\" decorates=\"bar\"/>
128132
</services>
129133
</container>

src/Symfony/Component/DependencyInjection/Tests/Fixtures/graphviz/services1.dot

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@ digraph sc {
33
node [fontsize="11" fontname="Arial" shape="record"];
44
edge [fontsize="9" fontname="Arial" color="grey" arrowhead="open" arrowsize="0.5"];
55

6-
node_service_container [label="service_container\nSymfony\\Component\\DependencyInjection\\ContainerBuilder\n", shape=record, fillcolor="#9999ff", style="filled"];
6+
node_service_container [label="service_container\nSymfony\\Component\\DependencyInjection\\ContainerInterface\n", shape=record, fillcolor="#eeeeee", style="filled"];
77
}

src/Symfony/Component/DependencyInjection/Tests/Fixtures/graphviz/services10-1.dot

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ digraph sc {
33
node [fontsize="13" fontname="Verdana" shape="square"];
44
edge [fontsize="12" fontname="Verdana" color="white" arrowhead="closed" arrowsize="1"];
55

6+
node_service_container [label="service_container\nSymfony\\Component\\DependencyInjection\\ContainerInterface\n", shape=square, fillcolor="grey", style="filled"];
67
node_foo [label="foo\nFooClass\n", shape=square, fillcolor="grey", style="filled"];
7-
node_service_container [label="service_container\nSymfony\\Component\\DependencyInjection\\ContainerBuilder\n", shape=square, fillcolor="green", style="empty"];
88
node_bar [label="bar\n\n", shape=square, fillcolor="red", style="empty"];
99
node_foo -> node_bar [label="" style="filled"];
1010
}

src/Symfony/Component/DependencyInjection/Tests/Fixtures/graphviz/services10.dot

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ digraph sc {
33
node [fontsize="11" fontname="Arial" shape="record"];
44
edge [fontsize="9" fontname="Arial" color="grey" arrowhead="open" arrowsize="0.5"];
55

6+
node_service_container [label="service_container\nSymfony\\Component\\DependencyInjection\\ContainerInterface\n", shape=record, fillcolor="#eeeeee", style="filled"];
67
node_foo [label="foo\nFooClass\n", shape=record, fillcolor="#eeeeee", style="filled"];
7-
node_service_container [label="service_container\nSymfony\\Component\\DependencyInjection\\ContainerBuilder\n", shape=record, fillcolor="#9999ff", style="filled"];
88
node_bar [label="bar\n\n", shape=record, fillcolor="#ff9999", style="filled"];
99
node_foo -> node_bar [label="" style="filled"];
1010
}

src/Symfony/Component/DependencyInjection/Tests/Fixtures/graphviz/services13.dot

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ digraph sc {
33
node [fontsize="11" fontname="Arial" shape="record"];
44
edge [fontsize="9" fontname="Arial" color="grey" arrowhead="open" arrowsize="0.5"];
55

6+
node_service_container [label="service_container\nSymfony\\Component\\DependencyInjection\\ContainerInterface\n", shape=record, fillcolor="#eeeeee", style="filled"];
67
node_foo [label="foo\nFooClass\n", shape=record, fillcolor="#eeeeee", style="filled"];
78
node_bar [label="bar\nBarClass\n", shape=record, fillcolor="#eeeeee", style="filled"];
8-
node_service_container [label="service_container\nSymfony\\Component\\DependencyInjection\\ContainerBuilder\n", shape=record, fillcolor="#9999ff", style="filled"];
99
node_foo -> node_bar [label="" style="filled"];
1010
}

src/Symfony/Component/DependencyInjection/Tests/Fixtures/graphviz/services14.dot

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@ digraph sc {
33
node [fontsize="11" fontname="Arial" shape="record"];
44
edge [fontsize="9" fontname="Arial" color="grey" arrowhead="open" arrowsize="0.5"];
55

6-
node_service_container [label="service_container\nContainer14\\ProjectServiceContainer\n", shape=record, fillcolor="#9999ff", style="filled"];
6+
node_service_container [label="service_container\nSymfony\\Component\\DependencyInjection\\ContainerInterface\n", shape=record, fillcolor="#eeeeee", style="filled"];
77
}

src/Symfony/Component/DependencyInjection/Tests/Fixtures/graphviz/services17.dot

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@ digraph sc {
33
node [fontsize="11" fontname="Arial" shape="record"];
44
edge [fontsize="9" fontname="Arial" color="grey" arrowhead="open" arrowsize="0.5"];
55

6+
node_service_container [label="service_container\nSymfony\\Component\\DependencyInjection\\ContainerInterface\n", shape=record, fillcolor="#eeeeee", style="filled"];
67
node_foo [label="foo\n%foo.class%\n", shape=record, fillcolor="#eeeeee", style="filled"];
7-
node_service_container [label="service_container\nSymfony\\Component\\DependencyInjection\\ContainerBuilder\n", shape=record, fillcolor="#9999ff", style="filled"];
88
}

src/Symfony/Component/DependencyInjection/Tests/Fixtures/graphviz/services9.dot

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ digraph sc {
33
node [fontsize="11" fontname="Arial" shape="record"];
44
edge [fontsize="9" fontname="Arial" color="grey" arrowhead="open" arrowsize="0.5"];
55

6+
node_service_container [label="service_container\nSymfony\\Component\\DependencyInjection\\ContainerInterface\n", shape=record, fillcolor="#eeeeee", style="filled"];
67
node_foo [label="foo (alias_for_foo)\nBar\\FooClass\n", shape=record, fillcolor="#eeeeee", style="filled"];
78
node_foo_baz [label="foo.baz\nBazClass\n", shape=record, fillcolor="#eeeeee", style="filled"];
89
node_bar [label="bar\nBar\\FooClass\n", shape=record, fillcolor="#eeeeee", style="filled"];
@@ -30,7 +31,6 @@ digraph sc {
3031
node_lazy_context_ignore_invalid_ref [label="lazy_context_ignore_invalid_ref\nLazyContext\n", shape=record, fillcolor="#eeeeee", style="filled"];
3132
node_closure_proxy [label="closure_proxy\nBarClass\n", shape=record, fillcolor="#eeeeee", style="filled"];
3233
node_service_locator [label="service_locator\nBar\n", shape=record, fillcolor="#eeeeee", style="filled"];
33-
node_service_container [label="service_container\nSymfony\\Component\\DependencyInjection\\ContainerBuilder\n", shape=record, fillcolor="#9999ff", style="filled"];
3434
node_foo2 [label="foo2\n\n", shape=record, fillcolor="#ff9999", style="filled"];
3535
node_foo3 [label="foo3\n\n", shape=record, fillcolor="#ff9999", style="filled"];
3636
node_foobaz [label="foobaz\n\n", shape=record, fillcolor="#ff9999", style="filled"];

0 commit comments

Comments
 (0)