Skip to content

[DI] Remove synthetic services from methodMap + generated methods #21244

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 15, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions src/Symfony/Component/DependencyInjection/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
42 changes: 20 additions & 22 deletions src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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 '';
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We go from \RuntimeException to \InvalidArgumentException now.. minor BC break :)

👍 for master, as it's needed to keep things simple.

* @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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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.
*
Expand Down