8000 feature #21244 [DI] Remove synthetic services from methodMap + genera… · symfony/symfony@f7679f7 · GitHub
[go: up one dir, main page]

Skip to content

Commit f7679f7

Browse files
committed
feature #21244 [DI] Remove synthetic services from methodMap + generated methods (nicolas-grekas)
This PR was merged into the 3.3-dev branch. Discussion ---------- [DI] Remove synthetic services from methodMap + generated methods | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | - | License | MIT | Doc PR | - For synthetic services, the generated methods are just dead code to fill opcache ;) And having them in "methodMap" prevents using the property for checking if a service comes from a (non-synthetic) definition or not. This prepares some changes that we'd like to do in 4.0, see #19668. Commits ------- c1e1e99 [DI] Remove synthetic services from methodMap + generated methods
2 parents 1e10227 + c1e1e99 commit f7679f7

File tree

5 files changed

+31
-58
lines changed

5 files changed

+31
-58
lines changed

src/Symfony/Component/DependencyInjection/Container.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -205,10 +205,13 @@ public function set($id, $service)
205205
public function has($id)
206206
{
207207
for ($i = 2;;) {
208-
if ('service_container' === $id
209-
|| isset($this->aliases[$id])
210-
|| isset($this->services[$id])
211-
) {
208+
if ('service_container' === $id) {
209+
return true;
210+
}
211+
if (isset($this->aliases[$id])) {
212+
$id = $this->aliases[$id];
213+
}
214+
if (isset($this->services[$id])) {
212215
return true;
213216
}
214217

src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -591,15 +591,16 @@ private function addServiceConfigurator($id, Definition $definition, $variableNa
591591
*/
592592
private function addService($id, Definition $definition)
593593
{
594+
if ($definition->isSynthetic()) {
595+
return '';
596+
}
594597
$this->definitionVariables = new \SplObjectStorage();
595598
$this->referenceVariables = array();
596599
$this->variableCount = 0;
597600

598601
$return = array();
599602

600-
if ($definition->isSynthetic()) {
601-
$return[] = '@throws RuntimeException always since this service is expected to be injected dynamically';
602-
} elseif ($class = $definition->getClass()) {
603+
if ($class = $definition->getClass()) {
603604
$class = $this->container->resolveEnvPlaceholders($class);
604605
$return[] = sprintf('@return %s A %s instance', 0 === strpos($class, '%') ? 'object' : '\\'.ltrim($class, '\\'), ltrim($class, '\\'));
605606
} elseif ($definition->getFactory()) {
@@ -680,26 +681,22 @@ private function addService($id, Definition $definition)
680681

681682
$code .= $isProxyCandidate ? $this->getProxyDumper()->getProxyFactoryCode($definition, $id, $methodName) : '';
682683

683-
if ($definition->isSynthetic()) { 8000
684-
$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);
685-
} else {
686-
if ($definition->isDeprecated()) {
687-
$code .= sprintf(" @trigger_error(%s, E_USER_DEPRECATED);\n\n", $this->export($definition->getDeprecationMessage($id)));
688-
}
689-
690-
$code .=
691-
$this->addServiceInclude($id, $definition).
692-
$this->addServiceLocalTempVariables($id, $definition).
693-
$this->addServiceInlinedDefinitions($id, $definition).
694-
$this->addServiceInstance($id, $definition).
695-
$this->addServiceInlinedDefinitionsSetup($id, $definition).
696-
$this->addServiceProperties($id, $definition).
697-
$this->addServiceMethodCalls($id, $definition).
698-
$this->addServiceConfigurator($id, $definition).
699-
$this->addServiceReturn($id, $definition)
700- 6D40
;
684+
if ($definition->isDeprecated()) {
685+
$code .= sprintf(" @trigger_error(%s, E_USER_DEPRECATED);\n\n", $this->export($definition->getDeprecationMessage($id)));
701686
}
702687

688+
$code .=
689+
$this->addServiceInclude($id, $definition).
690+
$this->addServiceLocalTempVariables($id, $definition).
691+
$this->addServiceInlinedDefinitions($id, $definition).
692+
$this->addServiceInstance($id, $definition).
693+
$this->addServiceInlinedDefinitionsSetup($id, $definition).
694+
$this->addServiceProperties($id, $definition).
695+
$this->addServiceMethodCalls($id, $definition).
696+
$this->addServiceConfigurator($id, $definition).
697+
$this->addServiceReturn($id, $definition)
698+
;
699+
703700
$this->definitionVariables = null;
704701
$this->referenceVariables = null;
705702

@@ -952,7 +949,8 @@ private function addNormalizedIds()
952949
*/
953950
private function addMethodMap()
954951
{
955-
if (!$definitions = $this->container->getDefinitions()) {
952+
$definitions = $this->container->getDefinitions();
953+
if (!$definitions || !$definitions = array_filter($definitions, function ($def) { return !$def->isSynthetic(); })) {
956954
return '';
957955
}
958956

src/Symfony/Component/DependencyInjection/Tests/ContainerTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -286,15 +286,15 @@ public function testGetCircularReference()
286286
}
287287

288288
/**
289-
* @expectedException \Symfony\Component\DependencyInjection\Exception\RuntimeException
290-
* @expectedExceptionMessage You have requested a synthetic service ("request"). The DIC does not know how to construct this service.
289+
* @expectedException \Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException
290+
* @expectedExceptionMessage You have requested a non-existent service "request".
291291
*/
292-
public function testGetSyntheticServiceAlwaysThrows()
292+
public function testGetSyntheticServiceThrows()
293293
{
294294
require_once __DIR__.'/Fixtures/php/services9.php';
295295

296296
$container = new \ProjectServiceContainer();
297-
$container->get('request', ContainerInterface::NULL_ON_INVALID_REFERENCE);
297+
$container->get('request');
298298
}
299299

300300
public function testHas()

src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services9.php

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ public function __construct()
5252
'method_call1' => 'getMethodCall1Service',
5353
'new_factory' => 'getNewFactoryService',
5454
'new_factory_service' => 'getNewFactoryServiceService',
55-
'request' => 'getRequestService',
5655
'service_from_static_method' => 'getServiceFromStaticMethodService',
5756
);
5857
$this->privates = array(
@@ -386,19 +385,6 @@ protected function getNewFactoryServiceService()
386385
return $instance;
387386
}
388387

389-
/**
390-
* Gets the 'request' service.
391-
*
392-
* This service is shared.
393-
* This method always returns the same instance of the service.
394-
*
395-
* @throws RuntimeException always since this service is expected to be injected dynamically
396-
*/
397-
protected function getRequestService()
398-
{
399-
throw new RuntimeException('You have requested a synthetic service ("request"). The DIC does not know how to construct this service.');
400-
}
401-
402388
/**
403389
* Gets the 'service_from_static_method' service.
404390
*

src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services9_compiled.php

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ public function __construct()
4848
'lazy_context_ignore_invalid_ref' => 'getLazyContextIgnoreInvalidRefService',
4949
'method_call1' => 'getMethodCall1Service',
5050
'new_factory_service' => 'getNewFactoryServiceService',
51-
'request' => 'getRequestService',
5251
'service_from_static_method' => 'getServiceFromStaticMethodService',
5352
);
5453
$this->aliases = array(
@@ -379,19 +378,6 @@ protected function getNewFactoryServiceService()
379378
return $instance;
380379
}
381380

382-
/**
383-
* Gets the 'request' service.
384-
*
385-
* This service is shared.
386-
* This method always returns the same instance of the service.
387-
*
388-
* @throws RuntimeException always since this service is expected to be injected dynamically
389-
*/
390 691E -
protected function getRequestService()
391-
{
392-
throw new RuntimeException('You have requested a synthetic service ("request"). The DIC does not know how to construct this service.');
393-
}
394-
395381
/**
396382
* Gets the 'service_from_static_method' service.
397383
*

0 commit comments

Comments
 (0)
0