Skip to content

[DI] Put non-shared service factories in closures #25867

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
Feb 4, 2018
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
6 changes: 5 additions & 1 deletion src/Symfony/Component/DependencyInjection/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ class Container implements ResettableContainerInterface
protected $services = array();
protected $fileMap = array();
protected $methodMap = array();
protected $factories = array();
protected $aliases = array();
protected $loading = array();
protected $resolving = array();
Expand Down Expand Up @@ -220,6 +221,9 @@ public function get($id, $invalidBehavior = /* self::EXCEPTION_ON_INVALID_REFERE
if ('service_container' === $id) {
return $this;
}
if (isset($this->factories[$id])) {
return $this->factories[$id]();
}

if (isset($this->loading[$id])) {
throw new ServiceCircularReferenceException($id, array_keys($this->loading));
Expand Down Expand Up @@ -289,7 +293,7 @@ public function initialized($id)
*/
public function reset()
{
$this->services = array();
$this->services = $this->factories = array();
}

/**
Expand Down
23 changes: 17 additions & 6 deletions src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php
Original file line number Diff line number Diff line change
Expand Up @@ -711,7 +711,7 @@ private function addService(string $id, Definition $definition, string &$file =
$lazyInitialization = '';
}

$asFile = $this->asFiles && $definition->isShared() && !$this->isHotPath($definition);
$asFile = $this->asFiles && !$this->isHotPath($definition);
$methodName = $this->generateMethodName($id);
if ($asFile) {
$file = $methodName.'.php';
Expand Down Expand Up @@ -785,7 +785,7 @@ private function addServices(): string
$definitions = $this->container->getDefinitions();
ksort($definitions);
foreach ($definitions as $id => $definition) {
if ($definition->isSynthetic() || ($this->asFiles && $definition->isShared() && !$this->isHotPath($definition))) {
if ($definition->isSynthetic() || ($this->asFiles && !$this->isHotPath($definition))) {
continue;
}
if ($definition->isPublic()) {
Expand All @@ -803,8 +803,15 @@ private function generateServiceFiles()
$definitions = $this->container->getDefinitions();
ksort($definitions);
foreach ($definitions as $id => $definition) {
if (!$definition->isSynthetic() && $definition->isShared() && !$this->isHotPath($definition)) {
if (!$definition->isSynthetic() && !$this->isHotPath($definition)) {
$code = $this->addService($id, $definition, $file);

if (!$definition->isShared()) {
$code = implode("\n", array_map(function ($line) { return $line ? ' '.$line : $line; }, explode("\n", $code)));
$factory = sprintf('$this->factories%s[\'%s\']', $definition->isPublic() ? '' : "['service_container']", $id);
$code = sprintf("\n%s = function () {\n%s};\n\nreturn %1\$s();\n", $factory, $code);
}

yield $file => $code;
}
}
Expand Down Expand Up @@ -1036,7 +1043,7 @@ private function addMethodMap(): string
$definitions = $this->container->getDefinitions();
ksort($definitions);
foreach ($definitions as $id => $definition) {
if (!$definition->isSynthetic() && $definition->isPublic() && (!$this->asFiles || !$definition->isShared() || $this->isHotPath($definition))) {
if (!$definition->isSynthetic() && $definition->isPublic() && (!$this->asFiles || $this->isHotPath($definition))) {
$code .= ' '.$this->doExport($id).' => '.$this->doExport($this->generateMethodName($id)).",\n";
}
}
Expand All @@ -1050,7 +1057,7 @@ private function addFileMap(): string
$definitions = $this->container->getDefinitions();
ksort($definitions);
foreach ($definitions as $id => $definition) {
if (!$definition->isSynthetic() && $definition->isPublic() && $definition->isShared() && !$this->isHotPath($definition)) {
if (!$definition->isSynthetic() && $definition->isPublic() && !$this->isHotPath($definition)) {
$code .= sprintf(" %s => __DIR__.'/%s.php',\n", $this->doExport($id), $this->generateMethodName($id));
}
}
Expand Down Expand Up @@ -1621,8 +1628,12 @@ private function getServiceCall(string $id, Reference $reference = null): string
if ($definition->isShared()) {
$code = sprintf('$this->%s[\'%s\'] = %s', $definition->isPublic() ? 'services' : 'privates', $id, $code);
}
} elseif ($this->asFiles && $definition->isShared() && !$this->isHotPath($definition)) {
} elseif ($this->asFiles && !$this->isHotPath($definition)) {
$code = sprintf("\$this->load(__DIR__.'/%s.php')", $this->generateMethodName($id));
if (!$definition->isShared()) {
$factory = sprintf('$this->factories%s[\'%s\']', $definition->isPublic() ? '' : "['service_container']", $id);
$code = sprintf('(isset(%s) ? %1$s() : %s)', $factory, $code);
}
} else {
$code = sprintf('$this->%s()', $this->generateMethodName($id));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,20 @@ $this->services['foo.baz'] = $instance = \BazClass::getInstance();

return $instance;

[Container%s/getFooBarService.php] => <?php

use Symfony\Component\DependencyInjection\Argument\RewindableGenerator;

// This file has been auto-generated by the Symfony Dependency Injection Component for internal use.

$this->factories['foo_bar'] = function () {
// Returns the public 'foo_bar' service.

return new \Bar\FooClass(($this->services['deprecated_service'] ?? $this->load(__DIR__.'/getDeprecatedServiceService.php')));
};

return $this->factories['foo_bar']();

[Container%s/getFooWithInlineService.php] => <?php

use Symfony\Component\DependencyInjection\Argument\RewindableGenerator;
Expand Down Expand Up @@ -331,7 +345,6 @@ class ProjectServiceContainer extends Container
);
$this->methodMap = array(
'bar' => 'getBarService',
'foo_bar' => 'getFooBarService',
);
$this->fileMap = array(
'BAR' => __DIR__.'/getBAR2Service.php',
Expand All @@ -347,6 +360,7 @@ class ProjectServiceContainer extends Container
'factory_service_simple' => __DIR__.'/getFactoryServiceSimpleService.php',
'foo' => __DIR__.'/getFooService.php',
'foo.baz' => __DIR__.'/getFoo_BazService.php',
'foo_bar' => __DIR__.'/getFooBarService.php',
'foo_with_inline' => __DIR__.'/getFooWithInlineService.php',
'lazy_context' => __DIR__.'/getLazyContextService.php',
'lazy_context_ignore_invalid_ref' => __DIR__.'/getLazyContextIgnoreInvalidRefService.php',
Expand Down Expand Up @@ -404,16 +418,6 @@ class ProjectServiceContainer extends Container
return $instance;
}

/**
* Gets the public 'foo_bar' service.
*
* @return \Bar\FooClass
*/
protected function getFooBarService()
{
return new \Bar\FooClass(($this->services['deprecated_service'] ?? $this->load(__DIR__.'/getDeprecatedServiceService.php')));
}

public function getParameter($name)
{
$name = (string) $name;
Expand Down