From c1e1e999f3a1529de99d41621e6d37cdd7533edb Mon Sep 17 00:00:00 2001 From: Nicolas Grekas Date: Thu, 12 Jan 2017 13:28:30 +0100 Subject: [PATCH] [DI] Remove synthetic services from methodMap + generated methods --- .../DependencyInjection/Container.php | 11 +++-- .../DependencyInjection/Dumper/PhpDumper.php | 42 +++++++++---------- .../Tests/ContainerTest.php | 8 ++-- .../Tests/Fixtures/php/services9.php | 14 ------- .../Tests/Fixtures/php/services9_compiled.php | 14 ------- 5 files changed, 31 insertions(+), 58 deletions(-) diff --git a/src/Symfony/Component/DependencyInjection/Container.php b/src/Symfony/Component/DependencyInjection/Container.php index cd144ead493d2..43b210ea6811b 100644 --- a/src/Symfony/Component/DependencyInjection/Container.php +++ b/src/Symfony/Component/DependencyInjection/Container.php @@ -205,10 +205,13 @@ public function set($id, $service) public function has($id) { for ($i = 2;;) { - if ('service_container' === $id - || isset($this->aliases[$id]) - || isset($this->services[$id]) - ) { + if ('service_container' === $id) { + return true; + } + if (isset($this->aliases[$id])) { + $id = $this->aliases[$id]; + } + if (isset($this->services[$id])) { return true; } diff --git a/src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php b/src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php index a4d6698d9e288..6a772fb7dbfcf 100644 --- a/src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php +++ b/src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php @@ -591,15 +591,16 @@ private function addServiceConfigurator($id, Definition $definition, $variableNa */ private function addService($id, Definition $definition) { + if ($definition->isSynthetic()) { + return ''; + } $this->definitionVariables = new \SplObjectStorage(); $this->referenceVariables = array(); $this->variableCount = 0; $return = array(); - if ($definition->isSynthetic()) { - $return[] = '@throws RuntimeException always since this service is expected to be injected dynamically'; - } elseif ($class = $definition->getClass()) { + if ($class = $definition->getClass()) { $class = $this->container->resolveEnvPlaceholders($class); $return[] = sprintf('@return %s A %s instance', 0 === strpos($class, '%') ? 'object' : '\\'.ltrim($class, '\\'), ltrim($class, '\\')); } elseif ($definition->getFactory()) { @@ -680,26 +681,22 @@ private function addService($id, Definition $definition) $code .= $isProxyCandidate ? $this->getProxyDumper()->getProxyFactoryCode($definition, $id, $methodName) : ''; - if ($definition->isSynthetic()) { - $code .= sprintf(" throw new RuntimeException('You have requested a synthetic service (\"%s\"). The DIC does not know how to construct this service.');\n }\n", $id); - } else { - if ($definition->isDeprecated()) { - $code .= sprintf(" @trigger_error(%s, E_USER_DEPRECATED);\n\n", $this->export($definition->getDeprecationMessage($id))); - } - - $code .= - $this->addServiceInclude($id, $definition). - $this->addServiceLocalTempVariables($id, $definition). - $this->addServiceInlinedDefinitions($id, $definition). - $this->addServiceInstance($id, $definition). - $this->addServiceInlinedDefinitionsSetup($id, $definition). - $this->addServiceProperties($id, $definition). - $this->addServiceMethodCalls($id, $definition). - $this->addServiceConfigurator($id, $definition). - $this->addServiceReturn($id, $definition) - ; + if ($definition->isDeprecated()) { + $code .= sprintf(" @trigger_error(%s, E_USER_DEPRECATED);\n\n", $this->export($definition->getDeprecationMessage($id))); } + $code .= + $this->addServiceInclude($id, $definition). + $this->addServiceLocalTempVariables($id, $definition). + $this->addServiceInlinedDefinitions($id, $definition). + $this->addServiceInstance($id, $definition). + $this->addServiceInlinedDefinitionsSetup($id, $definition). + $this->addServiceProperties($id, $definition). + $this->addServiceMethodCalls($id, $definition). + $this->addServiceConfigurator($id, $definition). + $this->addServiceReturn($id, $definition) + ; + $this->definitionVariables = null; $this->referenceVariables = null; @@ -950,7 +947,8 @@ private function addNormalizedIds() */ private function addMethodMap() { - if (!$definitions = $this->container->getDefinitions()) { + $definitions = $this->container->getDefinitions(); + if (!$definitions || !$definitions = array_filter($definitions, function ($def) { return !$def->isSynthetic(); })) { return ''; } diff --git a/src/Symfony/Component/DependencyInjection/Tests/ContainerTest.php b/src/Symfony/Component/DependencyInjection/Tests/ContainerTest.php index 8620fc4d7b124..d6a3518d6ae32 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/ContainerTest.php +++ b/src/Symfony/Component/DependencyInjection/Tests/ContainerTest.php @@ -285,15 +285,15 @@ public function testGetCircularReference() } /** - * @expectedException \Symfony\Component\DependencyInjection\Exception\RuntimeException - * @expectedExceptionMessage You have requested a synthetic service ("request"). The DIC does not know how to construct this service. + * @expectedException \Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException + * @expectedExceptionMessage You have requested a non-existent service "request". */ - public function testGetSyntheticServiceAlwaysThrows() + public function testGetSyntheticServiceThrows() { require_once __DIR__.'/Fixtures/php/services9.php'; $container = new \ProjectServiceContainer(); - $container->get('request', ContainerInterface::NULL_ON_INVALID_REFERENCE); + $container->get('request'); } public function testHas() diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services9.php b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services9.php index bc2eb1e0a60c9..27dd5e8ea3811 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services9.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services9.php @@ -50,7 +50,6 @@ public function __construct() 'method_call1' => 'getMethodCall1Service', 'new_factory' => 'getNewFactoryService', 'new_factory_service' => 'getNewFactoryServiceService', - 'request' => 'getRequestService', 'service_from_static_method' => 'getServiceFromStaticMethodService', ); $this->privates = array( @@ -384,19 +383,6 @@ protected function getNewFactoryServiceService() return $instance; } - /** - * Gets the 'request' service. - * - * This service is shared. - * This method always returns the same instance of the service. - * - * @throws RuntimeException always since this service is expected to be injected dynamically - */ - protected function getRequestService() - { - throw new RuntimeException('You have requested a synthetic service ("request"). The DIC does not know how to construct this service.'); - } - /** * Gets the 'service_from_static_method' service. * 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 e6a2cf98a75ab..97b2c1d900b6a 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services9_compiled.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services9_compiled.php @@ -46,7 +46,6 @@ public function __construct() 'lazy_context_ignore_invalid_ref' => 'getLazyContextIgnoreInvalidRefService', 'method_call1' => 'getMethodCall1Service', 'new_factory_service' => 'getNewFactoryServiceService', - 'request' => 'getRequestService', 'service_from_static_method' => 'getServiceFromStaticMethodService', ); $this->aliases = array( @@ -377,19 +376,6 @@ protected function getNewFactoryServiceService() return $instance; } - /** - * Gets the 'request' service. - * - * This service is shared. - * This method always returns the same instance of the service. - * - * @throws RuntimeException always since this service is expected to be injected dynamically - */ - protected function getRequestService() - { - throw new RuntimeException('You have requested a synthetic service ("request"). The DIC does not know how to construct this service.'); - } - /** * Gets the 'service_from_static_method' service. *