[DI] Move "include_once" out of closure factories
This commit is contained in:
parent
65b2bcde1b
commit
bbcd3d79ff
@ -738,15 +738,6 @@ EOTXT
|
||||
EOF;
|
||||
}
|
||||
|
||||
if ($this->getProxyDumper()->isProxyCandidate($definition)) {
|
||||
$factoryCode = $asFile ? "\$this->load(__DIR__.'/%s.php', false)" : '$this->%s(false)';
|
||||
$code .= $this->getProxyDumper()->getProxyFactoryCode($definition, $id, sprintf($factoryCode, $methodName));
|
||||
}
|
||||
|
||||
if ($definition->isDeprecated()) {
|
||||
$code .= sprintf(" @trigger_error(%s, E_USER_DEPRECATED);\n\n", $this->export($definition->getDeprecationMessage($id)));
|
||||
}
|
||||
|
||||
$inlinedDefinitions = $this->getDefinitionsFromArguments(array($definition));
|
||||
$constructorDefinitions = $this->getDefinitionsFromArguments(array($definition->getArguments(), $definition->getFactory()));
|
||||
$otherDefinitions = new \SplObjectStorage();
|
||||
@ -761,8 +752,18 @@ EOF;
|
||||
|
||||
$isSimpleInstance = !$definition->getProperties() && !$definition->getMethodCalls() && !$definition->getConfigurator();
|
||||
|
||||
$code .= $this->addServiceInclude($id, $definition, $inlinedDefinitions);
|
||||
|
||||
if ($this->getProxyDumper()->isProxyCandidate($definition)) {
|
||||
$factoryCode = $asFile ? "\$this->load(__DIR__.'/%s.php', false)" : '$this->%s(false)';
|
||||
$code .= $this->getProxyDumper()->getProxyFactoryCode($definition, $id, sprintf($factoryCode, $methodName));
|
||||
}
|
||||
|
||||
if ($definition->isDeprecated()) {
|
||||
$code .= sprintf(" @trigger_error(%s, E_USER_DEPRECATED);\n\n", $this->export($definition->getDeprecationMessage($id)));
|
||||
}
|
||||
|
||||
$code .=
|
||||
$this->addServiceInclude($id, $definition, $inlinedDefinitions).
|
||||
$this->addServiceLocalTempVariables($id, $definition, $constructorDefinitions, $inlinedDefinitions).
|
||||
$this->addServiceInlinedDefinitions($id, $definition, $constructorDefinitions, $isSimpleInstance).
|
||||
$this->addServiceInstance($id, $definition, $isSimpleInstance).
|
||||
@ -815,9 +816,16 @@ EOF;
|
||||
$code = $this->addService($id, $definition, $file);
|
||||
|
||||
if (!$definition->isShared()) {
|
||||
$code = implode("\n", array_map(function ($line) { return $line ? ' '.$line : $line; }, explode("\n", $code)));
|
||||
$i = strpos($code, "\n\ninclude_once ");
|
||||
if (false !== $i && false !== $i = strpos($code, "\n\n", 2 + $i)) {
|
||||
$code = array(substr($code, 0, 2 + $i), substr($code, 2 + $i));
|
||||
} else {
|
||||
$code = array("\n", $code);
|
||||
}
|
||||
$code[1] = implode("\n", array_map(function ($line) { return $line ? ' '.$line : $line; }, explode("\n", $code[1])));
|
||||
$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);
|
||||
$code[1] = sprintf("%s = function () {\n%s};\n\nreturn %1\$s();\n", $factory, $code[1]);
|
||||
$code = $code[0].$code[1];
|
||||
}
|
||||
|
||||
yield $file => $code;
|
||||
|
@ -210,6 +210,10 @@ class PhpDumperTest extends TestCase
|
||||
{
|
||||
$container = include self::$fixturesPath.'/containers/container9.php';
|
||||
$container->getDefinition('bar')->addTag('hot');
|
||||
$container->register('non_shared_foo', \Bar\FooClass::class)
|
||||
->setFile(realpath(self::$fixturesPath.'/includes/foo.php'))
|
||||
->setShared(false)
|
||||
->setPublic(true);
|
||||
$container->compile();
|
||||
$dumper = new PhpDumper($container);
|
||||
$dump = print_r($dumper->dump(array('as_files' => true, 'file' => __DIR__, 'hot_path_tag' => 'hot')), true);
|
||||
|
@ -271,6 +271,21 @@ $instance->foo = 'bar';
|
||||
|
||||
return $instance;
|
||||
|
||||
[Container%s/getNonSharedFooService.php] => <?php
|
||||
|
||||
use Symfony\Component\DependencyInjection\Argument\RewindableGenerator;
|
||||
|
||||
// This file has been auto-generated by the Symfony Dependency Injection Component for internal use.
|
||||
// Returns the public 'non_shared_foo' service.
|
||||
|
||||
include_once ($this->targetDirs[0].'/Fixtures/includes/foo.php');
|
||||
|
||||
$this->factories['non_shared_foo'] = function () {
|
||||
return new \Bar\FooClass();
|
||||
};
|
||||
|
||||
return $this->factories['non_shared_foo']();
|
||||
|
||||
[Container%s/getServiceFromStaticMethodService.php] => <?php
|
||||
|
||||
use Symfony\Component\DependencyInjection\Argument\RewindableGenerator;
|
||||
@ -366,6 +381,7 @@ class ProjectServiceContainer extends Container
|
||||
'lazy_context_ignore_invalid_ref' => __DIR__.'/getLazyContextIgnoreInvalidRefService.php',
|
||||
'method_call1' => __DIR__.'/getMethodCall1Service.php',
|
||||
'new_factory_service' => __DIR__.'/getNewFactoryServiceService.php',
|
||||
'non_shared_foo' => __DIR__.'/getNonSharedFooService.php',
|
||||
'service_from_static_method' => __DIR__.'/getServiceFromStaticMethodService.php',
|
||||
'tagged_iterator' => __DIR__.'/getTaggedIteratorService.php',
|
||||
);
|
||||
|
Reference in New Issue
Block a user