Skip to content

Commit 1b2a3f8

Browse files
[Console][DI] Fail gracefully
1 parent 36777bb commit 1b2a3f8

File tree

5 files changed

+29
-21
lines changed

5 files changed

+29
-21
lines changed

Dumper/PhpDumper.php

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -217,12 +217,16 @@ public function dump(array $options = array())
217217
{$namespaceLine}
218218
// This file has been auto-generated by the Symfony Dependency Injection Component for internal use.
219219
220-
if (!class_exists(\\Container{$hash}\\{$options['class']}::class, false)) {
221-
require __DIR__.'/Container{$hash}/{$options['class']}.php';
220+
if (\\class_exists(\\Container{$hash}\\{$options['class']}::class, false)) {
221+
// no-op
222+
} elseif (!include __DIR__.'/Container{$hash}/{$options['class']}.php') {
223+
touch(__DIR__.'/Container{$hash}.legacy');
224+
225+
return;
222226
}
223227
224-
if (!class_exists({$options['class']}::class, false)) {
225-
class_alias(\\Container{$hash}\\{$options['class']}::class, {$options['class']}::class, false);
228+
if (!\\class_exists({$options['class']}::class, false)) {
229+
\\class_alias(\\Container{$hash}\\{$options['class']}::class, {$options['class']}::class, false);
226230
}
227231
228232
return new \\Container{$hash}\\{$options['class']}();
@@ -428,13 +432,13 @@ private function addServiceInclude($cId, Definition $definition, \SplObjectStora
428432
}
429433

430434
foreach (array_diff_key(array_flip($lineage), $this->inlinedRequires) as $file => $class) {
431-
$code .= sprintf(" require_once %s;\n", $file);
435+
$code .= sprintf(" include_once %s;\n", $file);
432436
}
433437
}
434438

435439
foreach ($inlinedDefinitions as $def) {
436440
if ($file = $def->getFile()) {
437-
$code .= sprintf(" require_once %s;\n", $this->dumpValue($file));
441+
$code .= sprintf(" include_once %s;\n", $this->dumpValue($file));
438442
}
439443
}
440444

@@ -1233,7 +1237,7 @@ private function addInlineRequires()
12331237
foreach ($lineage as $file) {
12341238
if (!isset($this->inlinedRequires[$file])) {
12351239
$this->inlinedRequires[$file] = true;
1236-
$code .= sprintf(" require_once %s;\n", $file);
1240+
$code .= sprintf(" include_once %s;\n", $file);
12371241
}
12381242
}
12391243

Tests/Fixtures/php/services9.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ protected function getLazyContextIgnoreInvalidRefService()
297297
*/
298298
protected function getMethodCall1Service()
299299
{
300-
require_once '%path%foo.php';
300+
include_once '%path%foo.php';
301301

302302
$this->services['method_call1'] = $instance = new \Bar\FooClass();
303303

Tests/Fixtures/php/services9_as_files.txt

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ use Symfony\Component\DependencyInjection\Argument\RewindableGenerator;
198198
// This file has been auto-generated by the Symfony Dependency Injection Component for internal use.
199199
// Returns the public 'method_call1' shared service.
200200

201-
require_once ($this->targetDirs[0].'/Fixtures/includes/foo.php');
201+
include_once ($this->targetDirs[0].'/Fixtures/includes/foo.php');
202202

203203
$this->services['method_call1'] = $instance = new \Bar\FooClass();
204204

@@ -471,12 +471,16 @@ class ProjectServiceContainer extends Container
471471

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

474-
if (!class_exists(\Container%s\ProjectServiceContainer::class, false)) {
475-
require __DIR__.'/Container%s/ProjectServiceContainer.php';
474+
if (\class_exists(\Container%s\ProjectServiceContainer::class, false)) {
475+
// no-op
476+
} elseif (!include __DIR__.'/Container%s/ProjectServiceContainer.php') {
477+
touch(__DIR__.'/Container%s.legacy');
478+
479+
return;
476480
}
477481

478-
if (!class_exists(ProjectServiceContainer::class, false)) {
479-
class_alias(\Container%s\ProjectServiceContainer::class, ProjectServiceContainer::class, false);
482+
if (!\class_exists(ProjectServiceContainer::class, false)) {
483+
\class_alias(\Container%s\ProjectServiceContainer::class, ProjectServiceContainer::class, false);
480484
}
481485

482486
return new \Container%s\ProjectServiceContainer();

Tests/Fixtures/php/services9_compiled.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ protected function getLazyContextIgnoreInvalidRefService()
307307
*/
308308
protected function getMethodCall1Service()
309309
{
310-
require_once '%path%foo.php';
310+
include_once '%path%foo.php';
311311

312312
$this->services['method_call1'] = $instance = new \Bar\FooClass();
313313

Tests/Fixtures/php/services_inline_requires.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,10 @@ public function __construct()
4646

4747
$this->aliases = array();
4848

49-
require_once $this->targetDirs[1].'/includes/HotPath/I1.php';
50-
require_once $this->targetDirs[1].'/includes/HotPath/P1.php';
51-
require_once $this->targetDirs[1].'/includes/HotPath/T1.php';
52-
require_once $this->targetDirs[1].'/includes/HotPath/C1.php';
49+
include_once $this->targetDirs[1].'/includes/HotPath/I1.php';
50+
include_once $this->targetDirs[1].'/includes/HotPath/P1.php';
51+
include_once $this->targetDirs[1].'/includes/HotPath/T1.php';
52+
include_once $this->targetDirs[1].'/includes/HotPath/C1.php';
5353
}
5454

5555
public function getRemovedIds()
@@ -104,8 +104,8 @@ protected function getC1Service()
104104
*/
105105
protected function getC2Service()
106106
{
107-
require_once $this->targetDirs[1].'/includes/HotPath/C2.php';
108-
require_once $this->targetDirs[1].'/includes/HotPath/C3.php';
107+
include_once $this->targetDirs[1].'/includes/HotPath/C2.php';
108+
include_once $this->targetDirs[1].'/includes/HotPath/C3.php';
109109

110110
return $this->services['Symfony\Component\DependencyInjection\Tests\Fixtures\includes\HotPath\C2'] = new \Symfony\Component\DependencyInjection\Tests\Fixtures\includes\HotPath\C2(${($_ = isset($this->services['Symfony\Component\DependencyInjection\Tests\Fixtures\includes\HotPath\C3']) ? $this->services['Symfony\Component\DependencyInjection\Tests\Fixtures\includes\HotPath\C3'] : $this->services['Symfony\Component\DependencyInjection\Tests\Fixtures\includes\HotPath\C3'] = new \Symfony\Component\DependencyInjection\Tests\Fixtures\includes\HotPath\C3()) && false ?: '_'});
111111
}
@@ -117,7 +117,7 @@ protected function getC2Service()
117117
*/
118118
protected function getC3Service()
119119
{
120-
require_once $this->targetDirs[1].'/includes/HotPath/C3.php';
120+
include_once $this->targetDirs[1].'/includes/HotPath/C3.php';
121121

122122
return $this->services['Symfony\Component\DependencyInjection\Tests\Fixtures\includes\HotPath\C3'] = new \Symfony\Component\DependencyInjection\Tests\Fixtures\includes\HotPath\C3();
123123
}

0 commit comments

Comments
 (0)