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"];

0 commit comments

Comments
 (0)