8000 feature #14030 [DependencyInjection] make it possible to dump inlined… · symfony/symfony@4a38eb4 · GitHub
[go: up one dir, main page]

Skip to content

Commit 4a38eb4

Browse files
committed
feature #14030 [DependencyInjection] make it possible to dump inlined services to XML (xabbuh)
This PR was merged into the 2.7 branch. Discussion ---------- [DependencyInjection] make it possible to dump inlined services to XML | Q | A | ------------- | --- | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #13557 | License | MIT | Doc PR | Commits ------- 504e338 make it possible to dump inlined services to XML
2 parents 2f39901 + 504e338 commit 4a38eb4

File tree

10 files changed

+127
-92
lines changed

10 files changed

+127
-92
lines changed

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,12 @@ public function process(ContainerBuilder $container)
6262
$definition->setProperties(
6363
$this->inlineArguments($container, $definition->getProperties())
6464
);
65+
66+
$configurator = $this->inlineArguments($container, array($definition->getConfigurator()));
67+
$definition->setConfigurator($configurator[0]);
68+
69+
$factory = $this->inlineArguments($container, array($definition->getFactory()));
70+
$definition->setFactory($factory[0]);
6571
}
6672
}
6773

src/Symfony/Component/DependencyInjection/Dumper/XmlDumper.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,11 @@ private function addService($definition, $id, \DOMElement $parent)
178178

