8000 Merge branch '4.0' · symfony/symfony@34dd512 · GitHub
[go: up one dir, main page]

Skip to content

Commit 34dd512

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 96e53f8 + 8ba3061 commit 34dd512

File tree

18 files changed

+197
-67
lines changed

18 files changed

+197
-67
lines changed

src/Symfony/Bundle/FrameworkBundle/Controller/ControllerTrait.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ protected function file($file, string $fileName = null, string $disposition = Re
153153
protected function addFlash(string $type, string $message)
154154
{
155155
if (!$this->container->has('session')) {
156-
throw new \LogicException('You can not use the addFlash method if sessions are disabled. Enable them in config/packages/framework.yaml.');
156+
throw new \LogicException('You can not use the addFlash method if sessions are disabled. Enable them in "config/packages/framework.yaml".');
157157
}
158158

159159
$this->container->get('session')->getFlashBag()->add($type, $message);
@@ -169,7 +169,7 @@ protected function addFlash(string $type, string $message)
169169
protected function isGranted($attributes, $subject = null): bool
170170
{
171171
if (!$this->container->has('security.authorization_checker')) {
172-
throw new \LogicException('The SecurityBundle is not registered in your application. Try running "composer require security"');
172+
throw new \LogicException('The SecurityBundle is not registered in your application. Try running "composer require symfony/security-bundle".');
173173
}
174174

175175
return $this->container->get('security.authorization_checker')->isGranted($attributes, $subject);
@@ -206,7 +206,7 @@ protected function renderView(string $view, array $parameters = array()): string
206206
}
207207

208208
if (!$this->container->has('twig')) {
209-
throw new \LogicException('You can not use the "renderView" method if the Templating Component or the Twig Bundle are not available. Try running "composer require twig"');
209+
throw new \LogicException('You can not use the "renderView" method if the Templating Component or the Twig Bundle are not available. Try running "composer require symfony/twig-bundle".');
210210
}
211211

212212
return $this->container->get('twig')->render($view, $parameters);
@@ -224,7 +224,7 @@ protected function render(string $view, array $parameters = array(), Response $r
224224
} elseif ($this->container->has('twig')) {
225225
$content = $this->container->get('twig')->render($view, $parameters);
226226
} else {
227-
throw new \LogicException('You can not use the "render" method if the Templating Component or the Twig Bundle are not available. Try running "composer require twig"');
227+
throw new \LogicException('You can not use the "render" method if the Templating Component or the Twig Bundle are not available. Try running "composer require symfony/twig-bundle".');
228228
}
229229

230230
if (null === $response) {
@@ -256,7 +256,7 @@ protected function stream(string $view, array $parameters = array(), StreamedRes
256256
$twig->display($view, $parameters);
257257
};
258258
} else {
259-
throw new \LogicException('You can not use the "stream" method if the Templating Component or the Twig Bundle are not available. Try running "composer require twig"');
259+
throw new \LogicException('You can not use the "stream" method if the Templating Component or the Twig Bundle are not available. Try running "composer require symfony/twig-bundle".');
260260
}
261261

262262
if (null === $response) {
@@ -296,7 +296,7 @@ protected function createNotFoundException(string $message = 'Not Found', \Excep
296296
protected function createAccessDeniedException(string $message = 'Access Denied.', \Exception $previous = null): AccessDeniedException
297297
{
298298
if (!class_exists(AccessDeniedException::class)) {
299-
throw new \LogicException('You can not use the "createAccessDeniedException" method if the Security component is not available.');
299+
throw new \LogicException('You can not use the "createAccessDeniedException" method if the Security component is not available. Try running "composer require symfony/security-bundle".');
300300
}
301301

302302
return new AccessDeniedException($message, $previous);
@@ -332,7 +332,7 @@ protected function createFormBuilder($data = null, array $options = array()): Fo
332332
protected function getDoctrine(): ManagerRegistry
333333
{
334334
if (!$this->container->has('doctrine')) {
335-
throw new \LogicException('The DoctrineBundle is not registered in your application. Try running "composer require doctrine"');
335+
throw new \LogicException('The DoctrineBundle is not registered in your application. Try running "composer require symfony/orm-pack".');
336336
}
337337

338338
return $this->container->get('doctrine');
@@ -352,7 +352,7 @@ protected function getDoctrine(): ManagerRegistry
352352
protected function getUser()
353353
{
354354
if (!$this->container->has('security.token_storage')) {
355-
throw new \LogicException('The SecurityBundle is not registered in your application. Try running "composer require security"');
355+
throw new \LogicException('The SecurityBundle is not registered in your application. Try running "composer require symfony/security-bundle".');
356356
}
357357

358358
if (null === $token = $this->container->get('security.token_storage')->getToken()) {
@@ -378,7 +378,7 @@ protected function getUser()
378378
protected function isCsrfTokenValid(string $id, string $token): bool
379379
{
380380
if (!$this->container->has('security.csrf.token_manager')) {
381-
throw new \LogicException('CSRF protection is not enabled in your application. Enable it with the "csrf_protection" key in "config/packages/framework.yaml"');
381+
throw new \LogicException('CSRF protection is not enabled in your application. Enable it with the "csrf_protection" key in "config/packages/framework.yaml".');
382382
}
383383

384384
return $this->container->get('security.csrf.token_manager')->isTokenValid(new CsrfToken($id, $token));

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
3737
use Symfony\Component\DependencyInjection\Exception\LogicException;
3838
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
39+
use Symfony\Component\DependencyInjection\Parameter;
3940
use Symfony\Component\DependencyInjection\ParameterBag\ContainerBagInterface;
4041
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
4142
use Symfony\Component\DependencyInjection\Reference;
@@ -1158,7 +1159,7 @@ private function registerSecurityCsrfConfiguration(array $config, ContainerBuild
11581159
}
11591160

11601161
if (!class_exists('Symfony\Component\Security\Csrf\CsrfToken')) {
1161-
throw new LogicException('CSRF support cannot be enabled as the Security CSRF component is not installed. Try running "composer require security-csrf".');
1162+
throw new LogicException('CSRF support cannot be enabled as the Security CSRF component is not installed. Try running "composer require symfony/security-csrf".');
11621163
}
11631164

11641165
if (!$this->sessionConfigEnabled) {
@@ -1358,7 +1359,7 @@ private function registerLockConfiguration(array $config, ContainerBuilder $cont
13581359

13591360
private function registerCacheConfiguration(array $config, ContainerBuilder $container)
13601361
{
1361-
$version = substr(str_replace('/', '-', base64_encode(hash('sha256', uniqid(mt_rand(), true), true))), 0, 22);
1362+
$version = new Parameter('container.build_id');
13621363
$container->getDefinition('cache.adapter.apcu')->replaceArgument(2, $version);
13631364
$container->getDefinition('cache.adapter.system')->replaceArgument(2, $version);
13641365
$container->getDefinition('cache.adapter.filesystem')->replaceArgument(2, $config['directory']);

src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/FrameworkExtensionTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1056,6 +1056,9 @@ protected function createContainer(array $data = array())
10561056
'kernel.name' => 'kernel',
10571057
'kernel.root_dir' => __DIR__,
10581058
'kernel.container_class' => 'testContainer',
1059+
'container.build_hash' => 'Abc1234',
1060+
'container.build_id' => hash('crc32', 'Abc123423456789'),
1061+
'container.build_time' => 23456789,
10591062
), $data)));
10601063
}
10611064

src/Symfony/Bundle/FrameworkBundle/composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
"php": "^7.1.3",
2020
"ext-xml": "*",
2121
"symfony/cache": "~3.4|~4.0",
22-
"symfony/dependency-injection": "~3.4|~4.0",
22+
"symfony/dependency-injection": "^3.4.3|^4.0.3",
2323
"symfony/config": "~3.4|~4.0",
2424
"symfony/event-dispatcher": "~3.4|~4.0",
2525
"symfony/http-foundation": "~3.4|~4.0",

src/Symfony/Bundle/SecurityBundle/DependencyInjection/MainConfiguration.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ private function addFirewallsSection(ArrayNodeDefinition $rootNode, array $facto
240240
->arrayNode('anonymous')
241241
->canBeUnset()
242242
->children()
243-
->scalarNode('secret')->defaultValue(uniqid('', true))->end()
243+
->scalarNode('secret')->defaultNull()->end()
244244
->end()
245245
->end()
246246
->arrayNode('switch_user')

src/Symfony/Bundle/SecurityBundle/DependencyInjection/Security/UserProvider/InMemoryFactory.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Symfony\Component\Config\Definition\Builder\NodeDefinition;
1515
use Symfony\Component\DependencyInjection\ChildDefinition;
1616
use Symfony\Component\DependencyInjection\ContainerBuilder;
17+
use Symfony\Component\DependencyInjection\Parameter;
1718
use Symfony\Component\DependencyInjection\Reference;
1819

1920
/**
@@ -27,13 +28,14 @@ class InMemoryFactory implements UserProviderFactoryInterface
2728
public function create(ContainerBuilder $container, $id, $config)
2829
{
2930
$definition = $container->setDefinition($id, new ChildDefinition('security.user.provider.in_memory'));
31+
$defaultPassword = new Parameter('container.build_id');
3032

3133
foreach ($config['users'] as $username => $user) {
3234
$userId = $id.'_'.$username;
3335

3436
$container
3537
->setDefinition($userId, new ChildDefinition('security.user.provider.in_memory.user'))
36-
->setArguments(array($username, (string) $user['password'], $user['roles']))
38+
->setArguments(array($username, null !== $user['password'] ? (string) $user['password'] : $defaultPassword, $user['roles']))
3739
;
3840

3941
$definition->addMethodCall('createUser', array(new Reference($userId)));
@@ -55,7 +57,7 @@ public function addConfiguration(NodeDefinition $node)
5557
->normalizeKeys(false)
5658
->prototype('array')
5759
->children()
58-
->scalarNode('password')->defaultValue(uniqid('', true))->end()
60+
->scalarNode('password')->defaultNull()->end()
5961
->arrayNode('roles')
6062
->beforeNormalization()->ifString()->then(function ($v) { return preg_split('/\s*,\s*/', $v); })->end()
6163
->prototype('scalar')->end()

src/Symfony/Bundle/SecurityBundle/DependencyInjection/SecurityExtension.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
2424
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
2525
use Symfony\Component\DependencyInjection\ContainerBuilder;
26+
use Symfony\Component\DependencyInjection\Parameter;
2627
use Symfony\Component\DependencyInjection\Reference;
2728
use Symfony\Component\Config\FileLocator;
2829
use Symfony\Component\Security\Core\Authorization\ExpressionLanguage;
@@ -431,6 +432,10 @@ private function createAuthenticationListeners($container, $id, $firewall, &$aut
431432

432433
// Anonymous
433434
if (isset($firewall['anonymous'])) {
435+
if (null === $firewall['anonymous']['secret']) {
436+
$firewall['anonymous']['secret'] = new Parameter('container.build_hash');
437+
}
438+
434439
$listenerId = 'security.authentication.listener.anonymous.'.$id;
435440
$container
436441
->setDefinition($listenerId, new ChildDefinition('security.authentication.listener.anonymous'))

src/Symfony/Bundle/SecurityBundle/composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
"php": "^7.1.3",
2020
"ext-xml": "*",
2121
"symfony/security": "~3.4|~4.0",
22-
"symfony/dependency-injection": "~3.4|~4.0",
22+
"symfony/dependency-injection": "^3.4.3|^4.0.3",
2323
"symfony/http-kernel": "~3.4|~4.0"
2424
},
2525
"require-dev": {

src/Symfony/Bundle/TwigBundle/composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
"require": {
1919
"php": "^7.1.3",
2020
"symfony/config": "~3.4|~4.0",
21-
"symfony/twig-bridge": "^3.4.3|~4.0",
21+
"symfony/twig-bridge": "^3.4.3|^4.0.3",
2222
"symfony/http-foundation": "~3.4|~4.0",
2323
"symfony/http-kernel": "~3.4|~4.0",
2424
"twig/twig": "~1.34|~2.4"

src/Symfony/Bundle/WebProfilerBundle/Resources/views/Collector/time.html.twig

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,10 @@
1414
} %}
1515
{% endif %}
1616

17-
{% set has_time_events = collector.events|length > 0 %}
1817

1918
{% block toolbar %}
19+
{% set has_time_events = collector.events|length > 0 %}
20+
2021
{% set total_time = has_time_events ? '%.0f'|format(collector.duration) : 'n/a' %}
2122
{% set initialization_time = collector.events|length ? '%.0f'|format(collector.inittime) : 'n/a' %}
2223
{% set status_color = has_time_events and collector.duration > 1000 ? 'yellow' : '' %}
@@ -49,6 +50,7 @@
4950
{% endblock %}
5051

5152
{% block panel %}
53+
{% set has_time_events = collector.events|length > 0 %}
5254
<h2>Performance metrics</h2>
5355

5456
<div class="metrics">

src/Symfony/Component/DependencyInjection/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
}

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

Lines changed: 36 additions & 8 deletions
103CD
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

0 commit comments

Comments
 (0)
0