Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a9f2a09

Browse files
bug #26148 [DI] Move "include_once" out of closure factories (nicolas-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
2 parents 65b2bcd + bbcd3d7 commit a9f2a09

File tree

3 files changed

+40
-12
lines changed

3 files changed

+40
-12
lines changed

src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -738,15 +738,6 @@ protected function {$methodName}($lazyInitialization)
738738
EOF;
739739
}
740740

741-
if ($this->getProxyDumper()->isProxyCandidate($definition)) {
742-
$factoryCode = $asFile ? "\$this->load(__DIR__.'/%s.php', false)" : '$this->%s(false)';
743-
$code .= $this->getProxyDumper()->getProxyFactoryCode($definition, $id, sprintf($factoryCode, $methodName));
744-
}
745-
746-
if ($definition->isDeprecated()) {
747-
$code .= sprintf(" @trigger_error(%s, E_USER_DEPRECATED);\n\n", $this->export($definition->getDeprecationMessage($id)));
748-
}
749-
750741
$inlinedDefinitions = $this->getDefinitionsFromArguments(array($definition));
751742
$constructorDefinitions = $this->getDefinitionsFromArguments(array($definition->getArguments(), $definition->getFactory()));
752743
$otherDefinitions = new \SplObjectStorage();
@@ -761,8 +752,18 @@ protected function {$methodName}($lazyInitialization)
761752

762753
$isSimpleInstance = !$definition->getProperties() && !$definition->getMethodCalls() && !$definition->getConfigurator();
763754

755+
$code .= $this->addServiceInclude($id, $definition, $inlinedDefinitions);
756+
757+
if ($this->getProxyDumper()->isProxyCandidate($definition)) {
758+
$factoryCode = $asFile ? "\$this->load(__DIR__.'/%s.php', false)" : '$this->%s(false)';
759+
$code .= $this->getProxyDumper()->getProxyFactoryCode($definition, $id, sprintf($factoryCode, $methodName));
760+
}
761+
762+
if ($definition->isDeprecated()) {
763+
$code .= sprintf(" @trigger_error(%s, E_USER_DEPRECATED);\n\n", $this->export($definition->getDeprecationMessage($id)));
764+
}
765+
764766
$code .=
765-
$this->addServiceInclude($id, $definition, $inlinedDefinitions).
766767
$this->addServiceLocalTempVariables($id, $definition, $constructorDefinitions, $inlinedDefinitions).
767768
$this->addServiceInlinedDefinitions($id, $definition, $constructorDefinitions, $isSimpleInstance).
768769
$this->addServiceInstance($id, $definition, $isSimpleInstance).
@@ -815,9 +816,16 @@ private function generateServiceFiles()
815816
$code = $this->addService($id, $definition, $file);
816817

817818
if (!$definition->isShared()) {
818-
$code = implode("\n", array_map(function ($line) { return $line ? ' '.$line : $line; }, explode("\n", $code)));
819+
$i = strpos($code, "\n\ninclude_once ");
820+
if (false !== $i && false !== $i = strpos($code, "\n\n", 2 + $i)) {
821+
$code = array(substr($code, 0, 2 + $i), substr($code, 2 + $i));
822+
} else {
823+
$code = array("\n", $code);
824+
}
825+
$code[1] = implode("\n", array_map(function ($line) { return $line ? ' '.$line : $line; }, explode("\n", $code[1])));
819826
$factory = sprintf('$this->factories%s[\'%s\']', $definition->isPublic() ? '' : "['service_container']", $id);
820-
$code = sprintf("\n%s = function () {\n%s};\n\nreturn %1\$s();\n", $factory, $code);
827+
$code[1] = sprintf("%s = function () {\n%s};\n\nreturn %1\$s();\n", $factory, $code[1]);
828+
$code = $code[0].$code[1];
821829
}
822830

823831
yield $file => $code;

src/Symfony/Component/DependencyInjection/Tests/Dumper/PhpDumperTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,10 @@ public function testDumpAsFiles()
210210
{
211211
$container = include self::$fixturesPath.'/containers/container9.php';
212212
$container->getDefinition('bar')->addTag('hot');
213+
$container->register('non_shared_foo', \Bar\FooClass::class)
214+
->setFile(realpath(self::$fixturesPath.'/includes/foo.php'))
215+
->setShared(false)
216+
->setPublic(true);
213217
$container->compile();
214218
$dumper = new PhpDumper($container);
215219
$dump = print_r($dumper->dump(array('as_files' => true, 'file' => __DIR__, 'hot_path_tag' => 'hot')), true);

src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services9_as_files.txt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,21 @@ $instance->foo = 'bar';
271271

272272
return $instance;
273273

274+
[Container%s/getNonSharedFooService.php] => <?php
275+
276+
use Symfony\Component\DependencyInjection\Argument\RewindableGenerator;
277+
278+
// This file has been auto-generated by the Symfony Dependency Injection Component for internal use.
279+
// Returns the public 'non_shared_foo' service.
280+
281+
include_once ($this->targetDirs[0].'/Fixtures/includes/foo.php');
282+
283+
$this->factories['non_shared_foo'] = function () {
284+
return new \Bar\FooClass();
285+
};
286+
287+
return $this->factories['non_shared_foo']();
288+
274289
[Container%s/getServiceFromStaticMethodService.php] => <?php
275290

276291
use Symfony\Component\DependencyInjection\Argument\RewindableGenerator;
@@ -366,6 +381,7 @@ class ProjectServiceContainer extends Container
366381
'lazy_context_ignore_invalid_ref' => __DIR__.'/getLazyContextIgnoreInvalidRefService.php',
367382
'method_call1' => __DIR__.'/getMethodCall1Service.php',
368383
'new_factory_service' => __DIR__.'/getNewFactoryServiceService.php',
384+
'non_shared_foo' => __DIR__.'/getNonSharedFooService.php',
369385
'service_from_static_method' => __DIR__.'/getServiceFromStaticMethodService.php',
370386
'tagged_iterator' => __DIR__.'/getTaggedIteratorService.php',
371387
);

0 commit comments

Comments
 (0)