8000 do not inline service factories · symfony/symfony@663ae9f · GitHub
[go: up one dir, main page]

Skip to content

Commit 663ae9f

Browse files
committed
do not inline service factories
The `XmlDumper`, which is used in the full-stack framework to dump the used container, is not capable to dump inlined factories.
1 parent 900558c commit 663ae9f

File tree

4 files changed

+62
-7
lines changed

4 files changed

+62
-7
lines changed

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

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,6 @@ public function process(ContainerBuilder $container)
6565

6666
$configurator = $this->inlineArguments($container, array($definition->getConfigurator()));
6767
$definition->setConfigurator($configurator[0]);
68-
69-
$factory = $this->inlineArguments($container, array($definition->getFactory()));
70-
$definition->setFactory($factory[0]);
7168
}
7269
}
7370

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,23 @@ 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+
240257
protected function process(ContainerBuilder $container)
241258
{
242259
$repeatedPass = new RepeatedPass(array(new AnalyzeServiceReferencesPass(), new InlineServiceDefinitionsPass()));

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,4 +131,26 @@ public function provideDecoratedServicesData()
131131
", include $fixturesPath.'/containers/container16.php'),
132132
);
133133
}
134+
135+
/**
136+
* @dataProvider provideCompiledContainerData
137+
*/
138+
public function testCompiledContainerCanBeDumped($containerFile)
139+
{
140+
$fixturesPath = __DIR__.'/../Fixtures';
141+
$container = require $fixturesPath.'/containers/'.$containerFile.'.php';
142+
$container->compile();
143+
$dumper = new XmlDumper($container);
144+
$dumper->dump();
145+
}
146+
147+
public function provideCompiledContainerData()
148+
{
149+
return array(
150+
array('container8'),
151+
array('container11'),
152+
array('container12'),
153+
array('container14'),
154+
);
155+
}
134156
}

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

Lines changed: 23 additions & 4 deletions
1E0A
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ public function __construct()
4747
'foo_bar' => 'getFooBarService',
4848
'foo_with_inline' => 'getFooWithInlineService',
4949
'method_call1' => 'getMethodCall1Service',
50+
'new_factory' => 'getNewFactoryService',
5051
'new_factory_service' => 'getNewFactoryServiceService',
5152
'request' => 'getRequestService',
5253
'service_from_static_method' => 'getServiceFromStaticMethodService',
@@ -282,10 +283,7 @@ protected function getMethodCall1Service()
282283
*/
283284
protected function getNewFactoryServiceService()
284285
{
285-
$a = new \FactoryClass();
286-
$a->foo = 'bar';
287-
288-
$this->services['new_factory_service'] = $instance = $a->getInstance();
286+
$this->services['new_factory_service'] = $instance = $this->get('new_factory')->getInstance();
289287

290288
$instance->foo = 'bar';
291289

@@ -328,6 +326,27 @@ protected function synchronizeRequestService()
328326
}
329327
}
330328

329+
/**
330+
* Gets the 'new_factory' service.
331+
*
332+
* This service is shared.
333+
* This method always returns the same instance of the service.
334+
*
335+
* This service is private.
336+
* If you want to be able to request this service from the container directly,
337+
* make it public, otherwise you might end up with broken code.
338+
*
339+
* @return \FactoryClass A FactoryClass instance.
340+
*/
341+
protected function getNewFactoryService()
342+
{
343+
$this->services['new_factory'] = $instance = new \FactoryClass();
344+
345+
$instance->foo = 'bar';
346+
347+
return $instance;
348+
}
349+
331350
/**
332351
* {@inheritdoc}
333352
*/

0 commit comments

Comments
 (0)
0