8000 [3.4][DI] Inline trivial services · symfony/symfony@2a7f86f · GitHub
[go: up one dir, main page]

Skip to content

Commit 2a7f86f

Browse files
[3.4][DI] Inline trivial services
1 parent 80c0ec1 commit 2a7f86f

File tree

12 files changed

+139
-50
lines changed

12 files changed

+139
-50
lines changed

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

Lines changed: 53 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -475,6 +475,50 @@ private function isSimpleInstance($id, Definition $definition)
475475
return true;
476476
}
477477

478+
/**
479+
* Checks if the definition is a trivial instance.
480+
*
481+
* @param Definition $definition
482+
*
483+
* @return bool
484+
*/
485+
private function isTrivialInstance(Definition $definition)
486+
{
487+
if ($definition->isSynthetic() || $definition->getFile() || $definition->getMethodCalls() || $definition->getProperties() || $definition->getConfigurator()) {
488+
return false;
489+
}
490+
if ($definition->isDeprecated() || $definition->isLazy() || $definition->getFactory() || 3 < count($definition->getArguments())) {
491+
return false;
492+
}
493+
494+
foreach ($definition->getArguments() as $arg) {
495+
if (!$arg || ($arg instanceof Reference && 'service_container' !== (string) $arg)) {
496+
continue;
497+
}
498+
if (is_array($arg) && 3 >= count($arg)) {
499+
foreach ($arg as $k => $v) {
500+
if ($this->dumpValue($k) !== $this->dumpValue($k, false)) {
501+
return false;
502+
}
503+
if (!$v || ($v instanceof Reference && 'service_container' !== (string) $v)) {
504+
continue;
505+
}
506+
if (!is_scalar($v) || $this->dumpValue($v) !== $this->dumpValue($v, false)) {
507+
return false;
508+
}
509+
}
510+
} elseif (!is_scalar($arg) || $this->dumpValue($arg) !== $this->dumpValue($arg, false)) {
511+
return false;
512+
}
513+
}
514+
515+
if (false !== strpos($this->dumpLiteralClass($this->dumpValue($definition->getClass())), '$')) {
516+
return false;
517+
}
518+
519+
return true;
520+
}
521+
478522
/**
479523
* Adds method calls to a service definition.
480524
*
@@ -1669,14 +1713,19 @@ private function getServiceCall($id, Reference $reference = null)
16691713

16701714
if (null !== $reference && ContainerInterface::IGNORE_ON_UNINITIALIZED_REFERENCE === $reference->getInvalidBehavior()) {
16711715
$code = 'null';
1672-
} elseif ($this->asFiles && $this->container->hasDefinition($id)) {
1673-
if ($this->container->getDefinition($id)->isShared()) {
1716+
} elseif ($this->container->hasDefinition($id)) {
1717+
$definition = $this->container->getDefinition($id);
1718+
1719+
if ($this->isTrivialInstance($definition)) {
1720+
$code = substr($this->addNewInstance($definition, '', '', $id), 8, -2);
1721+
if ($definition->isShared()) {
1722+
$code = sprintf('$this->services[\'%s\'] = %s', $id, $code);
1723+
}
1724+
} elseif ($this->asFiles && $definition->isShared()) {
16741725
$code = sprintf("\$this->load(__DIR__.'/%s.php')", $this->generateMethodName($id));
16751726
} else {
16761727
$code = sprintf('$this->%s()', $this->generateMethodName($id));
16771728
}
1678-
} elseif ($this->container->hasDefinition($id) && !$this->container->getDefinition($id)->isPublic()) {
1679-
$code = sprintf('$this->%s()', $this->generateMethodName($id));
16801729
} elseif (null !== $reference && ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE !== $reference->getInvalidBehavior()) {
16811730
$code = sprintf('$this->get(\'%s\', ContainerInterface::NULL_ON_INVALID_REFERENCE)', $id);
16821731
} else {

src/Symfony/Component/DependencyInjection/Tests/Fixtures/containers/container9.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@
126126
$container
127127
->register('factory_simple', 'SimpleFactoryClass')
128128
->addArgument('foo')
129+
->setDeprecated(true)
129130
->setPublic(false)
130131
;
131132
$container

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

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ public function __construct()
7979
*/
8080
protected function getBarService()
8181
{
82-
$a = ${($_ = isset($this->services['foo.baz']) ? $this->services['foo.baz'] : $this->get('foo.baz')) && false ?: '_'};
82+
$a = ${($_ = isset($this->services['foo.baz']) ? $this->services['foo.baz'] : $this->getFoo_BazService()) && false ?: '_'};
8383

8484
$this->services['bar'] = $instance = new \Bar\FooClass('foo', $a, $this->getParameter('foo_bar'));
8585

@@ -97,7 +97,7 @@ protected function getBazService()
9797
{
9898
$this->services['baz'] = $instance = new \Baz();
9999

100-
$instance->setFoo(${($_ = isset($this->services['foo_with_inline']) ? $this->services['foo_with_inline'] : $this->get('foo_with_inline')) && false ?: '_'});
100+
$instance->setFoo(${($_ = isset($this->services['foo_with_inline']) ? $this->services['foo_with_inline'] : $this->getFooWithInlineService()) && false ?: '_'});
101101

102102
return $instance;
103103
}
@@ -125,7 +125,7 @@ protected function getConfiguredServiceSimpleService()
125125
{
126126
$this->services['configured_service_simple'] = $instance = new \stdClass();
127127

128-
${($_ = isset($this->services['configurator_service_simple']) ? $this->services['configurator_service_simple'] : $this->getConfiguratorServiceSimpleService()) && false ?: '_'}->configureStdClass($instance);
128+
${($_ = isset($this->services['configurator_service_simple']) ? $this->services['configurator_service_simple'] : $this->services['configurator_service_simple'] = new \ConfClass('bar')) && false ?: '_'}->configureStdClass($instance);
129129

130130
return $instance;
131131
}
@@ -181,7 +181,7 @@ protected function getDeprecatedServiceService()
181181
*/
182182
protected function getFactoryServiceService()
183183
{
184-
return $this->services['factory_service'] = ${($_ = isset($this->services['foo.baz']) ? $this->services['foo.baz'] : $this->get('foo.baz')) && false ?: '_'}->getInstance();
184+
return $this->services['factory_service'] = ${($_ = isset($this->services['foo.baz']) ? $this->services['foo.baz'] : $this->getFoo_BazService()) && false ?: '_'}->getInstance();
185185
}
186186

187187
/**
@@ -201,14 +201,14 @@ protected function getFactoryServiceSimpleService()
201201
*/
202202
protected function getFooService()
203203
{
204-
$a = ${($_ = isset($this->services['foo.baz']) ? $this->services['foo.baz'] : $this->get('foo.baz')) && false ?: '_'};
204+
$a = ${($_ = isset($this->services['foo.baz']) ? $this->services['foo.baz'] : $this->getFoo_BazService()) && false ?: '_'};
205205

206206
$this->services['foo'] = $instance = \Bar\FooClass::getInstance('foo', $a, array($this->getParameter('foo') => 'foo is '.$this->getParameter('foo').'', 'foobar' => $this->getParameter('foo')), true, $this);
207207

208208
$instance->foo = 'bar';
209209
$instance->moo = $a;
210210
$instance->qux = array($this->getParameter('foo') => 'foo is '.$this->getParameter('foo').'', 'foobar' => $this->getParameter('foo'));
211-
$instance->setBar(${($_ = isset($this->services['bar']) ? $this->services['bar'] : $this->get('bar')) && false ?: '_'});
211+
$instance->setBar(${($_ = isset($this->services['bar']) ? $this->services['bar'] : $this->getBarService()) && false ?: '_'});
212212
$instance->initialize();
213213
sc_configure($instance);
214214

@@ -238,7 +238,7 @@ protected function getFooBarService()
238238
{
239239
$class = $this->getParameter('foo_class');
240240

241-
return new $class(${($_ = isset($this->services['deprecated_service']) ? $this->services['deprecated_service'] : $this->get('deprecated_service')) && false ?: '_'});
241+
return new $class(${($_ = isset($this->services['deprecated_service']) ? $this->services['deprecated_service'] : $this->getDeprecatedServiceService()) && false ?: '_'});
242242
}
243243

244244
/**
@@ -263,7 +263,7 @@ protected function getFooWithInlineService()
263263
protected function getLazyContextService()
264264
{
265265
return $this->services['lazy_context'] = new \LazyContext(new RewindableGenerator(function () {
266-
yield 'k1' => ${($_ = isset($this->services['foo.baz']) ? $this->services['foo.baz'] : $this->get('foo.baz')) && false ?: '_'};
266+
yield 'k1' => ${($_ = isset($this->services['foo.baz']) ? $this->services['foo.baz'] : $this->getFoo_BazService()) && false ?: '_'};
267267
yield 'k2' => $this;
268268
}, 2), new RewindableGenerator(function () {
269269
return new \EmptyIterator();
@@ -278,7 +278,7 @@ protected function getLazyContextService()
278278
protected function getLazyContextIgnoreInvalidRefService()
279279
{
280280
return $this->services['lazy_context_ignore_invalid_ref'] = new \LazyContext(new RewindableGenerator(function () {
281-
yield 0 => ${($_ = isset($this->services['foo.baz']) ? $this->services['foo.baz'] : $this->get('foo.baz')) && false ?: '_'};
281+
yield 0 => ${($_ = isset($this->services['foo.baz']) ? $this->services['foo.baz'] : $this->getFoo_BazService()) && false ?: '_'};
282282
if ($this->has('invalid')) {
283283
yield 1 => ${($_ = isset($this->services['invalid']) ? $this->services['invalid'] : $this->get('invalid', ContainerInterface::NULL_ON_INVALID_REFERENCE)) && false ?: '_'};
284284
}
@@ -300,15 +300,15 @@ protected function getMethodCall1Service()
300300

301301
$this->services['method_call1'] = $instance = new \Bar\FooClass();
302302

303-
$instance->setBar(${($_ = isset($this->services['foo']) ? $this->services['foo'] : $this->get('foo')) && false ?: '_'});
303+
$instance->setBar(${($_ = isset($this->services['foo']) ? $this->services['foo'] : $this->getFooService()) && false ?: '_'});
304304
$instance->setBar(${($_ = isset($this->services['foo2']) ? $this->services['foo2'] : $this->get('foo2', ContainerInterface::NULL_ON_INVALID_REFERENCE)) && false ?: '_'});
305305
if ($this->has('foo3')) {
306306
$instance->setBar(${($_ = isset($this->services['foo3']) ? $this->services['foo3'] : $this->get('foo3', ContainerInterface::NULL_ON_INVALID_REFERENCE)) && false ?: '_'});
307307
}
308308
if ($this->has('foobaz')) {
309309
$instance->setBar(${($_ = isset($this->services['foobaz']) ? $this->services['foobaz'] : $this->get('foobaz', ContainerInterface::NULL_ON_INVALID_REFERENCE)) && false ?: '_'});
310310
}
311-
$instance->setBar((${($_ = isset($this->services['foo']) ? $this->services['foo'] : $this->get('foo')) && false ?: '_'}->foo() . (($this->hasParameter("foo")) ? ($this->getParameter("foo")) : ("default"))));
311+
$instance->setBar((${($_ = isset($this->services['foo']) ? $this->services['foo'] : $this->getFooService()) && false ?: '_'}->foo() . (($this->hasParameter("foo")) ? ($this->getParameter("foo")) : ("default"))));
312312

313313
return $instance;
314314
}
@@ -346,7 +346,7 @@ protected function getConfiguratorServiceService()
346346
{
347347
$this->services['configurator_service'] = $instance = new \ConfClass();
348348

349-
$instance->setFoo(${($_ = isset($this->services['baz']) ? $this->services['baz'] : $this->get('baz')) && false ?: '_'});
349+
$instance->setFoo(${($_ = isset($this->services['baz']) ? $this->services['baz'] : $this->getBazService()) && false ?: '_'});
350350

351351
return $instance;
352352
}
@@ -365,9 +365,13 @@ protected function getConfiguratorServiceSimpleService()
365365
* Gets the private 'factory_simple' shared service.
366366
*
367367
* @return \SimpleFactoryClass
368+
*
369+
* @deprecated The "factory_simple" service is deprecated. You should stop using it, as it will soon be removed.
368370
*/
369371
protected function getFactorySimpleService()
370372
{
373+
@trigger_error('The "factory_simple" service is deprecated. You should stop using it, as it will soon be removed.', E_USER_DEPRECATED);
374+
371375
return $this->services['factory_simple'] = new \SimpleFactoryClass('foo');
372376
}
373377

@@ -381,7 +385,7 @@ protected function getInlinedService()
381385
$this->services['inlined'] = $instance = new \Bar();
382386

383387
$instance->pub = 'pub';
384-
$instance->setBaz(${($_ = isset($this->services['baz']) ? $this->services['baz'] : $this->get('baz')) && false ?: '_'});
388+
$instance->setBaz(${($_ = isset($this->services['baz']) ? $this->services['baz'] : $this->getBazService()) && false ?: '_'});
385389

386390
return $instance;
387391
}

src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services9_as_files.txt

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,18 @@ use Symfony\Component\DependencyInjection\Argument\RewindableGenerator;
102102
// This file has been auto-generated by the Symfony Dependency Injection Component for internal use.
103103
// Returns the public 'factory_service_simple' shared service.
104104

105-
return $this->services['factory_service_simple'] = (new \SimpleFactoryClass('foo'))->getInstance();
105+
return $this->services['factory_service_simple'] = ${($_ = isset($this->services['factory_simple']) ? $this->services['factory_simple'] : $this->load(__DIR__.'/getFactorySimpleService.php')) && false ?: '_'}->getInstance();
106+
107+
[Container%s/getFactorySimpleService.php] => <?php
108+
109+
use Symfony\Component\DependencyInjection\Argument\RewindableGenerator;
110+
111+
// This file has been auto-generated by the Symfony Dependency Injection Component for internal use.
112+
// Returns the private 'factory_simple' shared service.
113+
114+
@trigger_error('The "factory_simple" service is deprecated. You should stop using it, as it will soon be removed.', E_USER_DEPRECATED);
115+
116+
return $this->services['factory_simple'] = new \SimpleFactoryClass('foo');
106117

107118
[Container%s/getFooService.php] => <?php
108119

@@ -272,6 +283,7 @@ class Container%s extends Container
272283
'deprecated_service' => __DIR__.'/getDeprecatedServiceService.php',
273284
'factory_service' => __DIR__.'/getFactoryServiceService.php',
274285
'factory_service_simple' => __DIR__.'/getFactoryServiceSimpleService.php',
286+
'factory_simple' => __DIR__.'/getFactorySimpleService.php',
275287
'foo' => __DIR__.'/getFooService.php',
276288
'foo.baz' => __DIR__.'/getFoo_BazService.php',
277289
'foo_with_inline' => __DIR__.'/getFooWithInlineService.php',
@@ -281,6 +293,9 @@ class Container%s extends Container
281293
'new_factory_service' => __DIR__.'/getNewFactoryServiceService.php',
282294
'service_from_static_method' => __DIR__.'/getServiceFromStaticMethodService.php',
283295
);
296+
$this->privates = array(
297+
'factory_simple' => true,
298+
);
284299
$this->aliases = array(
285300
'alias_for_alias' => 'foo',
286301
'alias_for_foo' => 'foo',

0 commit comments

Comments
 (0)
0