179179
if ($callable = $definition->getFactory()) {
180180
$factory = $this->document->createElement('factory');
181-
if (is_array($callable)) {
181+
182+
if (is_array($callable) && $callable[0] instanceof Definition) {
183+
$this->addService($callable[0], null, $factory);
184+
$factory->setAttribute('method', $callable[1]);
185+
} elseif (is_array($callable)) {
182186
$factory->setAttribute($callable[0] instanceof Reference ? 'service' : 'class', $callable[0]);
183187
$factory->setAttribute('method', $callable[1]);
184188
} else {
@@ -189,7 +193,11 @@ private function addService($definition, $id, \DOMElement $parent)
189193

190194
if ($callable = $definition->getConfigurator()) {
191195
$configurator = $this->document->createElement('configurator');
192-
if (is_array($callable)) {
196+
197+
if (is_array($callable) && $callable[0] instanceof Definition) {
198+
8000 $this->addService($callable[0], null, $configurator);
199+
$configurator->setAttribute('method', $callable[1]);
200+
} elseif (is_array($callable)) {
193201
$configurator->setAttribute($callable[0] instanceof Reference ? 'service' : 'class', $callable[0]);
194202
$configurator->setAttribute('method', $callable[1]);
195203
} else {

src/Symfony/Component/DependencyInjection/Loader/XmlFileLoader.php

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -116,25 +116,27 @@ private function parseDefinitions(\DOMDocument $xml, $file)
116116
}
117117

118118
foreach ($services as $service) {
119-
$this->parseDefinition((string) $service->getAttribute('id'), $service, $file);
119+
if (null !== $definition = $this->parseDefinition($service, $file)) {
120+
$this->container->setDefinition((string) $service->getAttribute('id'), $definition);
121+
}
120122
}
121123
}
122124

123125
/**
124126
* Parses an individual Definition.
125127
*
126-
* @param string $id
127128
* @param \DOMElement $service
128-
* @param string $file
129+
*
130+
* @return Definition|null
129131
*/
130-
private function parseDefinition($id, \DOMElement $service, $file)
132+
private function parseDefinition(\DOMElement $service)
131133
{
132134
if ($alias = $service->getAttribute('alias')) {
133135
$public = true;
134136
if ($publicAttr = $service->getAttribute('public')) {
135137
$public = XmlUtils::phpize($publicAttr);
136138
}
137-
$this->container->setAlias($id, new Alias($alias, $public));
139+
$this->container->setAlias((string) $service->getAttribute('id'), new Alias($alias, $public));
138140

139141
return;
140142
}
@@ -153,7 +155,7 @@ private function parseDefinition($id, \DOMElement $service, $file)
153155
}
154156

155157
if ($value = $service->getAttribute('synchronized')) {
156-
$definition->setSynchronized(XmlUtils::phpize($value), 'request' !== $id);
158+
$definition->setSynchronized(XmlUtils::phpize($value), 'request' !== (string) $service->getAttribute('id'));
157159
}
158160

159161
if ($files = $this->getChildren($service, 'file')) {
@@ -168,7 +170,11 @@ private function parseDefinition($id, \DOMElement $service, $file)
168170
if ($function = $factory->getAttribute('function')) {
169171
$definition->setFactory($function);
170172
} else {
171-
if ($childService = $factory->getAttribute('service')) {
173+
$factoryService = $this->getChildren($factory, 'service');
174+
175+
if (isset($factoryService[0])) {
176+
$class = $this->parseDefinition($factoryService[0]);
177+
} elseif ($childService = $factory->getAttribute('service')) {
172178
$class = new Reference($childService, ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE, false);
173179
} else {
174180
$class = $factory->getAttribute('class');
@@ -183,7 +189,11 @@ private function parseDefinition($id, \DOMElement $service, $file)
183189
if ($function = $configurator->getAttribute('function')) {
184190
$definition->setConfigurator($function);
185191
} else {
186-
if ($childService = $configurator->getAttribute('service')) {
192+
$configuratorService = $this->getChildren($configurator, 'service');
193+
194+
if (isset($configuratorService[0])) {
195+
$class = $this->parseDefinition($configuratorService[0]);
196+
} elseif ($childService = $configurator->getAttribute('service')) {
187197
$class = new Reference($childService, ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE, false);
188198
} else {
189199
$class = $configurator->getAttribute('class');
@@ -219,7 +229,7 @@ private function parseDefinition($id, \DOMElement $service, $file)
219229
$definition->setDecoratedService($value, $renameId);
220230
}
221231

222-
$this->container->setDefinition($id, $definition);
232+
return $definition;
223233
}
224234

225235
/**
@@ -295,7 +305,9 @@ private function processAnonymousServices(\DOMDocument $xml, $file)
295305
// we could not use the constant false here, because of XML parsing
296306
$domElement->setAttribute('public', 'false');
297307

298-
$this->parseDefinition($id, $domElement, $file);
308+
if (null !== $definition = $this->parseDefinition($domElement, $file)) {
309+
$this->container->setDefinition($id, $definition);
310+
}
299311

300312
if (true === $wild) {
301313
$tmpDomElement = new \DOMElement('_services', null, self::NS);

src/Symfony/Component/DependencyInjection/Loader/schema/dic/services/services-1.0.xsd

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,9 @@
6565
</xsd:complexType>
6666

6767
<xsd:complexType name="callable">
68+
<xsd:choice minOccurs="0" maxOccurs="1">
69+
<xsd:element name="service" type="service" minOccurs="0" maxOccurs="1" />
70+
</xsd:choice>
6871
<xsd:attribute name="id" type="xsd:string" />
6972
<xsd:attribute name="service" type="xsd:string" />
7073
<xsd:attribute name="class" type="xsd:string" />

src/Symfony/Component/DependencyInjection/Tests/Compiler/InlineServiceDefinitionsPassTest.php

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -237,40 +237,6 @@ public function testProcessDoesNotInlineWhenServiceReferencesItself()
237237
$this->assertSame($ref, $calls[0][1][0]);
238238
}
239239

240-
public function testProcessDoesNotInlineFactories()
241-
{
242-
$container = new ContainerBuilder();
243-
$container
244-
->register('foo.factory')
245-
->setPublic(false)
246-
;
247-
$container
248-
->register('foo')
249-
->setFactory(array(new Reference('foo.factory'), 'getFoo'))
250-
;
251-
$this->process($container);
252-
253-
$factory = $container->getDefinition('foo')->getFactory();
254-
$this->assertInstanceOf('Symfony\Component\DependencyInjection\Reference', $factory[0]);
255-
}
256-
257-
public function testProcessDoesNotInlineConfigurators()
258-
{
259-
$container = new ContainerBuilder();
260-
$container
261-
->register('foo.configurator')
262-
->setPublic(false)
263-
;
264-
$container
265-
->register('foo')
266-
->setConfigurator(array(new Reference('foo.configurator'), 'getFoo'))
267-
;
268-
$this->process($container);
269-
270-
$configurator = $container->getDefinition('foo')->getConfigurator();
271-
$this->assertInstanceOf('Symfony\Component\DependencyInjection\Reference', $configurator[0]);
272-
}
273-
274240
protected function process(ContainerBuilder $container)
275241
{
276242
$repeatedPass = new RepeatedPass(array(new AnalyzeServiceReferencesPass(), new InlineServiceDefinitionsPass()));

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,4 +175,12 @@ public function provideCompiledContainerData()
175175
array('container14'),
176176
);
177177
}
178+
179+
public function testDumpInlinedServices()
180+
{
181+
$container = include self::$fixturesPath.'/containers/container21.php';
182+
$dumper = new XmlDumper($container);
183+
184+
$this->assertEquals(file_get_contents(self::$fixturesPath.'/xml/services21.xml'), $dumper->dump());
185+
}
178186
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
use Symfony\Component\DependencyInjection\ContainerBuilder;
4+
use Symfony\Component\DependencyInjection\Definition;
5+
6+
$container = new ContainerBuilder();
7+
8+
$bar = new Definition('Bar');
9+
$bar->setConfigurator(array(new Definition('Baz'), 'configureBar'));
10+
11+
$fooFactory = new Definition('FooFactory');
12+
$fooFactory->setFactory(array(new Definition('Foobar'), 'createFooFactory'));
13+
14+
$container
15+
->register('foo', 'Foo')
16+
->setFactory(array($fooFactory, 'createFoo'))
17+
->setConfigurator(array($bar, 'configureFoo'))
18+
;
19+
20+
return $container;

src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services9_compiled.php

Lines changed: 8 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ public function __construct()
3737
$this->methodMap = array(
3838
'bar' => 'getBarService',
3939
'baz' => 'getBazService',
40-
'configurator_service' => 'getConfiguratorServiceService',
4140
'configured_service' => 'getConfiguredServiceService',
4241
'decorator_service' => 'getDecoratorServiceService',
4342
'decorator_service_with_name' => 'getDecoratorServiceWithNameService',
@@ -47,7 +46,6 @@ public function __construct()
4746
'foo_bar' => 'getFooBarService',
4847
'foo_with_inline' => 'getFooWithInlineService',
4948
'method_call1' => 'getMethodCall1Service',
50-
'new_factory' => 'getNewFactoryService',
5149
'new_factory_service' => 'getNewFactoryServiceService',
5250
'request' => 'getRequestService',
5351
'service_from_static_method' => 'getServiceFromStaticMethodService',
@@ -113,9 +111,12 @@ protected function getBazService()
113111
*/
114112
protected function getConfiguredServiceService()
115113
{
114+
$a = new \ConfClass();
115+
$a->setFoo($this->get('baz'));
116+
116117
$this->services['configured_service'] = $instance = new \stdClass();
117118

118-
$this->get('configurator_service')->configureStdClass($instance);
119+
$a->configureStdClass($instance);
119120

120121
return $instance;
121122
}
@@ -263,7 +264,10 @@ protected function getMethodCall1Service()
263264
*/
264265
protected function getNewFactoryServiceService()
265266
{
266-
$this->services['new_factory_service'] = $instance = $this->get('new_factory')->getInstance();
267+
$a = new \FactoryClass();
268+
$a->foo = 'bar';
269+
270+
$this->services['new_factory_service'] = $instance = $a->getInstance();
267271

268272
$instance->foo = 'bar';
269273

@@ -296,48 +300,6 @@ protected function getServiceFromStaticMethodService()
296300
return $this->services['service_from_static_method'] = \Bar\FooClass::getInstance();
297301
}
298302

299-
/**
300-
* Gets the 'configurator_service' service.
301-
*
302-
* This service is shared.
303-
* This method always returns the same instance of the service.
304-
*
305-
* This service is private.
306-
* If you want to be able to request this service from the container directly,
307-
* make it public, otherwise you might end up with broken code.
308-
*
309-
* @return \ConfClass A ConfClass instance.
310-
*/
311-
protected function getConfiguratorServiceService()
312-
{
313-
$this->services['configurator_service'] = $instance = new \ConfClass();
314-
315-
$instance->setFoo($this->get('baz'));
316-
317-
return $instance;
318-
}
319-
320-
/**
321-
* Gets the 'new_factory' service.
322-
*
323-
* This service is shared.
324-
* This method always returns the same instance of the service.
325-
*
326-
* This service is private.
327-
* If you want to be able to request this service from the container directly,
328-
* make it public, otherwise you might end up with broken code.
329-
*
330-
* @return \FactoryClass A FactoryClass instance.
331-
*/
332-
protected function getNewFactoryService()
333-
{
334-
$this->services['new_factory'] = $instance = new \FactoryClass();
335-
336-
$instance->foo = 'bar';
337-
338-
return $instance;
339-
}
340-
341303
/**
342304
* {@inheritdoc}
343305
*/
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?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">
3+
<services>
4+
<service id="foo" class="Foo">
5+
<factory method="createFoo">
6+
<service class="FooFactory">
7+
<factory method="createFooFactory">
8+
<service class="Foobar"/>
9+
</factory>
10+
</service>
11+
</factory>
12+
<configurator method="configureFoo">
13+
<service class="Bar">
14+
<configurator method="configureBar">
15+
<service class="Baz"/>
16+
</configurator>
17+
</service>
18+
</configurator>
19+
</service>
20+
</services>
21+
</container>

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -467,4 +467,33 @@ public function testLoadIndexedArguments()
467467

468468
$this->assertEquals(array('index_0' => 'app'), $container->findDefinition('logger')->getArguments());
469469
}
470+
471+
public function testLoadInlinedServices()
472+
{
473+
$container = new ContainerBuilder();
474+
$loader = new XmlFileLoader($container, new FileLocator(self::$fixturesPath.'/xml'));
475+
$loader->load('services21.xml');
476+
477+
$foo = $container->getDefinition('foo');
478+
479+
$fooFactory = $foo->getFactory();
480+
$this->assertInstanceOf('Symfony\Component\DependencyInjection\Definition', $fooFactory[0]);
481+
$this->assertSame('FooFactory', $fooFactory[0]->getClass());
482+
$this->assertSame('createFoo', $fooFactory[1]);
483+
484+
$fooFactoryFactory = $fooFactory[0]->getFactory();
485+
$this->assertInstanceOf('Symfony\Component\DependencyInjection\Definition', $fooFactoryFactory[0]);
486+
$this->assertSame('Foobar', $fooFactoryFactory[0]->getClass());
487+
$this->assertSame('createFooFactory', $fooFactoryFactory[1]);
488+
489+
$fooConfigurator = $foo->getConfigurator();
490+
$this->assertInstanceOf('Symfony\Component\DependencyInjection\Definition', $fooConfigurator[0]);
491+
$this->assertSame('Bar', $fooConfigurator[0]->getClass());
492+
$this->assertSame('configureFoo', $fooConfigurator[1]);
493+
494+
$barConfigurator = $fooConfigurator[0]->getConfigurator();
495+
$this->assertInstanceOf('Symfony\Component\DependencyInjection\Definition', $barConfigurator[0]);
496+
$this->assertSame('Baz', $barConfigurator[0]->getClass());
497+
$this->assertSame('configureBar', $barConfigurator[1]);
498+
}
470499
}

0 commit comments

Comments
 (0)
0