Skip to content

Commit a0dc65f

Browse files
Merge branch '4.0'
* 4.0: [DI] fix param name cast Remove randomness from dumped containers fixed messages to be explicit about the package needed to be installed [FrameworkBundle] Fix recommended composer command (add vendor) [WebProfilerBundle] set the var in the right scope [TwigBundle] fix lowest dep [HttpKernel] Disable CSP header on exception pages Use the default host even if context is empty and fallback to relative URL if empty host Proposing Flex-specific error messages in the controller shortcuts
2 parents 462fa21 + 67bf5e4 commit a0dc65f

File tree

4 files changed

+57
-12
lines changed

4 files changed

+57
-12
lines changed

ContainerBuilder.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1190,6 +1190,8 @@ private function doResolveServices($value, array &$inlineServices = array())
11901190
$value = $this->doGet((string) $value, $value->getInvalidBehavior(), $inlineServices);
11911191
} elseif ($value instanceof Definition) {
11921192
$value = $this->createService($value, $inlineServices);
1193+
} elseif ($value instanceof Parameter) {
1194+
$value = $this->getParameter((string) $value);
11931195
} elseif ($value instanceof Expression) {
11941196
$value = $this->getExpressionLanguage()->evaluate($value, array('container' => $this));
11951197
}

Dumper/PhpDumper.php

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,8 @@ public function dump(array $options = array())
218218
array_pop($code);
219219
$code["Container{$hash}/{$options['class']}.php"] = substr_replace($files[$options['class'].'.php'], "<?php\n\nnamespace Container{$hash};\n", 0, 6);
220220
$namespaceLine = $this->namespace ? "\nnamespace {$this->namespace};\n" : '';
221+
$time = time();
222+
$id = hash('crc32', $hash.$time);
221223

222224
$code[$options['class'].'.php'] = <<<EOF
223225
<?php
@@ -236,7 +238,11 @@ public function dump(array $options = array())
236238
\\class_alias(\\Container{$hash}\\{$options['class']}::class, {$options['class']}::class, false);
237239
}
238240
239-
return new \\Container{$hash}\\{$options['class']}();
241+
return new \\Container{$hash}\\{$options['class']}(array(
242+
'container.build_hash' => '$hash',
243+
'container.build_id' => '$id',
244+
'container.build_time' => $time,
245+
));
240246
241247
EOF;
242248
} else {
@@ -543,15 +549,15 @@ private function isTrivialInstance(Definition $definition): bool
543549
}
544550

