Skip to content

Commit

Permalink
bug #26148 [DI] Move "include_once" out of closure factories (nicolas…
Browse files Browse the repository at this point in the history
…-grekas)

This PR was merged into the 4.1-dev branch.

Discussion
----------

[DI] Move "include_once" out of closure factories

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | -
| License       | MIT
| Doc PR        | -

They were previously dumped inside the closures.

Commits
-------

bbcd3d7 [DI] Move "include_once" out of closure factories
  • Loading branch information
nicolas-grekas committed Feb 12, 2018
2 parents 65b2bcd + bbcd3d7 commit a9f2a09
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 12 deletions.
32 changes: 20 additions & 12 deletions src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php
Expand Up @@ -738,15 +738,6 @@ protected function {$methodName}($lazyInitialization)
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();
Expand All @@ -761,8 +752,18 @@ protected function {$methodName}($lazyInitialization)

$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).
Expand Down Expand Up @@ -815,9 +816,16 @@ private function generateServiceFiles()
$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;
Expand Down
Expand Up @@ -210,6 +210,10 @@ public function testDumpAsFiles()
{
$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);
Expand Down
Expand Up @@ -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;
Expand Down Expand Up @@ -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',
);
Expand Down

0 comments on commit a9f2a09

Please sign in to comment.