[DI] Put non-shared service factories in closures
This commit is contained in:
parent
8cf8d1619f
commit
22c5325ca5
@ -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();
|
||||
@ -220,6 +221,9 @@ class Container implements ResettableContainerInterface
|
||||
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));
|
||||
@ -289,7 +293,7 @@ class Container implements ResettableContainerInterface
|
||||
*/
|
||||
public function reset()
|
||||
{
|
||||
$this->services = array();
|
||||
$this->services = $this->factories = array();
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -711,7 +711,7 @@ EOTXT
|
||||
$lazyInitialization = '';
|
||||
}
|
||||
|
||||
$asFile = $this->asFiles && $definition->isShared() && !$this->isHotPath($definition);
|
||||
$asFile = $this->asFiles && !$this->isHotPath($definition);
|
||||
$methodName = $this->generateMethodName($id);
|
||||
if ($asFile) {
|
||||
$file = $methodName.'.php';
|
||||
@ -785,7 +785,7 @@ EOF;
|
||||
$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()) {
|
||||
@ -803,8 +803,15 @@ EOF;
|
||||
$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;
|
||||
}
|
||||
}
|
||||
@ -1036,7 +1043,7 @@ EOF;
|
||||
$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";
|
||||
}
|
||||
}
|
||||
@ -1050,7 +1057,7 @@ EOF;
|
||||
$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));
|
||||
}
|
||||
}
|
||||
@ -1621,8 +1628,12 @@ EOF;
|
||||
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));
|
||||
}
|
||||
|
@ -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;
|
||||
@ -331,7 +345,6 @@ class ProjectServiceContainer extends Container
|
||||
);
|
||||
$this->methodMap = array(
|
||||
'bar' => 'getBarService',
|
||||
'foo_bar' => 'getFooBarService',
|
||||
);
|
||||
$this->fileMap = array(
|
||||
'BAR' => __DIR__.'/getBAR2Service.php',
|
||||
@ -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',
|
||||
@ -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;
|
||||
|
Reference in New Issue
Block a user