From 7a42199115de18c358cb9d61935d3503f6e9374f Mon Sep 17 00:00:00 2001 From: Nicolas Grekas Date: Sat, 7 Jan 2017 12:45:28 +0100 Subject: [PATCH] =?UTF-8?q?[DI]=20Dont=20call=20get()=20when=20not=20requi?= =?UTF-8?q?red=20-=20=C2=B5-optim?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../DependencyInjection/Dumper/PhpDumper.php | 19 ++++++++++------ .../Tests/Fixtures/php/services31.php | 6 ++--- .../Tests/Fixtures/php/services32.php | 6 ++--- .../Tests/Fixtures/php/services9.php | 22 +++++++++---------- .../Tests/Fixtures/php/services9_compiled.php | 22 +++++++++---------- 5 files changed, 40 insertions(+), 35 deletions(-) diff --git a/src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php b/src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php index ad6e8fdd24b2..dd97b2a68b41 100644 --- a/src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php +++ b/src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php @@ -1539,19 +1539,24 @@ private function getServiceCall($id, Reference $reference = null) } if ($this->container->hasDefinition($id) && !$this->container->getDefinition($id)->isPublic()) { - // The following is PHP 5.5 syntax for what could be written as "(\$this->services['$id'] ?? \$this->{$this->generateMethodName($id)}())" on PHP>=7.0 - - return "\${(\$_ = isset(\$this->services['$id']) ? \$this->services['$id'] : \$this->{$this->generateMethodName($id)}()) && false ?: '_'}"; - } - if (null !== $reference && ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE !== $reference->getInvalidBehavior()) { - return sprintf('$this->get(\'%s\', ContainerInterface::NULL_ON_INVALID_REFERENCE)', $id); + $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 { if ($this->container->hasAlias($id)) { $id = (string) $this->container->getAlias($id); } - return sprintf('$this->get(\'%s\')', $id); + $code = sprintf('$this->get(\'%s\')', $id); } + + if ($this->container->hasDefinition($id) && (!$this->container->getDefinition($id)->isPublic() || $this->container->getDefinition($id)->isShared())) { + // The following is PHP 5.5 syntax for what could be written as "(\$this->services['$id'] ?? $code)" on PHP>=7.0 + + $code = "\${(\$_ = isset(\$this->services['$id']) ? \$this->services['$id'] : $code) && false ?: '_'}"; + } + + return $code; } /** diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services31.php b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services31.php index 33e947bc2d30..9c9f8d930f95 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services31.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services31.php @@ -62,11 +62,11 @@ protected function getBarService() $this->services['bar'] = $instance = new \stdClass(); $instance->foo = array(0 => /** @closure-proxy Symfony\Component\DependencyInjection\Tests\Fixtures\Container31\Foo::withNoArgs */ function () { - return $this->get('foo')->withNoArgs(); + return ${($_ = isset($this->services['foo']) ? $this->services['foo'] : $this->get('foo')) && false ?: '_'}->withNoArgs(); }, 1 => /** @closure-proxy Symfony\Component\DependencyInjection\Tests\Fixtures\Container31\Foo::withArgs */ function ($a, \Symfony\Component\DependencyInjection\Tests\Fixtures\Container31\Foo $b = NULL, $c = array(0 => 123)) { - return $this->get('foo')->withArgs($a, $b, $c); + return ${($_ = isset($this->services['foo']) ? $this->services['foo'] : $this->get('foo')) && false ?: '_'}->withArgs($a, $b, $c); }, 2 => /** @closure-proxy Symfony\Component\DependencyInjection\Tests\Fixtures\Container31\Foo::withRefs */ function &(&$a = NULL, &$b) { - return $this->get('foo')->withRefs($a, $b); + return ${($_ = isset($this->services['foo']) ? $this->services['foo'] : $this->get('foo')) && false ?: '_'}->withRefs($a, $b); }); return $instance; diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services32.php b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services32.php index 1407ac60b657..d312e402c262 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services32.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services32.php @@ -62,11 +62,11 @@ protected function getBarService() $this->services['bar'] = $instance = new \stdClass(); $instance->foo = array(0 => /** @closure-proxy Symfony\Component\DependencyInjection\Tests\Fixtures\Container32\Foo::withVariadic */ function ($a, &...$c) { - return $this->get('foo')->withVariadic($a, ...$c); + return ${($_ = isset($this->services['foo']) ? $this->services['foo'] : $this->get('foo')) && false ?: '_'}->withVariadic($a, ...$c); }, 1 => /** @closure-proxy Symfony\Component\DependencyInjection\Tests\Fixtures\Container32\Foo::withNullable */ function (?int $a) { - return $this->get('foo')->withNullable($a); + return ${($_ = isset($this->services['foo']) ? $this->services['foo'] : $this->get('foo')) && false ?: '_'}->withNullable($a); }, 2 => /** @closure-proxy Symfony\Component\DependencyInjection\Tests\Fixtures\Container32\Foo::withReturnType */ function () { - return $this->get('foo')->withReturnType(); + return ${($_ = isset($this->services['foo']) ? $this->services['foo'] : $this->get('foo')) && false ?: '_'}->withReturnType(); }); return $instance; diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services9.php b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services9.php index a03826de9368..bc2eb1e0a60c 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services9.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services9.php @@ -76,7 +76,7 @@ public function __construct() */ protected function getBarService() { - $a = $this->get('foo.baz'); + $a = ${($_ = isset($this->services['foo.baz']) ? $this->services['foo.baz'] : $this->get('foo.baz')) && 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($this->get('foo_with_inline')); + $instance->setFoo(${($_ = isset($this->services['foo_with_inline']) ? $this->services['foo_with_inline'] : $this->get('foo_with_inline')) && false ?: '_'}); return $instance; } @@ -113,7 +113,7 @@ protected function getBazService() protected function getClosureProxyService() { return $this->services['closure_proxy'] = new \BarClass(/** @closure-proxy BarClass::getBaz */ function () { - return $this->get('closure_proxy')->getBaz(); + return ${($_ = isset($this->services['closure_proxy']) ? $this->services['closure_proxy'] : $this->get('closure_proxy')) && false ?: '_'}->getBaz(); }); } @@ -217,7 +217,7 @@ protected function getDeprecatedServiceService() */ protected function getFactoryServiceService() { - return $this->services['factory_service'] = $this->get('foo.baz')->getInstance(); + return $this->services['factory_service'] = ${($_ = isset($this->services['foo.baz']) ? $this->services['foo.baz'] : $this->get('foo.baz')) && false ?: '_'}->getInstance(); } /** @@ -243,14 +243,14 @@ protected function getFactoryServiceSimpleService() */ protected function getFooService() { - $a = $this->get('foo.baz'); + $a = ${($_ = isset($this->services['foo.baz']) ? $this->services['foo.baz'] : $this->get('foo.baz')) && 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($this->get('bar')); + $instance->setBar(${($_ = isset($this->services['bar']) ? $this->services['bar'] : $this->get('bar')) && false ?: '_'}); $instance->initialize(); sc_configure($instance); @@ -315,7 +315,7 @@ protected function getLazyContextService() { return $this->services['lazy_context'] = new \LazyContext(new RewindableGenerator(function() { yield 0 => 'foo'; - yield 1 => $this->get('foo.baz'); + yield 1 => ${($_ = isset($this->services['foo.baz']) ? $this->services['foo.baz'] : $this->get('foo.baz')) && false ?: '_'}; yield 2 => array($this->getParameter('foo') => 'foo is '.$this->getParameter('foo').'', 'foobar' => $this->getParameter('foo')); yield 3 => true; yield 4 => $this; @@ -333,7 +333,7 @@ protected function getLazyContextService() protected function getLazyContextIgnoreInvalidRefService() { return $this->services['lazy_context_ignore_invalid_ref'] = new \LazyContext(new RewindableGenerator(function() { - yield 0 => $this->get('foo.baz'); + yield 0 => ${($_ = isset($this->services['foo.baz']) ? $this->services['foo.baz'] : $this->get('foo.baz')) && false ?: '_'}; if ($this->has('invalid')) { yield 1 => $this->get('invalid', ContainerInterface::NULL_ON_INVALID_REFERENCE); } @@ -354,7 +354,7 @@ protected function getMethodCall1Service() $this->services['method_call1'] = $instance = new \Bar\FooClass(); - $instance->setBar($this->get('foo')); + $instance->setBar(${($_ = isset($this->services['foo']) ? $this->services['foo'] : $this->get('foo')) && false ?: '_'}); $instance->setBar($this->get('foo2', ContainerInterface::NULL_ON_INVALID_REFERENCE)); if ($this->has('foo3')) { $instance->setBar($this->get('foo3', ContainerInterface::NULL_ON_INVALID_REFERENCE)); @@ -426,7 +426,7 @@ protected function getConfiguratorServiceService() { $this->services['configurator_service'] = $instance = new \ConfClass(); - $instance->setFoo($this->get('baz')); + $instance->setFoo(${($_ = isset($this->services['baz']) ? $this->services['baz'] : $this->get('baz')) && false ?: '_'}); return $instance; } @@ -482,7 +482,7 @@ protected function getInlinedService() $this->services['inlined'] = $instance = new \Bar(); $instance->pub = 'pub'; - $instance->setBaz($this->get('baz')); + $instance->setBaz(${($_ = isset($this->services['baz']) ? $this->services['baz'] : $this->get('baz')) && false ?: '_'}); return $instance; } 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 d018c44d27ce..e6a2cf98a75a 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services9_compiled.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services9_compiled.php @@ -82,7 +82,7 @@ public function isFrozen() */ protected function getBarService() { - $a = $this->get('foo.baz'); + $a = ${($_ = isset($this->services['foo.baz']) ? $this->services['foo.baz'] : $this->get('foo.baz')) && false ?: '_'}; $this->services['bar'] = $instance = new \Bar\FooClass('foo', $a, $this->getParameter('foo_bar')); @@ -103,7 +103,7 @@ protected function getBazService() { $this->services['baz'] = $instance = new \Baz(); - $instance->setFoo($this->get('foo_with_inline')); + $instance->setFoo(${($_ = isset($this->services['foo_with_inline']) ? $this->services['foo_with_inline'] : $this->get('foo_with_inline')) && false ?: '_'}); return $instance; } @@ -119,7 +119,7 @@ protected function getBazService() protected function getClosureProxyService() { return $this->services['closure_proxy'] = new \BarClass(/** @closure-proxy BarClass::getBaz */ function () { - return $this->get('closure_proxy')->getBaz(); + return ${($_ = isset($this->services['closure_proxy']) ? $this->services['closure_proxy'] : $this->get('closure_proxy')) && false ?: '_'}->getBaz(); }); } @@ -134,7 +134,7 @@ protected function getClosureProxyService() protected function getConfiguredServiceService() { $a = new \ConfClass(); - $a->setFoo($this->get('baz')); + $a->setFoo(${($_ = isset($this->services['baz']) ? $this->services['baz'] : $this->get('baz')) && false ?: '_'}); $this->services['configured_service'] = $instance = new \stdClass(); @@ -213,7 +213,7 @@ protected function getDeprecatedServiceService() */ protected function getFactoryServiceService() { - return $this->services['factory_service'] = $this->get('foo.baz')->getInstance(); + return $this->services['factory_service'] = ${($_ = isset($this->services['foo.baz']) ? $this->services['foo.baz'] : $this->get('foo.baz')) && false ?: '_'}->getInstance(); } /** @@ -239,14 +239,14 @@ protected function getFactoryServiceSimpleService() */ protected function getFooService() { - $a = $this->get('foo.baz'); + $a = ${($_ = isset($this->services['foo.baz']) ? $this->services['foo.baz'] : $this->get('foo.baz')) && false ?: '_'}; $this->services['foo'] = $instance = \Bar\FooClass::getInstance('foo', $a, array('bar' => 'foo is bar', 'foobar' => 'bar'), true, $this); $instance->foo = 'bar'; $instance->moo = $a; $instance->qux = array('bar' => 'foo is bar', 'foobar' => 'bar'); - $instance->setBar($this->get('bar')); + $instance->setBar(${($_ = isset($this->services['bar']) ? $this->services['bar'] : $this->get('bar')) && false ?: '_'}); $instance->initialize(); sc_configure($instance); @@ -295,7 +295,7 @@ protected function getFooWithInlineService() $this->services['foo_with_inline'] = $instance = new \Foo(); $a->pub = 'pub'; - $a->setBaz($this->get('baz')); + $a->setBaz(${($_ = isset($this->services['baz']) ? $this->services['baz'] : $this->get('baz')) && false ?: '_'}); $instance->setBar($a); @@ -314,7 +314,7 @@ protected function getLazyContextService() { return $this->services['lazy_context'] = new \LazyContext(new RewindableGenerator(function() { yield 0 => 'foo'; - yield 1 => $this->get('foo.baz'); + yield 1 => ${($_ = isset($this->services['foo.baz']) ? $this->services['foo.baz'] : $this->get('foo.baz')) && false ?: '_'}; yield 2 => array('bar' => 'foo is '.'bar'.'', 'foobar' => 'bar'); yield 3 => true; yield 4 => $this; @@ -332,7 +332,7 @@ protected function getLazyContextService() protected function getLazyContextIgnoreInvalidRefService() { return $this->services['lazy_context_ignore_invalid_ref'] = new \LazyContext(new RewindableGenerator(function() { - yield 0 => $this->get('foo.baz'); + yield 0 => ${($_ = isset($this->services['foo.baz']) ? $this->services['foo.baz'] : $this->get('foo.baz')) && false ?: '_'}; })); } @@ -350,7 +350,7 @@ protected function getMethodCall1Service() $this->services['method_call1'] = $instance = new \Bar\FooClass(); - $instance->setBar($this->get('foo')); + $instance->setBar(${($_ = isset($this->services['foo']) ? $this->services['foo'] : $this->get('foo')) && false ?: '_'}); $instance->setBar(NULL); $instance->setBar(($this->get("foo")->foo() . (($this->hasParameter("foo")) ? ($this->getParameter("foo")) : ("default"))));