diff --git a/src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php b/src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php
index 1492979e8ee9d..a090938baa362 100644
--- a/src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php
+++ b/src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php
@@ -446,6 +446,50 @@ private function isSimpleInstance($id, Definition $definition)
return true;
}
+ /**
+ * Checks if the definition is a trivial instance.
+ *
+ * @param Definition $definition
+ *
+ * @return bool
+ */
+ private function isTrivialInstance(Definition $definition)
+ {
+ if ($definition->isPublic() || $definition->getMethodCalls() || $definition->getProperties() || $definition->getConfigurator()) {
+ return false;
+ }
+ if ($definition->isDeprecated() || $definition->isLazy() || $definition->getFactory() || 3 < count($definition->getArguments())) {
+ return false;
+ }
+
+ foreach ($definition->getArguments() as $arg) {
+ if (!$arg || ($arg instanceof Reference && 'service_container' !== (string) $arg)) {
+ continue;
+ }
+ if (is_array($arg) && 3 >= count($arg)) {
+ foreach ($arg as $k => $v) {
+ if ($this->dumpValue($k) !== $this->dumpValue($k, false)) {
+ return false;
+ }
+ if (!$v || ($v instanceof Reference && 'service_container' !== (string) $v)) {
+ continue;
+ }
+ if (!is_scalar($v) || $this->dumpValue($v) !== $this->dumpValue($v, false)) {
+ return false;
+ }
+ }
+ } elseif (!is_scalar($arg) || $this->dumpValue($arg) !== $this->dumpValue($arg, false)) {
+ return false;
+ }
+ }
+
+ if (false !== strpos($this->dumpLiteralClass($this->dumpValue($definition->getClass())), '$')) {
+ return false;
+ }
+
+ return true;
+ }
+
/**
* Adds method calls to a service definition.
*
@@ -675,7 +719,7 @@ private function addServices()
foreach ($definitions as $id => $definition) {
if ($definition->isPublic()) {
$publicServices .= $this->addService($id, $definition);
- } else {
+ } elseif (!$this->isTrivialInstance($definition)) {
$privateServices .= $this->addService($id, $definition);
}
}
@@ -1504,10 +1548,18 @@ private function getServiceCall($id, Reference $reference = null)
}
if ($this->container->hasDefinition($id)) {
- $code = sprintf('$this->%s()', $this->generateMethodName($id));
+ $definition = $this->container->getDefinition($id);
- if ($this->container->getDefinition($id)->isShared()) {
- $code = sprintf('($this->%s[\'%s\'] ?? %s)', $this->container->getDefinition($id)->isPublic() ? 'services' : 'privates', $id, $code);
+ if ($definition->isPublic() || !$this->isTrivialInstance($definition)) {
+ $code = sprintf('$this->%s()', $this->generateMethodName($id));
+ } else {
+ $code = substr($this->addNewInstance($definition, '', '', $id), 8, -2);
+ if ($definition->isShared()) {
+ $code = sprintf('($this->privates[\'%s\'] = %s)', $id, $code);
+ }
+ }
+ if ($definition->isShared()) {
+ $code = sprintf('($this->%s[\'%s\'] ?? %s)', $definition->isPublic() ? 'services' : 'privates', $id, $code);
}
} elseif (null !== $reference && ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE !== $reference->getInvalidBehavior()) {
$code = sprintf('$this->get(\'%s\', ContainerInterface::NULL_ON_INVALID_REFERENCE)', $id);
diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/containers/container9.php b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/containers/container9.php
index 472a211537160..b662cf53c1211 100644
--- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/containers/container9.php
+++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/containers/container9.php
@@ -125,6 +125,7 @@
$container
->register('factory_simple', 'SimpleFactoryClass')
->addArgument('foo')
+ ->setDeprecated(true)
->setPublic(false)
;
$container
diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services9_compiled.php b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services9_compiled.php
index ef25100d11b5f..e59b6fed95da5 100644
--- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services9_compiled.php
+++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services9_compiled.php
@@ -231,7 +231,7 @@ protected function getFactoryServiceService()
*/
protected function getFactoryServiceSimpleService()
{
- return $this->services['factory_service_simple'] = (new \SimpleFactoryClass('foo'))->getInstance();
+ return $this->services['factory_service_simple'] = ($this->privates['factory_simple'] ?? $this->getFactorySimpleService())->getInstance();
}
/**
@@ -372,6 +372,20 @@ protected function getServiceFromStaticMethodService()
return $this->services['service_from_static_method'] = \Bar\FooClass::getInstance();
}
+ /**
+ * Gets the private 'factory_simple' shared service.
+ *
+ * @return \SimpleFactoryClass
+ *
+ * @deprecated The "factory_simple" service is deprecated. You should stop using it, as it will soon be removed.
+ */
+ private function getFactorySimpleService()
+ {
+ @trigger_error('The "factory_simple" service is deprecated. You should stop using it, as it will soon be removed.', E_USER_DEPRECATED);
+
+ return $this->privates['factory_simple'] = new \SimpleFactoryClass('foo');
+ }
+
/**
* {@inheritdoc}
*/
diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_locator.php b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_locator.php
index cd4d4ca2246f4..852011ac7d1ca 100644
--- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_locator.php
+++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_locator.php
@@ -74,7 +74,7 @@ public function isCompiled()
*/
protected function getBarServiceService()
{
- return $this->services['bar_service'] = new \stdClass(($this->privates['baz_service'] ?? $this->getBazServiceService()));
+ return $this->services['bar_service'] = new \stdClass(($this->privates['baz_service'] ?? ($this->privates['baz_service'] = new \stdClass())));
}
/**
@@ -87,7 +87,7 @@ protected function getFooServiceService()
return $this->services['foo_service'] = new \Symfony\Component\DependencyInjection\ServiceLocator(array('bar' => function () {
return ($this->services['bar_service'] ?? $this->getBarServiceService());
}, 'baz' => function (): \stdClass {
- return ($this->privates['baz_service'] ?? $this->getBazServiceService());
+ return ($this->privates['baz_service'] ?? ($this->privates['baz_service'] = new \stdClass()));
}, 'nil' => function () {
return NULL;
}));
@@ -169,14 +169,4 @@ protected function getTranslator3Service()
return $instance;
}
-
- /**
- * Gets the private 'baz_service' shared service.
- *
- * @return \stdClass
- */
- private function getBazServiceService()
- {
- return $this->privates['baz_service'] = new \stdClass();
- }
}
diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_private_frozen.php b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_private_frozen.php
index 678a62130ba7e..9a0611949b57e 100644
--- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_private_frozen.php
+++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_private_frozen.php
@@ -68,7 +68,7 @@ public function isCompiled()
*/
protected function getBarServiceService()
{
- return $this->services['bar_service'] = new \stdClass(($this->privates['baz_service'] ?? $this->getBazServiceService()));
+ return $this->services['bar_service'] = new \stdClass(($this->privates['baz_service'] ?? ($this->privates['baz_service'] = new \stdClass())));
}
/**
@@ -78,16 +78,6 @@ protected function getBarServiceService()
*/
protected function getFooServiceService()
{
- return $this->services['foo_service'] = new \stdClass(($this->privates['baz_service'] ?? $this->getBazServiceService()));
- }
-
- /**
- * Gets the private 'baz_service' shared service.
- *
- * @return \stdClass
- */
- private function getBazServiceService()
- {
- return $this->privates['baz_service'] = new \stdClass();
+ return $this->services['foo_service'] = new \stdClass(($this->privates['baz_service'] ?? ($this->privates['baz_service'] = new \stdClass())));
}
}
diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_private_in_expression.php b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_private_in_expression.php
index 96e8c8c52c493..8e3060e130436 100644
--- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_private_in_expression.php
+++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_private_in_expression.php
@@ -67,16 +67,6 @@ public function isCompiled()
*/
protected function getPublicFooService()
{
- return $this->services['public_foo'] = new \stdClass(($this->privates['private_foo'] ?? $this->getPrivateFooService()));
- }
-
- /**
- * Gets the private 'private_foo' shared service.
- *
- * @return \stdClass
- */
- private function getPrivateFooService()
- {
- return $this->privates['private_foo'] = new \stdClass();
+ return $this->services['public_foo'] = new \stdClass(($this->privates['private_foo'] ?? ($this->privates['private_foo'] = new \stdClass())));
}
}
diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_subscriber.php b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_subscriber.php
index 0c8311c5ff4cc..e29fed9d2a727 100644
--- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_subscriber.php
+++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_subscriber.php
@@ -79,23 +79,13 @@ protected function getSymfony_Component_DependencyInjection_Tests_Fixtures_TestS
protected function getFooServiceService()
{
return $this->services['foo_service'] = new \Symfony\Component\DependencyInjection\Tests\Fixtures\TestServiceSubscriber(new \Symfony\Component\DependencyInjection\ServiceLocator(array('Symfony\\Component\\DependencyInjection\\Tests\\Fixtures\\CustomDefinition' => function (): \Symfony\Component\DependencyInjection\Tests\Fixtures\CustomDefinition {
- return ($this->privates['autowired.Symfony\Component\DependencyInjection\Tests\Fixtures\CustomDefinition'] ?? $this->getAutowired_Symfony_Component_DependencyInjection_Tests_Fixtures_CustomDefinitionService());
+ return ($this->privates['autowired.Symfony\Component\DependencyInjection\Tests\Fixtures\CustomDefinition'] ?? ($this->privates['autowired.Symfony\Component\DependencyInjection\Tests\Fixtures\CustomDefinition'] = new \Symfony\Component\DependencyInjection\Tests\Fixtures\CustomDefinition()));
}, 'Symfony\\Component\\DependencyInjection\\Tests\\Fixtures\\TestServiceSubscriber' => function (): \Symfony\Component\DependencyInjection\Tests\Fixtures\TestServiceSubscriber {
return ($this->services['Symfony\Component\DependencyInjection\Tests\Fixtures\TestServiceSubscriber'] ?? $this->getSymfony_Component_DependencyInjection_Tests_Fixtures_TestServiceSubscriberService());
}, 'bar' => function (): \Symfony\Component\DependencyInjection\Tests\Fixtures\CustomDefinition {
return ($this->services['Symfony\Component\DependencyInjection\Tests\Fixtures\TestServiceSubscriber'] ?? $this->getSymfony_Component_DependencyInjection_Tests_Fixtures_TestServiceSubscriberService());
}, 'baz' => function (): \Symfony\Component\DependencyInjection\Tests\Fixtures\CustomDefinition {
- return ($this->privates['autowired.Symfony\Component\DependencyInjection\Tests\Fixtures\CustomDefinition'] ?? $this->getAutowired_Symfony_Component_DependencyInjection_Tests_Fixtures_CustomDefinitionService());
+ return ($this->privates['autowired.Symfony\Component\DependencyInjection\Tests\Fixtures\CustomDefinition'] ?? ($this->privates['autowired.Symfony\Component\DependencyInjection\Tests\Fixtures\CustomDefinition'] = new \Symfony\Component\DependencyInjection\Tests\Fixtures\CustomDefinition()));
})));
}
-
- /**
- * Gets the private 'autowired.Symfony\Component\DependencyInjection\Tests\Fixtures\CustomDefinition' shared autowired service.
- *
- * @return \Symfony\Component\DependencyInjection\Tests\Fixtures\CustomDefinition
- */
- private function getAutowired_Symfony_Component_DependencyInjection_Tests_Fixtures_CustomDefinitionService()
- {
- return $this->privates['autowired.Symfony\Component\DependencyInjection\Tests\Fixtures\CustomDefinition'] = new \Symfony\Component\DependencyInjection\Tests\Fixtures\CustomDefinition();
- }
}
diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/xml/services9.xml b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/xml/services9.xml
index 5f46821a0f90e..9b78f7d9eed3f 100644
--- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/xml/services9.xml
+++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/xml/services9.xml
@@ -112,6 +112,7 @@
foo
+ The "%service_id%" service is deprecated. You should stop using it, as it will soon be removed.
diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/yaml/services9.yml b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/yaml/services9.yml
index e91fbb208c1ae..ddd4c25dd5188 100644
--- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/yaml/services9.yml
+++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/yaml/services9.yml
@@ -105,6 +105,7 @@ services:
factory: [Bar\FooClass, getInstance]
factory_simple:
class: SimpleFactoryClass
+ deprecated: The "%service_id%" service is deprecated. You should stop using it, as it will soon be removed.
public: false
arguments: ['foo']
factory_service_simple: