8000 [DI] Remove synthetic services from methodMap + generated methods · symfony/symfony@c1e1e99 · GitHub
[go: up one dir, main page]

Skip to content

Commit c1e1e99

Browse files
[DI] Remove synthetic services from methodMap + generated methods
1 parent 7aeb31e commit c1e1e99

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()) {
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-
;
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

@@ -950,7 +947,8 @@ private function addNormalizedIds()
950947
*/
951948
private function addMethodMap()
952949
{
953-
if (!$definitions = $this->container->getDefinitions()) {
950+
$definitions = $this->container->getDefinitions();
951+
if (!$definitions || !$definitions = array_filter($definitions, function ($def) { return !$def->isSynthetic(); })) {
954952
return '';
955953
}
956954

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

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

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

295295
$container = new \ProjectServiceContainer();
296-
$container->get('request', ContainerInterface::NULL_ON_INVALID_REFERENCE);
296+
$container->get('request');
297297
}
298298

299299
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
@@ -50,7 +50,6 @@ public function __construct()
5050
'method_call1' => 'getMethodCall1Service',
5151
'new_factory' => 'getNewFactoryService',
5252
'new_factory_service' => 'getNewFactoryServiceService',
53-
'request' => 'getRequestService',
5453
'service_from_static_method' => 'getServiceFromStaticMethodService',
5554
);
5655
$this->privates = array(
@@ -384,19 +383,6 @@ protected function getNewFactoryServiceService()
384383
return $instance;
385384
}
386385

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

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

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ public function __construct()
4646
'lazy_context_ignore_invalid_ref' => 'getLazyContextIgnoreInvalidRefService',
4747
'method_call1' => 'getMethodCall1Service',
4848
'new_factory_service' => 'getNewFactoryServiceService',
49-
'request' => 'getRequestService',
5049
'service_from_static_method' => 'getServiceFromStaticMethodService',
5150
);
5251
$this->aliases = array(
@@ -377,19 +376,6 @@ protected function getNewFactoryServiceService()
377376
return $instance;
378377
}
379378

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

0 commit comments

Comments
 (0)
0