From c0c1881fe437fc22c1a842bdaad7cb4102b0df31 Mon Sep 17 00:00:00 2001 From: Nicolas Grekas Date: Mon, 17 Jul 2017 22:21:41 +0200 Subject: [PATCH] [DI] Optimize use of private and pre-defined services --- .../DependencyInjection/Container.php | 23 -------- .../DependencyInjection/Dumper/PhpDumper.php | 52 +++++-------------- .../Tests/ContainerTest.php | 29 ++--------- .../Tests/Fixtures/php/services1-1.php | 3 +- .../Tests/Fixtures/php/services1.php | 3 +- .../Tests/Fixtures/php/services10.php | 3 +- .../Tests/Fixtures/php/services12.php | 3 +- .../Tests/Fixtures/php/services13.php | 3 +- .../Tests/Fixtures/php/services19.php | 3 +- .../Tests/Fixtures/php/services24.php | 3 +- .../Tests/Fixtures/php/services26.php | 3 +- .../Tests/Fixtures/php/services33.php | 3 +- .../Tests/Fixtures/php/services8.php | 3 +- .../Tests/Fixtures/php/services9_compiled.php | 25 ++++----- .../Fixtures/php/services_array_params.php | 3 +- .../Tests/Fixtures/php/services_locator.php | 27 +++++----- .../Fixtures/php/services_private_frozen.php | 15 +++--- .../Fixtures/php/services_subscriber.php | 19 +++---- 18 files changed, 77 insertions(+), 146 deletions(-) diff --git a/src/Symfony/Component/DependencyInjection/Container.php b/src/Symfony/Component/DependencyInjection/Container.php index db3202c252..def4c4ce15 100644 --- a/src/Symfony/Component/DependencyInjection/Container.php +++ b/src/Symfony/Component/DependencyInjection/Container.php @@ -50,11 +50,6 @@ class Container implements ResettableContainerInterface protected $aliases = array(); protected $loading = array(); - /** - * @internal - */ - protected $privates = array(); - private $envCache = array(); private $compiled = false; @@ -155,10 +150,6 @@ class Container implements ResettableContainerInterface throw new InvalidArgumentException('You cannot set service "service_container".'); } - if (isset($this->privates[$id])) { - throw new InvalidArgumentException(sprintf('You cannot set the private service "%s".', $id)); - } - if (isset($this->methodMap[$id])) { throw new InvalidArgumentException(sprintf('You cannot set the pre-defined service "%s".', $id)); } @@ -185,9 +176,6 @@ class Container implements ResettableContainerInterface */ public function has($id) { - if (isset($this->privates[$id])) { - return false; - } if (isset($this->aliases[$id])) { $id = $this->aliases[$id]; } @@ -224,13 +212,6 @@ class Container implements ResettableContainerInterface */ public function get($id, $invalidBehavior = self::EXCEPTION_ON_INVALID_REFERENCE) { - if (isset($this->privates[$id])) { - if (self::EXCEPTION_ON_INVALID_REFERENCE === $invalidBehavior) { - throw new ServiceNotFoundException($id); - } - - return; - } if (isset($this->aliases[$id])) { $id = $this->aliases[$id]; } @@ -293,10 +274,6 @@ class Container implements ResettableContainerInterface */ public function initialized($id) { - if (isset($this->privates[$id])) { - return false; - } - if (isset($this->aliases[$id])) { $id = $this->aliases[$id]; } diff --git a/src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php b/src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php index 4c64bbdf87..87c0a96922 100644 --- a/src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php +++ b/src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php @@ -401,7 +401,7 @@ class PhpDumper extends Dumper $instantiation = ''; if (!$isProxyCandidate && $definition->isShared()) { - $instantiation = "\$this->services['$id'] = ".($isSimpleInstance ? '' : '$instance'); + $instantiation = sprintf('$this->%s[\'%s\'] = %s', $this->container->getDefinition($id)->isPublic() ? 'services' : 'privates', $id, $isSimpleInstance ? '' : '$instance'); } elseif (!$isSimpleInstance) { $instantiation = '$instance'; } @@ -646,7 +646,7 @@ EOF; // with proxies, for 5.3.3 compatibility, the getter must be public to be accessible to the initializer $isProxyCandidate = $this->getProxyDumper()->isProxyCandidate($definition); - $visibility = $isProxyCandidate ? 'public' : 'protected'; + $visibility = $isProxyCandidate ? 'public' : ($definition->isPublic() ? 'protected' : 'private'); $methodName = $this->generateMethodName($id); $code = <<addMethodMap(); - $code .= $this->addPrivateServices(); $code .= $this->addAliases(); $code .= <<<'EOF' @@ -886,40 +886,12 @@ EOF; $code = " \$this->methodMap = array(\n"; ksort($definitions); foreach ($definitions as $id => $definition) { - $code .= ' '.$this->export($id).' => '.$this->export($this->generateMethodName($id)).",\n"; - } - - return $code." );\n"; - } - - /** - * Adds the privates property definition. - * - * @return string - */ - private function addPrivateServices() - { - if (!$definitions = $this->container->getDefinitions()) { - return ''; - } - - $code = ''; - ksort($definitions); - foreach ($definitions as $id => $definition) { - if (!$definition->isPublic()) { - $code .= ' '.$this->export($id)." => true,\n"; + if ($definition->isPublic()) { + $code .= ' '.$this->export($id).' => '.$this->export($this->generateMethodName($id)).",\n"; } } - if (empty($code)) { - return ''; - } - - $out = " \$this->privates = array(\n"; - $out .= $code; - $out .= " );\n"; - - return $out; + return $code." );\n"; } /** @@ -1535,8 +1507,12 @@ EOF; return '$this'; } - if ($this->container->hasDefinition($id) && !$this->container->getDefinition($id)->isPublic()) { + if ($this->container->hasDefinition($id)) { $code = sprintf('$this->%s()', $this->generateMethodName($id)); + + if ($this->container->getDefinition($id)->isShared()) { + $code = sprintf('($this->%s[\'%s\'] ?? %s)', $this->container->getDefinition($id)->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); } else { @@ -1547,10 +1523,6 @@ EOF; $code = sprintf('$this->get(\'%s\')', $id); } - if ($this->container->hasDefinition($id) && $this->container->getDefinition($id)->isShared()) { - $code = "(\$this->services['$id'] ?? $code)"; - } - return $code; } diff --git a/src/Symfony/Component/DependencyInjection/Tests/ContainerTest.php b/src/Symfony/Component/DependencyInjection/Tests/ContainerTest.php index 4a3566b27a..6e72c6075d 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/ContainerTest.php +++ b/src/Symfony/Component/DependencyInjection/Tests/ContainerTest.php @@ -134,7 +134,7 @@ class ContainerTest extends TestCase $sc = new ProjectServiceContainer(); $sc->set('foo', $obj = new \stdClass()); - $this->assertEquals(array('service_container', 'internal', 'bar', 'foo_bar', 'foo.baz', 'circular', 'throw_exception', 'throws_exception_on_service_configuration', 'internal_dependency', 'foo'), $sc->getServiceIds(), '->getServiceIds() returns defined service ids by factory methods in the method map, followed by service ids defined by set()'); + $this->assertEquals(array('service_container', 'bar', 'foo_bar', 'foo.baz', 'circular', 'throw_exception', 'throws_exception_on_service_configuration', 'internal_dependency', 'foo'), $sc->getServiceIds(), '->getServiceIds() returns defined service ids by factory methods in the method map, followed by service ids defined by set()'); } public function testSet() @@ -340,26 +340,6 @@ class ContainerTest extends TestCase $this->assertTrue($clone->isPrivate()); } - /** - * @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException - * @expectedExceptionMessage You cannot set the private service "internal". - */ - public function testUnsetInternalPrivateService() - { - $c = new ProjectServiceContainer(); - $c->set('internal', null); - } - - /** - * @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException - * @expectedExceptionMessage You cannot set the private service "internal". - */ - public function testChangeInternalPrivateService() - { - $c = new ProjectServiceContainer(); - $c->set('internal', new \stdClass()); - } - public function testCheckExistenceOfAnInternalPrivateService() { $c = new ProjectServiceContainer(); @@ -396,7 +376,6 @@ class ProjectServiceContainer extends Container public $__foo_baz; public $__internal; protected $methodMap = array( - 'internal' => 'getInternalService', 'bar' => 'getBarService', 'foo_bar' => 'getFooBarService', 'foo.baz' => 'getFoo_BazService', @@ -414,13 +393,13 @@ class ProjectServiceContainer extends Container $this->__foo_bar = new \stdClass(); $this->__foo_baz = new \stdClass(); $this->__internal = new \stdClass(); - $this->privates = array('internal' => true); + $this->privates = array(); $this->aliases = array('alias' => 'bar'); } protected function getInternalService() { - return $this->services['internal'] = $this->__internal; + return $this->privates['internal'] = $this->__internal; } protected function getBarService() @@ -459,7 +438,7 @@ class ProjectServiceContainer extends Container { $this->services['internal_dependency'] = $instance = new \stdClass(); - $instance->internal = isset($this->services['internal']) ? $this->services['internal'] : $this->getInternalService(); + $instance->internal = $this->privates['internal'] ?? $this->getInternalService(); return $instance; } diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services1-1.php b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services1-1.php index 32f488e8f3..01f159f842 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services1-1.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services1-1.php @@ -22,13 +22,14 @@ class Container extends AbstractContainer { private $parameters; private $targetDirs = array(); + private $privates = array(); /** * Constructor. */ public function __construct() { - $this->services = array(); + $this->services = $this->privates = array(); $this->aliases = array(); } diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services1.php b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services1.php index baa1874a9a..967146d410 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services1.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services1.php @@ -20,13 +20,14 @@ class ProjectServiceContainer extends Container { private $parameters; private $targetDirs = array(); + private $privates = array(); /** * Constructor. */ public function __construct() { - $this->services = array(); + $this->services = $this->privates = array(); $this->aliases = array(); } diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services10.php b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services10.php index 97bdc025c8..78ce815472 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services10.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services10.php @@ -20,6 +20,7 @@ class ProjectServiceContainer extends Container { private $parameters; private $targetDirs = array(); + private $privates = array(); /** * Constructor. @@ -28,7 +29,7 @@ class ProjectServiceContainer extends Container { $this->parameters = $this->getDefaultParameters(); - $this->services = array(); + $this->services = $this->privates = array(); $this->methodMap = array( 'test' => 'getTestService', ); diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services12.php b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services12.php index 4961457b29..db9faadd18 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services12.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services12.php @@ -20,6 +20,7 @@ class ProjectServiceContainer extends Container { private $parameters; private $targetDirs = array(); + private $privates = array(); /** * Constructor. @@ -32,7 +33,7 @@ class ProjectServiceContainer extends Container } $this->parameters = $this->getDefaultParameters(); - $this->services = array(); + $this->services = $this->privates = array(); $this->methodMap = array( 'test' => 'getTestService', ); diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services13.php b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services13.php index da30f18e5f..35a68aa9b3 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services13.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services13.php @@ -20,13 +20,14 @@ class ProjectServiceContainer extends Container { private $parameters; private $targetDirs = array(); + private $privates = array(); /** * Constructor. */ public function __construct() { - $this->services = array(); + $this->services = $this->privates = array(); $this->methodMap = array( 'bar' => 'getBarService', ); diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services19.php b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services19.php index 81ab4a6ca6..6e384da3b9 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services19.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services19.php @@ -20,13 +20,14 @@ class ProjectServiceContainer extends Container { private $parameters; private $targetDirs = array(); + private $privates = array(); /** * Constructor. */ public function __construct() { - $this->services = array(); + $this->services = $this->privates = array(); $this->methodMap = array( 'service_from_anonymous_factory' => 'getServiceFromAnonymousFactoryService', 'service_with_method_call_and_factory' => 'getServiceWithMethodCallAndFactoryService', diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services24.php b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services24.php index f64c99f5d0..382613ccf4 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services24.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services24.php @@ -20,13 +20,14 @@ class ProjectServiceContainer extends Container { private $parameters; private $targetDirs = array(); + private $privates = array(); /** * Constructor. */ public function __construct() { - $this->services = array(); + $this->services = $this->privates = array(); $this->methodMap = array( 'foo' => 'getFooService', ); diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services26.php b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services26.php index 54166bb016..0fc608186a 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services26.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services26.php @@ -20,6 +20,7 @@ class ProjectServiceContainer extends Container { private $parameters; private $targetDirs = array(); + private $privates = array(); /** * Constructor. @@ -28,7 +29,7 @@ class ProjectServiceContainer extends Container { $this->parameters = $this->getDefaultParameters(); - $this->services = array(); + $this->services = $this->privates = array(); $this->methodMap = array( 'test' => 'getTestService', ); diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services33.php b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services33.php index 3c3ac43244..548ce20c8b 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services33.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services33.php @@ -20,13 +20,14 @@ class ProjectServiceContainer extends Container { private $parameters; private $targetDirs = array(); + private $privates = array(); /** * Constructor. */ public function __construct() { - $this->services = array(); + $this->services = $this->privates = array(); $this->methodMap = array( 'Symfony\\Component\\DependencyInjection\\Tests\\Fixtures\\Container33\\Foo' => 'getSymfony_Component_DependencyInjection_Tests_Fixtures_Container33_FooService', ); diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services8.php b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services8.php index b9721035df..17bbbfae71 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services8.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services8.php @@ -20,6 +20,7 @@ class ProjectServiceContainer extends Container { private $parameters; private $targetDirs = array(); + private $privates = array(); /** * Constructor. @@ -28,7 +29,7 @@ class ProjectServiceContainer extends Container { $this->parameters = $this->getDefaultParameters(); - $this->services = array(); + $this->services = $this->privates = array(); $this->aliases = array(); } 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 8fb33d8cbd..3d47bac17e 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services9_compiled.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services9_compiled.php @@ -20,6 +20,7 @@ class ProjectServiceContainer extends Container { private $parameters; private $targetDirs = array(); + private $privates = array(); /** * Constructor. @@ -28,7 +29,7 @@ class ProjectServiceContainer extends Container { $this->parameters = $this->getDefaultParameters(); - $this->services = array(); + $this->services = $this->privates = array(); $this->methodMap = array( 'BAR' => 'getBARService', 'BAR2' => 'getBAR2Service', @@ -87,7 +88,7 @@ class ProjectServiceContainer extends Container { $this->services['BAR'] = $instance = new \stdClass(); - $instance->bar = ($this->services['bar'] ?? $this->get('bar')); + $instance->bar = ($this->services['bar'] ?? $this->getBar3Service()); return $instance; } @@ -115,7 +116,7 @@ class ProjectServiceContainer extends Container */ protected function getBar3Service() { - $a = ($this->services['foo.baz'] ?? $this->get('foo.baz')); + $a = ($this->services['foo.baz'] ?? $this->getFoo_BazService()); $this->services['bar'] = $instance = new \Bar\FooClass('foo', $a, $this->getParameter('foo_bar')); @@ -149,7 +150,7 @@ class ProjectServiceContainer extends Container { $this->services['baz'] = $instance = new \Baz(); - $instance->setFoo(($this->services['foo_with_inline'] ?? $this->get('foo_with_inline'))); + $instance->setFoo(($this->services['foo_with_inline'] ?? $this->getFooWithInlineService())); return $instance; } @@ -165,7 +166,7 @@ class ProjectServiceContainer extends Container protected function getConfiguredServiceService() { $a = new \ConfClass(); - $a->setFoo(($this->services['baz'] ?? $this->get('baz'))); + $a->setFoo(($this->services['baz'] ?? $this->getBazService())); $this->services['configured_service'] = $instance = new \stdClass(); @@ -244,7 +245,7 @@ class ProjectServiceContainer extends Container */ protected function getFactoryServiceService() { - return $this->services['factory_service'] = ($this->services['foo.baz'] ?? $this->get('foo.baz'))->getInstance(); + return $this->services['factory_service'] = ($this->services['foo.baz'] ?? $this->getFoo_BazService())->getInstance(); } /** @@ -270,14 +271,14 @@ class ProjectServiceContainer extends Container */ protected function getFooService() { - $a = ($this->services['foo.baz'] ?? $this->get('foo.baz')); + $a = ($this->services['foo.baz'] ?? $this->getFoo_BazService()); $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->services['bar'] ?? $this->get('bar'))); + $instance->setBar(($this->services['bar'] ?? $this->getBar3Service())); $instance->initialize(); sc_configure($instance); @@ -326,7 +327,7 @@ class ProjectServiceContainer extends Container $this->services['foo_with_inline'] = $instance = new \Foo(); $a->pub = 'pub'; - $a->setBaz(($this->services['baz'] ?? $this->get('baz'))); + $a->setBaz(($this->services['baz'] ?? $this->getBazService())); $instance->setBar($a); @@ -344,7 +345,7 @@ class ProjectServiceContainer extends Container protected function getLazyContextService() { return $this->services['lazy_context'] = new \LazyContext(new RewindableGenerator(function () { - yield 'k1' => ($this->services['foo.baz'] ?? $this->get('foo.baz')); + yield 'k1' => ($this->services['foo.baz'] ?? $this->getFoo_BazService()); yield 'k2' => $this; }, 2), new RewindableGenerator(function () { return new \EmptyIterator(); @@ -362,7 +363,7 @@ class ProjectServiceContainer extends Container protected function getLazyContextIgnoreInvalidRefService() { return $this->services['lazy_context_ignore_invalid_ref'] = new \LazyContext(new RewindableGenerator(function () { - yield 0 => ($this->services['foo.baz'] ?? $this->get('foo.baz')); + yield 0 => ($this->services['foo.baz'] ?? $this->getFoo_BazService()); }, 1), new RewindableGenerator(function () { return new \EmptyIterator(); }, 0)); @@ -382,7 +383,7 @@ class ProjectServiceContainer extends Container $this->services['method_call1'] = $instance = new \Bar\FooClass(); - $instance->setBar(($this->services['foo'] ?? $this->get('foo'))); + $instance->setBar(($this->services['foo'] ?? $this->getFooService())); $instance->setBar(NULL); $instance->setBar(($this->get("foo")->foo() . (($this->hasParameter("foo")) ? ($this->getParameter("foo")) : ("default")))); diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_array_params.php b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_array_params.php index 9c1643fa6f..d5f4f2ae9d 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_array_params.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_array_params.php @@ -20,6 +20,7 @@ class ProjectServiceContainer extends Container { private $parameters; private $targetDirs = array(); + private $privates = array(); /** * Constructor. @@ -32,7 +33,7 @@ class ProjectServiceContainer extends Container } $this->parameters = $this->getDefaultParameters(); - $this->services = array(); + $this->services = $this->privates = array(); $this->methodMap = array( 'bar' => 'getBarService', ); 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 98305c1a1d..a413e1cfdc 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_locator.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_locator.php @@ -20,16 +20,16 @@ class ProjectServiceContainer extends Container { private $parameters; private $targetDirs = array(); + private $privates = array(); /** * Constructor. */ public function __construct() { - $this->services = array(); + $this->services = $this->privates = array(); $this->methodMap = array( 'bar_service' => 'getBarServiceService', - 'baz_service' => 'getBazServiceService', 'foo_service' => 'getFooServiceService', 'translator.loader_1' => 'getTranslator_Loader1Service', 'translator.loader_2' => 'getTranslator_Loader2Service', @@ -38,9 +38,6 @@ class ProjectServiceContainer extends Container 'translator_2' => 'getTranslator2Service', 'translator_3' => 'getTranslator3Service', ); - $this->privates = array( - 'baz_service' => true, - ); $this->aliases = array(); } @@ -71,7 +68,7 @@ class ProjectServiceContainer extends Container */ protected function getBarServiceService() { - return $this->services['bar_service'] = new \stdClass(($this->services['baz_service'] ?? $this->getBazServiceService())); + return $this->services['bar_service'] = new \stdClass(($this->privates['baz_service'] ?? $this->getBazServiceService())); } /** @@ -85,9 +82,9 @@ class ProjectServiceContainer extends Container protected function getFooServiceService() { return $this->services['foo_service'] = new \Symfony\Component\DependencyInjection\ServiceLocator(array('bar' => function () { - return ($this->services['bar_service'] ?? $this->get('bar_service')); + return ($this->services['bar_service'] ?? $this->getBarServiceService()); }, 'baz' => function (): \stdClass { - return ($this->services['baz_service'] ?? $this->getBazServiceService()); + return ($this->privates['baz_service'] ?? $this->getBazServiceService()); }, 'nil' => function () { return NULL; })); @@ -143,7 +140,7 @@ class ProjectServiceContainer extends Container protected function getTranslator1Service() { return $this->services['translator_1'] = new \Symfony\Component\DependencyInjection\Tests\Fixtures\StubbedTranslator(new \Symfony\Component\DependencyInjection\ServiceLocator(array('translator.loader_1' => function () { - return ($this->services['translator.loader_1'] ?? $this->get('translator.loader_1')); + return ($this->services['translator.loader_1'] ?? $this->getTranslator_Loader1Service()); }))); } @@ -158,10 +155,10 @@ class ProjectServiceContainer extends Container protected function getTranslator2Service() { $this->services['translator_2'] = $instance = new \Symfony\Component\DependencyInjection\Tests\Fixtures\StubbedTranslator(new \Symfony\Component\DependencyInjection\ServiceLocator(array('translator.loader_2' => function () { - return ($this->services['translator.loader_2'] ?? $this->get('translator.loader_2')); + return ($this->services['translator.loader_2'] ?? $this->getTranslator_Loader2Service()); }))); - $instance->addResource('db', ($this->services['translator.loader_2'] ?? $this->get('translator.loader_2')), 'nl'); + $instance->addResource('db', ($this->services['translator.loader_2'] ?? $this->getTranslator_Loader2Service()), 'nl'); return $instance; } @@ -176,10 +173,10 @@ class ProjectServiceContainer extends Container */ protected function getTranslator3Service() { - $a = ($this->services['translator.loader_3'] ?? $this->get('translator.loader_3')); + $a = ($this->services['translator.loader_3'] ?? $this->getTranslator_Loader3Service()); $this->services['translator_3'] = $instance = new \Symfony\Component\DependencyInjection\Tests\Fixtures\StubbedTranslator(new \Symfony\Component\DependencyInjection\ServiceLocator(array('translator.loader_3' => function () { - return ($this->services['translator.loader_3'] ?? $this->get('translator.loader_3')); + return ($this->services['translator.loader_3'] ?? $this->getTranslator_Loader3Service()); }))); $instance->addResource('db', $a, 'nl'); @@ -200,8 +197,8 @@ class ProjectServiceContainer extends Container * * @return \stdClass A stdClass instance */ - protected function getBazServiceService() + private function getBazServiceService() { - return $this->services['baz_service'] = new \stdClass(); + 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 1df28c328a..7bd14e1608 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 @@ -20,21 +20,18 @@ class ProjectServiceContainer extends Container { private $parameters; private $targetDirs = array(); + private $privates = array(); /** * Constructor. */ public function __construct() { - $this->services = array(); + $this->services = $this->privates = array(); $this->methodMap = array( 'bar_service' => 'getBarServiceService', - 'baz_service' => 'getBazServiceService', 'foo_service' => 'getFooServiceService', ); - $this->privates = array( - 'baz_service' => true, - ); $this->aliases = array(); } @@ -65,7 +62,7 @@ class ProjectServiceContainer extends Container */ protected function getBarServiceService() { - return $this->services['bar_service'] = new \stdClass(($this->services['baz_service'] ?? $this->getBazServiceService())); + return $this->services['bar_service'] = new \stdClass(($this->privates['baz_service'] ?? $this->getBazServiceService())); } /** @@ -78,7 +75,7 @@ class ProjectServiceContainer extends Container */ protected function getFooServiceService() { - return $this->services['foo_service'] = new \stdClass(($this->services['baz_service'] ?? $this->getBazServiceService())); + return $this->services['foo_service'] = new \stdClass(($this->privates['baz_service'] ?? $this->getBazServiceService())); } /** @@ -93,8 +90,8 @@ class ProjectServiceContainer extends Container * * @return \stdClass A stdClass instance */ - protected function getBazServiceService() + private function getBazServiceService() { - return $this->services['baz_service'] = new \stdClass(); + return $this->privates['baz_service'] = 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 1d852fab35..aaaeaa45f6 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_subscriber.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_subscriber.php @@ -20,21 +20,18 @@ class ProjectServiceContainer extends Container { private $parameters; private $targetDirs = array(); + private $privates = array(); /** * Constructor. */ public function __construct() { - $this->services = array(); + $this->services = $this->privates = array(); $this->methodMap = array( 'Symfony\\Component\\DependencyInjection\\Tests\\Fixtures\\TestServiceSubscriber' => 'getSymfony_Component_DependencyInjection_Tests_Fixtures_TestServiceSubscriberService', - 'autowired.Symfony\\Component\\DependencyInjection\\Tests\\Fixtures\\CustomDefinition' => 'getAutowired_Symfony_Component_DependencyInjection_Tests_Fixtures_CustomDefinitionService', 'foo_service' => 'getFooServiceService', ); - $this->privates = array( - 'autowired.Symfony\\Component\\DependencyInjection\\Tests\\Fixtures\\CustomDefinition' => true, - ); $this->aliases = array(); } @@ -81,13 +78,13 @@ class ProjectServiceContainer extends Container 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->services['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->getAutowired_Symfony_Component_DependencyInjection_Tests_Fixtures_CustomDefinitionService()); }, 'Symfony\\Component\\DependencyInjection\\Tests\\Fixtures\\TestServiceSubscriber' => function (): \Symfony\Component\DependencyInjection\Tests\Fixtures\TestServiceSubscriber { - return ($this->services['Symfony\Component\DependencyInjection\Tests\Fixtures\TestServiceSubscriber'] ?? $this->get('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->get('Symfony\Component\DependencyInjection\Tests\Fixtures\TestServiceSubscriber')); + 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->services['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->getAutowired_Symfony_Component_DependencyInjection_Tests_Fixtures_CustomDefinitionService()); }))); } @@ -105,8 +102,8 @@ class ProjectServiceContainer extends Container * * @return \Symfony\Component\DependencyInjection\Tests\Fixtures\CustomDefinition A Symfony\Component\DependencyInjection\Tests\Fixtures\CustomDefinition instance */ - protected function getAutowired_Symfony_Component_DependencyInjection_Tests_Fixtures_CustomDefinitionService() + private function getAutowired_Symfony_Component_DependencyInjection_Tests_Fixtures_CustomDefinitionService() { - return $this->services['autowired.Symfony\Component\DependencyInjection\Tests\Fixtures\CustomDefinition'] = new \Symfony\Component\DependencyInjection\Tests\Fixtures\CustomDefinition(); + return $this->privates['autowired.Symfony\Component\DependencyInjection\Tests\Fixtures\CustomDefinition'] = new \Symfony\Component\DependencyInjection\Tests\Fixtures\CustomDefinition(); } }