Permalink
Browse files

minor #24079 [3.4][DI] Inline trivial services (nicolas-grekas)

This PR was merged into the 3.4 branch.

Discussion
----------

[3.4][DI] Inline trivial services

| Q             | A
| ------------- | ---
| Branch?       | 3.4
| Bug fix?      | no
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | -
| License       | MIT
| Doc PR        | -

Unless I missed something, 3.4 also can have trivial services inlined.
And calls to factory methods also can be inlined btw, isn't it @stof?

Commits
-------

2a7f86f [3.4][DI] Inline trivial services
  • Loading branch information...
nicolas-grekas committed Sep 4, 2017
2 parents 80c0ec1 + 2a7f86f commit cf35e0c104590e7760bdc7b68b749d37d348464e
@@ -475,6 +475,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->isSynthetic() || $definition->getFile() || $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.
*
@@ -1669,14 +1713,19 @@ private function getServiceCall($id, Reference $reference = null)
if (null !== $reference && ContainerInterface::IGNORE_ON_UNINITIALIZED_REFERENCE === $reference->getInvalidBehavior()) {
$code = 'null';
} elseif ($this->asFiles && $this->container->hasDefinition($id)) {
if ($this->container->getDefinition($id)->isShared()) {
} elseif ($this->container->hasDefinition($id)) {
$definition = $this->container->getDefinition($id);
if ($this->isTrivialInstance($definition)) {
$code = substr($this->addNewInstance($definition, '', '', $id), 8, -2);
if ($definition->isShared()) {
$code = sprintf('$this->services[\'%s\'] = %s', $id, $code);
}
} elseif ($this->asFiles && $definition->isShared()) {
$code = sprintf("\$this->load(__DIR__.'/%s.php')", $this->generateMethodName($id));
} else {
$code = sprintf('$this->%s()', $this->generateMethodName($id));
}
} elseif ($this->container->hasDefinition($id) && !$this->container->getDefinition($id)->isPublic()) {
$code = sprintf('$this->%s()', $this->generateMethodName($id));
} elseif (null !== $reference && ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE !== $reference->getInvalidBehavior()) {
$code = sprintf('$this->get(\'%s\', ContainerInterface::NULL_ON_INVALID_REFERENCE)', $id);
} else {
@@ -126,6 +126,7 @@
$container
->register('factory_simple', 'SimpleFactoryClass')
->addArgument('foo')
->setDeprecated(true)
->setPublic(false)
;
$container
@@ -79,7 +79,7 @@ public function __construct()
*/
protected function getBarService()
{
$a = ${($_ = isset($this->services['foo.baz']) ? $this->services['foo.baz'] : $this->get('foo.baz')) && false ?: '_'};
$a = ${($_ = isset($this->services['foo.baz']) ? $this->services['foo.baz'] : $this->getFoo_BazService()) && false ?: '_'};
$this->services['bar'] = $instance = new \Bar\FooClass('foo', $a, $this->getParameter('foo_bar'));
@@ -97,7 +97,7 @@ protected function getBazService()
{
$this->services['baz'] = $instance = new \Baz();
$instance->setFoo(${($_ = isset($this->services['foo_with_inline']) ? $this->services['foo_with_inline'] : $this->get('foo_with_inline')) && false ?: '_'});
$instance->setFoo(${($_ = isset($this->services['foo_with_inline']) ? $this->services['foo_with_inline'] : $this->getFooWithInlineService()) && false ?: '_'});
return $instance;
}
@@ -125,7 +125,7 @@ protected function getConfiguredServiceSimpleService()
{
$this->services['configured_service_simple'] = $instance = new \stdClass();
${($_ = isset($this->services['configurator_service_simple']) ? $this->services['configurator_service_simple'] : $this->getConfiguratorServiceSimpleService()) && false ?: '_'}->configureStdClass($instance);
${($_ = isset($this->services['configurator_service_simple']) ? $this->services['configurator_service_simple'] : $this->services['configurator_service_simple'] = new \ConfClass('bar')) && false ?: '_'}->configureStdClass($instance);
return $instance;
}
@@ -181,7 +181,7 @@ protected function getDeprecatedServiceService()
*/
protected function getFactoryServiceService()
{
return $this->services['factory_service'] = ${($_ = isset($this->services['foo.baz']) ? $this->services['foo.baz'] : $this->get('foo.baz')) && false ?: '_'}->getInstance();
return $this->services['factory_service'] = ${($_ = isset($this->services['foo.baz']) ? $this->services['foo.baz'] : $this->getFoo_BazService()) && false ?: '_'}->getInstance();
}
/**
@@ -201,14 +201,14 @@ protected function getFactoryServiceSimpleService()
*/
protected function getFooService()
{
$a = ${($_ = isset($this->services['foo.baz']) ? $this->services['foo.baz'] : $this->get('foo.baz')) && false ?: '_'};
$a = ${($_ = isset($this->services['foo.baz']) ? $this->services['foo.baz'] : $this->getFoo_BazService()) && false ?: '_'};
$this->services['foo'] = $instance = \Bar\FooClass::getInstance('foo', $a, array($this->getParameter('foo') => 'foo is '.$this->getParameter('foo').'', 'foobar' => $this->getParameter('foo')), true, $this);
$instance->foo = 'bar';
$instance->moo = $a;
$instance->qux = array($this->getParameter('foo') => 'foo is '.$this->getParameter('foo').'', 'foobar' => $this->getParameter('foo'));
$instance->setBar(${($_ = isset($this->services['bar']) ? $this->services['bar'] : $this->get('bar')) && false ?: '_'});
$instance->setBar(${($_ = isset($this->services['bar']) ? $this->services['bar'] : $this->getBarService()) && false ?: '_'});
$instance->initialize();
sc_configure($instance);
@@ -238,7 +238,7 @@ protected function getFooBarService()
{
$class = $this->getParameter('foo_class');
return new $class(${($_ = isset($this->services['deprecated_service']) ? $this->services['deprecated_service'] : $this->get('deprecated_service')) && false ?: '_'});
return new $class(${($_ = isset($this->services['deprecated_service']) ? $this->services['deprecated_service'] : $this->getDeprecatedServiceService()) && false ?: '_'});
}
/**
@@ -263,7 +263,7 @@ protected function getFooWithInlineService()
protected function getLazyContextService()
{
return $this->services['lazy_context'] = new \LazyContext(new RewindableGenerator(function () {
yield 'k1' => ${($_ = isset($this->services['foo.baz']) ? $this->services['foo.baz'] : $this->get('foo.baz')) && false ?: '_'};
yield 'k1' => ${($_ = isset($this->services['foo.baz']) ? $this->services['foo.baz'] : $this->getFoo_BazService()) && false ?: '_'};
yield 'k2' => $this;
}, 2), new RewindableGenerator(function () {
return new \EmptyIterator();
@@ -278,7 +278,7 @@ protected function getLazyContextService()
protected function getLazyContextIgnoreInvalidRefService()
{
return $this->services['lazy_context_ignore_invalid_ref'] = new \LazyContext(new RewindableGenerator(function () {
yield 0 => ${($_ = isset($this->services['foo.baz']) ? $this->services['foo.baz'] : $this->get('foo.baz')) && false ?: '_'};
yield 0 => ${($_ = isset($this->services['foo.baz']) ? $this->services['foo.baz'] : $this->getFoo_BazService()) && false ?: '_'};
if ($this->has('invalid')) {
yield 1 => ${($_ = isset($this->services['invalid']) ? $this->services['invalid'] : $this->get('invalid', ContainerInterface::NULL_ON_INVALID_REFERENCE)) && false ?: '_'};
}
@@ -300,15 +300,15 @@ protected function getMethodCall1Service()
$this->services['method_call1'] = $instance = new \Bar\FooClass();
$instance->setBar(${($_ = isset($this->services['foo']) ? $this->services['foo'] : $this->get('foo')) && false ?: '_'});
$instance->setBar(${($_ = isset($this->services['foo']) ? $this->services['foo'] : $this->getFooService()) && false ?: '_'});
$instance->setBar(${($_ = isset($this->services['foo2']) ? $this->services['foo2'] : $this->get('foo2', ContainerInterface::NULL_ON_INVALID_REFERENCE)) && false ?: '_'});
if ($this->has('foo3')) {
$instance->setBar(${($_ = isset($this->services['foo3']) ? $this->services['foo3'] : $this->get('foo3', ContainerInterface::NULL_ON_INVALID_REFERENCE)) && false ?: '_'});
}
if ($this->has('foobaz')) {
$instance->setBar(${($_ = isset($this->services['foobaz']) ? $this->services['foobaz'] : $this->get('foobaz', ContainerInterface::NULL_ON_INVALID_REFERENCE)) && false ?: '_'});
}
$instance->setBar((${($_ = isset($this->services['foo']) ? $this->services['foo'] : $this->get('foo')) && false ?: '_'}->foo() . (($this->hasParameter("foo")) ? ($this->getParameter("foo")) : ("default"))));
$instance->setBar((${($_ = isset($this->services['foo']) ? $this->services['foo'] : $this->getFooService()) && false ?: '_'}->foo() . (($this->hasParameter("foo")) ? ($this->getParameter("foo")) : ("default"))));
return $instance;
}
@@ -346,7 +346,7 @@ protected function getConfiguratorServiceService()
{
$this->services['configurator_service'] = $instance = new \ConfClass();
$instance->setFoo(${($_ = isset($this->services['baz']) ? $this->services['baz'] : $this->get('baz')) && false ?: '_'});
$instance->setFoo(${($_ = isset($this->services['baz']) ? $this->services['baz'] : $this->getBazService()) && false ?: '_'});
return $instance;
}
@@ -365,9 +365,13 @@ protected function getConfiguratorServiceSimpleService()
* 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.
*/
protected 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->services['factory_simple'] = new \SimpleFactoryClass('foo');
}
@@ -381,7 +385,7 @@ protected function getInlinedService()
$this->services['inlined'] = $instance = new \Bar();
$instance->pub = 'pub';
$instance->setBaz(${($_ = isset($this->services['baz']) ? $this->services['baz'] : $this->get('baz')) && false ?: '_'});
$instance->setBaz(${($_ = isset($this->services['baz']) ? $this->services['baz'] : $this->getBazService()) && false ?: '_'});
return $instance;
}
@@ -102,7 +102,18 @@ use Symfony\Component\DependencyInjection\Argument\RewindableGenerator;
// This file has been auto-generated by the Symfony Dependency Injection Component for internal use.
// Returns the public 'factory_service_simple' shared service.
return $this->services['factory_service_simple'] = (new \SimpleFactoryClass('foo'))->getInstance();
return $this->services['factory_service_simple'] = ${($_ = isset($this->services['factory_simple']) ? $this->services['factory_simple'] : $this->load(__DIR__.'/getFactorySimpleService.php')) && false ?: '_'}->getInstance();
[Container%s/getFactorySimpleService.php] => <?php
use Symfony\Component\DependencyInjection\Argument\RewindableGenerator;
// This file has been auto-generated by the Symfony Dependency Injection Component for internal use.
// Returns the private 'factory_simple' shared service.
@trigger_error('The "factory_simple" service is deprecated. You should stop using it, as it will soon be removed.', E_USER_DEPRECATED);
return $this->services['factory_simple'] = new \SimpleFactoryClass('foo');
[Container%s/getFooService.php] => <?php
@@ -272,6 +283,7 @@ class Container%s extends Container
'deprecated_service' => __DIR__.'/getDeprecatedServiceService.php',
'factory_service' => __DIR__.'/getFactoryServiceService.php',
'factory_service_simple' => __DIR__.'/getFactoryServiceSimpleService.php',
'factory_simple' => __DIR__.'/getFactorySimpleService.php',
'foo' => __DIR__.'/getFooService.php',
'foo.baz' => __DIR__.'/getFoo_BazService.php',
'foo_with_inline' => __DIR__.'/getFooWithInlineService.php',
@@ -281,6 +293,9 @@ class Container%s extends Container
'new_factory_service' => __DIR__.'/getNewFactoryServiceService.php',
'service_from_static_method' => __DIR__.'/getServiceFromStaticMethodService.php',
);
$this->privates = array(
'factory_simple' => true,
);
$this->aliases = array(
'alias_for_alias' => 'foo',
'alias_for_foo' => 'foo',
Oops, something went wrong.

0 comments on commit cf35e0c

Please sign in to comment.