545551
foreach ($definition->getArguments() as $arg) {
546-
if (!$arg) {
552+
if (!$arg || $arg instanceof Parameter) {
547553
continue;
548554
}
549555
if (is_array($arg) && 3 >= count($arg)) {
550556
foreach ($arg as $k => $v) {
551557
if ($this->dumpValue($k) !== $this->dumpValue($k, false)) {
552558
return false;
553559
}
554-
if (!$v) {
560+
if (!$v || $v instanceof Parameter) {
555561
continue;
556562
}
557563
if ($v instanceof Reference && $this->container->has($id = (string) $v) && $this->container->findDefinition($id)->isSynthetic()) {
@@ -837,10 +843,10 @@ private function addNewInstance(Definition $definition, $return, $instantiation,
837843
}
838844

839845
if (0 === strpos($class, 'new ')) {
840-
return $return.sprintf("(%s)->%s(%s);\n", $this->dumpValue($callable[0]), $callable[1], $arguments ? implode(', ', $arguments) : '');
846+
return $return.sprintf("(%s)->%s(%s);\n", $class, $callable[1], $arguments ? implode(', ', $arguments) : '');
841847
}
842848

843-
return $return.sprintf("[%s, '%s'](%s);\n", $this->dumpValue($callable[0]), $callable[1], $arguments ? implode(', ', $arguments) : '');
849+
return $return.sprintf("[%s, '%s'](%s);\n", $class, $callable[1], $arguments ? implode(', ', $arguments) : '');
844850
}
845851

846852
return $return.sprintf("%s(%s);\n", $this->dumpLiteralClass($this->dumpValue($callable)), $arguments ? implode(', ', $arguments) : '');
@@ -898,6 +904,11 @@ public function __construct()
898904
899905
EOF;
900906
}
907+
if ($this->asFiles) {
908+
$code = str_replace('$parameters', "\$buildParameters;\n private \$parameters", $code);
909+
$code = str_replace('__construct()', '__construct(array $buildParameters = array())', $code);
910+
$code .= " \$this->buildParameters = \$buildParameters;\n";
911+
}
901912

902913
if ($this->container->getParameterBag()->all()) {
903914
$code .= " \$this->parameters = \$this->getDefaultParameters();\n\n";
@@ -1126,6 +1137,9 @@ private function addDefaultParametersMethod(): string
11261137
public function getParameter($name)
11271138
{
11281139
$name = (string) $name;
1140+
if (isset($this->buildParameters[$name])) {
1141+
return $this->buildParameters[$name];
1142+
}
11291143
11301144
if (!(isset($this->parameters[$name]) || isset($this->loadedDynamicParameters[$name]) || array_key_exists($name, $this->parameters))) {
11311145
throw new InvalidArgumentException(sprintf('The parameter "%s" must be defined.', $name));
@@ -1140,6 +1154,9 @@ public function getParameter($name)
11401154
public function hasParameter($name)
11411155
{
11421156
$name = (string) $name;
1157+
if (isset($this->buildParameters[$name])) {
1158+
return true;
1159+
}
11431160
11441161
return isset($this->parameters[$name]) || isset($this->loadedDynamicParameters[$name]) || array_key_exists($name, $this->parameters);
11451162
}
@@ -1156,13 +1173,19 @@ public function getParameterBag()
11561173
foreach ($this->loadedDynamicParameters as $name => $loaded) {
11571174
$parameters[$name] = $loaded ? $this->dynamicParameters[$name] : $this->getDynamicParameter($name);
11581175
}
1176+
foreach ($this->buildParameters as $name => $value) {
1177+
$parameters[$name] = $value;
1178+
}
11591179
$this->parameterBag = new FrozenParameterBag($parameters);
11601180
}
11611181
11621182
return $this->parameterBag;
11631183
}
11641184

11651185
EOF;
1186+
if (!$this->asFiles) {
1187+
$code = preg_replace('/^.*buildParameters.*\n.*\n.*\n/m', '', $code);
1188+
}
11661189

11671190
if ($dynamicPhp) {
11681191
$loadedDynamicParameters = $this->exportParameters(array_combine(array_keys($dynamicPhp), array_fill(0, count($dynamicPhp), false)), '', 8);
@@ -1483,16 +1506,21 @@ private function dumpValue($value, bool $interpolate = true): string
14831506
throw new RuntimeException(sprintf('Cannot dump definition because of invalid factory method (%s)', $factory[1] ?: 'n/a'));
14841507
}
14851508

1509+
$class = $this->dumpValue($factory[0]);
14861510
if (is_string($factory[0])) {
1487-
return sprintf('%s::%s(%s)', $this->dumpLiteralClass($this->dumpValue($factory[0])), $factory[1], implode(', ', $arguments));
1511+
return sprintf('%s::%s(%s)', $this->dumpLiteralClass($class), $factory[1], implode(', ', $arguments));
14881512
}
14891513

14901514
if ($factory[0] instanceof Definition) {
1491-
return sprintf("[%s, '%s'](%s)", $this->dumpValue($factory[0]), $factory[1], implode(', ', $arguments));
1515+
if (0 === strpos($class, 'new ')) {
1516+
return sprintf('(%s)->%s(%s)', $class, $factory[1], implode(', ', $arguments));
1517+
}
1518+
1519+
return sprintf("[%s, '%s'](%s)", $class, $factory[1], implode(', ', $arguments));
14921520
}
14931521

14941522
if ($factory[0] instanceof Reference) {
1495-
return sprintf('%s->%s(%s)', $this->dumpValue($factory[0]), $factory[1], implode(', ', $arguments));
1523+
return sprintf('%s->%s(%s)', $class, $factory[1], implode(', ', $arguments));
14961524
}
14971525
}
14981526

Tests/Fixtures/php/services9_as_files.txt

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,7 @@ use Symfony\Component\DependencyInjection\ParameterBag\FrozenParameterBag;
307307
*/
308308
class ProjectServiceContainer extends Container
309309
{
310+
private $buildParameters;
310311
private $parameters;
311312
private $targetDirs = array();
312313

@@ -315,12 +316,13 @@ class ProjectServiceContainer extends Container
315316
*/
316317
protected $privates = array();
317318

318-
public function __construct()
319+
public function __construct(array $buildParameters = array())
319320
{
320321
$dir = $this->targetDirs[0] = \dirname(__DIR__);
321322
for ($i = 1; $i <= 5; ++$i) {
322323
$this->targetDirs[$i] = $dir = \dirname($dir);
323324
}
325+
$this->buildParameters = $buildParameters;
324326
$this->parameters = $this->getDefaultParameters();
325327

326328
$this->services = $this->privates = array();
@@ -415,6 +417,9 @@ class ProjectServiceContainer extends Container
415417
public function getParameter($name)
416418
{
417419
$name = (string) $name;
420+
if (isset($this->buildParameters[$name])) {
421+
return $this->buildParameters[$name];
422+
}
418423

419424
if (!(isset($this->parameters[$name]) || isset($this->loadedDynamicParameters[$name]) || array_key_exists($name, $this->parameters))) {
420425
throw new InvalidArgumentException(sprintf('The parameter "%s" must be defined.', $name));
@@ -429,6 +434,9 @@ class ProjectServiceContainer extends Container
429434
public function hasParameter($name)
430435
{
431436
$name = (string) $name;
437+
if (isset($this->buildParameters[$name])) {
438+
return true;
439+
}
432440

433441
return isset($this->parameters[$name]) || isset($this->loadedDynamicParameters[$name]) || array_key_exists($name, $this->parameters);
434442
}
@@ -445,6 +453,9 @@ class ProjectServiceContainer extends Container
445453
foreach ($this->loadedDynamicParameters as $name => $loaded) {
446454
$parameters[$name] = $loaded ? $this->dynamicParameters[$name] : $this->getDynamicParameter($name);
447455
}
456+
foreach ($this->buildParameters as $name => $value) {
457+
$parameters[$name] = $value;
458+
}
448459
$this->parameterBag = new FrozenParameterBag($parameters);
449460
}
450461

@@ -499,6 +510,10 @@ if (!\class_exists(ProjectServiceContainer::class, false)) {
499510
\class_alias(\Container%s\ProjectServiceContainer::class, ProjectServiceContainer::class, false);
500511
}
501512

502-
return new \Container%s\ProjectServiceContainer();
513+
return new \Container%s\ProjectServiceContainer(array(
514+
'container.build_hash' => '%s',
515+
'container.build_id' => '%s',
516+
'container.build_time' => %d,
517+
));
503518

504519
)

Tests/Fixtures/php/services_subscriber.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,14 +79,14 @@ protected function getTestServiceSubscriberService()
7979
*/
8080
protected function getFooServiceService()
8181
{
82-
return $this->services['foo_service'] = new \Symfony\Component\DependencyInjection\Tests\Fixtures\TestServiceSubscriber([new \Symfony\Component\DependencyInjection\ServiceLocator(array('Symfony\\Component\\DependencyInjection\\Tests\\Fixtures\\CustomDefinition' => function (): ?\Symfony\Component\DependencyInjection\Tests\Fixtures\CustomDefinition {
82+
return $this->services['foo_service'] = new \Symfony\Component\DependencyInjection\Tests\Fixtures\TestServiceSubscriber((new \Symfony\Component\DependencyInjection\ServiceLocator(array('Symfony\\Component\\DependencyInjection\\Tests\\Fixtures\\CustomDefinition' => function (): ?\Symfony\Component\DependencyInjection\Tests\Fixtures\CustomDefinition {
8383
return ($this->privates['Symfony\Component\DependencyInjection\Tests\Fixtures\CustomDefinition'] ?? $this->privates['Symfony\Component\DependencyInjection\Tests\Fixtures\CustomDefinition'] = new \Symfony\Component\DependencyInjection\Tests\Fixtures\CustomDefinition());
8484
}, 'Symfony\\Component\\DependencyInjection\\Tests\\Fixtures\\TestServiceSubscriber' => function (): \Symfony\Component\DependencyInjection\Tests\Fixtures\TestServiceSubscriber {
8585
return ($this->services['Symfony\Component\DependencyInjection\Tests\Fixtures\TestServiceSubscriber'] ?? $this->services['Symfony\Component\DependencyInjection\Tests\Fixtures\TestServiceSubscriber'] = new \Symfony\Component\DependencyInjection\Tests\Fixtures\TestServiceSubscriber());
8686
}, 'bar' => function (): \Symfony\Component\DependencyInjection\Tests\Fixtures\CustomDefinition {
8787
return ($this->services['Symfony\Component\DependencyInjection\Tests\Fixtures\TestServiceSubscriber'] ?? $this->services['Symfony\Component\DependencyInjection\Tests\Fixtures\TestServiceSubscriber'] = new \Symfony\Component\DependencyInjection\Tests\Fixtures\TestServiceSubscriber());
8888
}, 'baz' => function (): ?\Symfony\Component\DependencyInjection\Tests\Fixtures\CustomDefinition {
8989
return ($this->privates['Symfony\Component\DependencyInjection\Tests\Fixtures\CustomDefinition'] ?? $this->privates['Symfony\Component\DependencyInjection\Tests\Fixtures\CustomDefinition'] = new \Symfony\Component\DependencyInjection\Tests\Fixtures\CustomDefinition());
90-
})), 'withContext']('foo_service', $this));
90+
})))->withContext('foo_service', $this));
9191
}
9292
}

0 commit comments

Comments
 (0)