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

Skip to content

Commit 9802a41

Browse files
[DependencyInjection] Resolve aliases before removing abstract services + add tests
1 parent 5fbfcb0 commit 9802a41

File tree

4 files changed

+44
-6
lines changed

4 files changed

+44
-6
lines changed

src/Symfony/Component/DependencyInjection/Compiler/PassConfig.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,8 @@ public function __construct()
5959

6060
$this->removingPasses = array(
6161
new RemovePrivateAliasesPass(),
62-
new RemoveAbstractDefinitionsPass(),
6362
new ReplaceAliasByActualDefinitionPass(),
63+
new RemoveAbstractDefinitionsPass(),
6464
new RepeatedPass(array(
6565
new AnalyzeServiceReferencesPass(),
6666
new InlineServiceDefinitionsPass(),
@@ -103,8 +103,7 @@ public function addPass(CompilerPassInterface $pass, $type = self::TYPE_BEFORE_O
103103
throw new InvalidArgumentException(sprintf('Invalid type "%s".', $type));
104104
}
105105

106-
$passes = &$this->$property;
107-
$passes[] = $pass;
106+
$this->{$property}[] = $pass;
108107
}
109108

110109
/**

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -707,6 +707,21 @@ public function testExtensionConfig()
707707
$this->assertEquals(array($second, $first), $configs);
708708
}
709709

710+
public function testAbstractAlias()
711+
{
712+
$container = new ContainerBuilder();
713+
714+
$abstract = new Definition('AbstractClass');
715+
$abstract->setAbstract(true);
716+
717+
$container->setDefinition('abstract_service', $abstract);
718+
$container->setAlias('abstract_alias', 'abstract_service');
719+
720+
$container->compile();
721+
722+
$this->assertSame('abstract_service', (string) $container->getAlias('abstract_alias'));
723+
}
724+
710725
public function testLazyLoadedService()
711726
{
712727
$loader = new ClosureLoader($container = new ContainerBuilder());

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,12 @@
1414
</service>
1515
</argument>
1616
<property name="p" type="service">
17-
<service class="BazClass" />
17+
<service class="BuzClass" />
1818
</property>
1919
</service>
20+
<service id="bar" parent="foo" />
21+
<service class="BizClass">
22+
<tag name="biz_tag" />
23+
</service>
2024
</services>
2125
</container>

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

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ public function testLoadAnonymousServices()
121121
$loader = new XmlFileLoader($container, new FileLocator(self::$fixturesPath.'/xml'));
122122
$loader->load('services5.xml');
123123
$services = $container->getDefinitions();
124-
$this->assertCount(4, $services, '->load() attributes unique ids to anonymous services');
124+
$this->assertCount(6, $services, '->load() attributes unique ids to anonymous services');
125125

126126
// anonymous service as an argument
127127
$args = $services['foo']->getArguments();
@@ -130,6 +130,7 @@ public function testLoadAnonymousServices()
130130
$this->assertTrue(isset($services[(string) $args[0]]), '->load() makes a reference to the created ones');
131131
$inner = $services[(string) $args[0]];
132132
$this->assertEquals('BarClass', $inner->getClass(), '->load() uses the same configuration as for the anonymous ones');
133+
$this->assertFalse($inner->isPublic());
133134

134135
// inner anonymous services
135136
$args = $inner->getArguments();
@@ -138,14 +139,33 @@ public function testLoadAnonymousServices()
138139
$this->assertTrue(isset($services[(string) $args[0]]), '->load() makes a reference to the created ones');
139140
$inner = $services[(string) $args[0]];
140141
$this->assertEquals('BazClass', $inner->getClass(), '->load() uses the same configuration as for the anonymous ones');
142+
$this->assertFalse($inner->isPublic());
141143

142144
// anonymous service as a property
143145
$properties = $services['foo']->getProperties();
144146
$property = $properties['p'];
145147
$this->assertInstanceOf('Symfony\\Component\\DependencyInjection\\Reference', $property, '->load() converts anonymous services to references to "normal" services');
146148
$this->assertTrue(isset($services[(string) $property]), '->load() makes a reference to the created ones');
147149
$inner = $services[(string) $property];
148-
$this->assertEquals('BazClass', $inner->getClass(), '->load() uses the same configuration as for the anonymous ones');
150+
$this->assertEquals('BuzClass', $inner->getClass(), '->load() uses the same configuration as for the anonymous ones');
151+
$this->assertFalse($inner->isPublic());
152+
153+
// "wild" service
154+
$service = $container->findTaggedServiceIds('biz_tag');
155+
$this->assertCount(1, $service);
156+
157+
foreach ($service as $id => $tag) {
158+
$service = $container->getDefinition($id);
159+
}
160+
$this->assertEquals('BizClass', $service->getClass(), '->load() uses the same configuration as for the anonymous ones');
161+
$this->assertFalse($service->isPublic());
162+
163+
// anonymous services are shared when using decoration definitions
164+
$container->compile();
165+
$services = $container->getDefinitions();
166+
$fooArgs = $services['foo']->getArguments();
167+
$barArgs = $services['bar']->getArguments();
168+
$this->assertSame($fooArgs[0], $barArgs[0]);
149169
}
150170

151171
public function testLoadServices()

0 commit comments

Comments
 (0)