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

Skip to content

Commit fc66b28

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 0976c86 commit fc66b28

21 files changed

+64
-16
lines changed

src/Symfony/Component/DependencyInjection/ContainerBuilder.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
use Symfony\Component\DependencyInjection\LazyProxy\Instantiator\RealServiceInstantiator;
3939
use Symfony\Component\DependencyInjection\LazyProxy\InheritanceProxyHelper;
4040
use Symfony\Component\DependencyInjection\LazyProxy\InheritanceProxyInterface;
41+
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
4142
use Symfony\Component\ExpressionLanguage\Expression;
4243
use Symfony\Component\ExpressionLanguage\ExpressionFunctionProviderInterface;
4344

@@ -117,6 +118,13 @@ class ContainerBuilder extends Container implements TaggedContainerInterface
117118
*/
118119
private $vendors;
119120

121+
public function __construct(ParameterBagInterface $parameterBag = null)
122+
{
123+
parent::__construct($parameterBag);
124+
125+
$this->setDefinition('service_container', (new Definition(ContainerInterface::class))->setSynthetic(true));
126+
}
127+
120128
/**
121129
* Sets the track resources flag.
122130
*

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

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,19 @@
4141

4242
class ContainerBuilderTest extends \PHPUnit_Framework_TestCase
4343
{
44+
public function testDefaultRegisteredDefinitions()
45+
{
46+
$builder = new ContainerBuilder();
47+
48+
$this->assertCount(1, $builder->getDefinitions());
49+
$this->assertTrue($builder->hasDefinition('service_container'));
50+
51+
$definition = $builder->getDefinition('service_container');
52+
$this->assertInstanceOf(Definition::class, $definition);
53+
$this->assertTrue($definition->isSynthetic());
54+
$this->assertSame(ContainerInterface::class, $definition->getClass());
55+
}
56+
4457
public function testDefinitions()
4558
{
4659
$builder = new ContainerBuilder();
@@ -202,7 +215,7 @@ public function testGetServiceIds()
202215
$builder->register('foo', 'stdClass');
203216
$builder->bar = $bar = new \stdClass();
204217
$builder->register('bar', 'stdClass');
205-
$this->assertEquals(array('foo', 'bar', 'service_container'), $builder->getServiceIds(), '->getServiceIds() returns all defined service ids');
218+
$this->assertEquals(array('service_container', 'foo', 'bar'), $builder->getServiceIds(), '->getServiceIds() returns all defined service ids');
206219
}
207220

208221
public function testAliases()
@@ -537,7 +550,7 @@ public function testMerge()
537550
$config->setDefinition('baz', new Definition('BazClass'));
538551
$config->setAlias('alias_for_foo', 'foo');
539552
$container->merge($config);
540-
$this->assertEquals(array('foo', 'bar', 'baz'), array_keys($container->getDefinitions()), '->merge() merges definitions already defined ones');
553+
$this->assertEquals(array('service_container', 'foo', 'bar', 'baz'), array_keys($container->getDefinitions()), '->merge() merges definitions already defined ones');
541554

542555
$aliases = $container->getAliases();
543556
$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
@@ -69,6 +69,7 @@ public function testDumpAnonymousServices()
6969
$this->assertEquals('<?xml version="1.0" encoding="utf-8"?>
7070
<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">
7171
<services>
72+
<service id="service_container" class="Symfony\Component\DependencyInjection\ContainerInterface" synthetic="true"/>
7273
<service id="foo" class="FooClass">
7374
<argument type="service">
7475
<service class="BarClass">
@@ -90,6 +91,7 @@ public function testDumpEntities()
9091
$this->assertEquals("<?xml version=\"1.0\" encoding=\"utf-8\"?>
9192
<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\">
9293
<services>
94+
<service id=\"service_container\" class=\"Symfony\Component\DependencyInjection\ContainerInterface\" synthetic=\"true\"/>
9395
<service id=\"foo\" class=\"FooClass\Foo\">
9496
<tag name=\"foo&quot;bar\bar\" foo=\"foo&quot;barřž€\"/>
9597
<argument>foo&lt;&gt;&amp;bar</argument>
@@ -116,13 +118,15 @@ public function provideDecoratedServicesData()
116118
array("<?xml version=\"1.0\" encoding=\"utf-8\"?>
117119
<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\">
118120
<services>
121+
<service id=\"service_container\" class=\"Symfony\Component\DependencyInjection\ContainerInterface\" synthetic=\"true\"/>
119122
<service id=\"foo\" class=\"FooClass\Foo\" decorates=\"bar\" decoration-inner-name=\"bar.woozy\"/>
120123
</services>
121124
</container>
122125
", include $fixturesPath.'/containers/container15.php'),
123126
array("<?xml version=\"1.0\" encoding=\"utf-8\"?>
124127
<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\">
125128
<services>
129+
<service id=\"service_container\" class=\"Symfony\Component\DependencyInjection\ContainerInterface\" synthetic=\"true\"/>
126130
<service id=\"foo\" class=\"FooClass\Foo\" decorates=\"bar\"/>
127131
</services>
128132
</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"];
Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,6 @@
11
<?xml version="1.0" encoding="utf-8"?>
2-
<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"/>
2+
<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">
3+
<services>
4+
<service class="Symfony\Component\DependencyInjection\ContainerInterface" id="service_container" synthetic="true"/>
5+
</services>
6+
</container>

src/Symfony/Component/DependencyInjection/Tests/Fixtures/xml/services21.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<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">
33
<services>
4+
<service id="service_container" class="Symfony\Component\DependencyInjection\ContainerInterface" synthetic="true"/>
45
<service id="foo" class="Foo">
56
<factory method="createFoo">
67
<service class="FooFactory">
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<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">
33
<services>
4+
<service id="service_container" class="Symfony\Component\DependencyInjection\ContainerInterface" synthetic="true"/>
45
<service id="foo" class="Foo" autowire="true"/>
56
</services>
67
</container>

src/Symfony/Component/DependencyInjection/Tests/Fixtures/xml/services8.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,7 @@
1919
<parameter type="string">null</parameter>
2020
</parameter>
2121
</parameters>
22+
<services>
23+
<service id="service_container" class="Symfony\Component\DependencyInjection\ContainerInterface" synthetic="true"/>
24+
</services>
2225
</container>

src/Symfony/Component/DependencyInjection/Tests/Fixtures/xml/services9.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
<parameter key="foo">bar</parameter>
77
</parameters>
88
<services>
9+
<service id="service_container" class="Symfony\Component\DependencyInjection\ContainerInterface" synthetic="true"/>
910
<service id="foo" class="Bar\FooClass">
1011
<tag name="foo" foo="foo"/>
1112
<tag name="foo" bar="bar" baz="baz"/>
Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
1-
1+
services:
2+
service_container:
3+
class: Symfony\Component\DependencyInjection\ContainerInterface
4+
synthetic: true
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11

22
services:
3+
service_container:
4+
class: Symfony\Component\DependencyInjection\ContainerInterface
5+
synthetic: true
36
foo:
47
class: Foo
58
autowire: true

src/Symfony/Component/DependencyInjection/Tests/Fixtures/yaml/services8.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,7 @@ parameters:
55
escape: '@@escapeme'
66
values: [true, false, null, 0, 1000.3, 'true', 'false', 'null']
77

8+
services:
9+
service_container:
10+
class: Symfony\Component\DependencyInjection\ContainerInterface
11+
synthetic: true

src/Symfony/Component/DependencyInjection/Tests/Fixtures/yaml/services9.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ parameters:
44
foo: bar
55

66
services:
7+
service_container:
8+
class: Symfony\Component\DependencyInjection\ContainerInterface
9+
synthetic: true
710
foo:
811
class: Bar\FooClass
912
tags:

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ public function testLoadAnonymousServices()
184184
$loader = new XmlFileLoader($container, new FileLocator(self::$fixturesPath.'/xml'));
185185
$loader->load('services5.xml');
186186
$services = $container->getDefinitions();
187-
$this->assertCount(6, $services, '->load() attributes unique ids to anonymous services');
187+
$this->assertCount(7, $services, '->load() attributes unique ids to anonymous services');
188188

189189
// anonymous service as an argument
190190
$args = $services['foo']->getArguments();
@@ -484,11 +484,11 @@ public function testNoNamingConflictsForAnonymousServices()
484484
$loader1 = new XmlFileLoader($container, new FileLocator(self::$fixturesPath.'/xml/extension1'));
485485
$loader1->load('services.xml');
486486
$services = $container->getDefinitions();
487-
$this->assertCount(2, $services, '->load() attributes unique ids to anonymous services');
487+
$this->assertCount(3, $services, '->load() attributes unique ids to anonymous services');
488488
$loader2 = new XmlFileLoader($container, new FileLocator(self::$fixturesPath.'/xml/extension2'));
489489
$loader2->load('services.xml');
490490
$services = $container->getDefinitions();
491-
$this->assertCount(4, $services, '->load() attributes unique ids to anonymous services');
491+
$this->assertCount(5, $services, '->load() attributes unique ids to anonymous services');
492492

493493
$services = $container->getDefinitions();
494494
$args1 = $services['extension1.foo']->getArguments();
@@ -631,7 +631,7 @@ public function testPrototype()
631631

632632
$ids = array_keys($container->getDefinitions());
633633
sort($ids);
634-
$this->assertSame(array(Prototype\Foo::class, Prototype\Sub\Bar::class), $ids);
634+
$this->assertSame(array(Prototype\Foo::class, Prototype\Sub\Bar::class, 'service_container'), $ids);
635635

636636
$resources = $container->getResources();
637637

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,7 @@ public function testPrototype()
396396

397397
$ids = array_keys($container->getDefinitions());
398398
sort($ids);
399-
$this->assertSame(array(Prototype\Foo::class, Prototype\Sub\Bar::class), $ids);
399+
$this->assertSame(array(Prototype\Foo::class, Prototype\Sub\Bar::class, 'service_container'), $ids);
400400

401401
$resources = $container->getResources();
402402

0 commit comments

Comments
 (0)