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"];
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
@@ -185,7 +185,7 @@ public function testLoadAnonymousServices()
185185
$loader = new XmlFileLoader($container, new FileLocator(self::$fixturesPath.'/xml'));
186186
$loader->load('services5.xml');
187187
$services = $container->getDefinitions();
188-
$this->assertCount(6, $services, '->load() attributes unique ids to anonymous services');
188+
$this->assertCount(7, $services, '->load() attributes unique ids to anonymous services');
189189

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

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

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

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

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

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

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

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

0 commit comments

Comments
 (0)