Skip to content

Commit 86f8d74

Browse files
committed
Merge branch '2.8' into 3.1
* 2.8: [FrameworkBundle] Fix Incorrect line break in exception message (500 debug page) [Security] Added note inside phpdoc. Minor cleanups and improvements [form] lazy trans `post_max_size_message`. [DI] Fix setting synthetic services on ContainerBuilder [ClassLoader] Fix ClassCollectionLoader inlining with declare(strict_types=1)
2 parents c5aa4f0 + 9e9b41d commit 86f8d74

File tree

20 files changed

+113
-57
lines changed

20 files changed

+113
-57
lines changed

src/Symfony/Bundle/FrameworkBundle/Resources/public/css/body.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ build: 56
3939
font-family: Georgia, "Times New Roman", Times, serif;
4040
font-size: 20px;
4141
color: #313131;
42-
word-break: break-all;
42+
word-wrap: break-word;
4343
}
4444
.sf-reset li {
4545
padding-bottom: 10px;

src/Symfony/Component/ClassLoader/ClassCollectionLoader.php

Lines changed: 38 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,12 @@ public static function load($classes, $cacheDir, $name, $autoReload, $adaptive =
5555

5656
$classes = array_unique($classes);
5757

58-
$cache = $cacheDir.'/'.$name.$extension;
58+
// cache the core classes
59+
if (!is_dir($cacheDir) && !@mkdir($cacheDir, 0777, true) && !is_dir($cacheDir)) {
60+
throw new \RuntimeException(sprintf('Class Collection Loader was not able to create directory "%s"', $cacheDir));
61+
}
62+
$cacheDir = rtrim(realpath($cacheDir), '/'.DIRECTORY_SEPARATOR);
63+
$cache = $cacheDir.DIRECTORY_SEPARATOR.$name.$extension;
5964

6065
// auto-reload
6166
$reload = false;
@@ -93,31 +98,50 @@ public static function load($classes, $cacheDir, $name, $autoReload, $adaptive =
9398
$declared = array_merge(get_declared_classes(), get_declared_interfaces(), get_declared_traits());
9499
}
95100

101+
$c = '(?:\s*+(?:(?:#|//)[^\n]*+\n|/\*(?:(?<!\*/).)++)?+)*+';
102+
$strictTypesRegex = str_replace('.', $c, "'^<\?php\s.declare.\(.strict_types.=.1.\).;'is");
103+
104+
$cacheDir = explode(DIRECTORY_SEPARATOR, $cacheDir);
96105
$files = array();
97106
$content = '';
98107
foreach (self::getOrderedClasses($classes) as $class) {
99108
if (in_array($class->getName(), $declared)) {
100109
continue;
101110
}
102111

103-
$files[] = $class->getFileName();
112+
$files[] = $file = $class->getFileName();
113+
$c = file_get_contents($file);
104114

105-
$c = preg_replace(array('/^\s*<\?php/', '/\?>\s*$/'), '', file_get_contents($class->getFileName()));
115+
if (preg_match($strictTypesRegex, $c)) {
116+
$file = explode(DIRECTORY_SEPARATOR, $file);
106117

107-
// fakes namespace declaration for global code
108-
if (!$class->inNamespace()) {
109-
$c = "\nnamespace\n{\n".$c."\n}\n";
110-
}
118+
for ($i = 0; isset($file[$i], $cacheDir[$i]); ++$i) {
119+
if ($file[$i] !== $cacheDir[$i]) {
120+
break;
121+
}
122+
}
123+
if (1 >= $i) {
124+
$file = var_export(implode(DIRECTORY_SEPARATOR, $file), true);
125+
} else {
126+
$file = array_slice($file, $i);
127+
$file = str_repeat('..'.DIRECTORY_SEPARATOR, count($cacheDir) - $i).implode(DIRECTORY_SEPARATOR, $file);
128+
$file = '__DIR__.'.var_export(DIRECTORY_SEPARATOR.$file, true);
129+
}
111130

112-
$c = self::fixNamespaceDeclarations('<?php '.$c);
113-
$c = preg_replace('/^\s*<\?php/', '', $c);
131+
$c = "\nnamespace {require $file;}";
132+
} else {
133+
$c = preg_replace(array('/^\s*<\?php/', '/\?>\s*$/'), '', $c);
114134

115-
$content .= $c;
116-
}
135+
// fakes namespace declaration for global code
136+
if (!$class->inNamespace()) {
137+
$c = "\nnamespace\n{\n".$c."\n}\n";
138+
}
117139

118-
// cache the core classes
119-
if (!is_dir($cacheDir) && !@mkdir($cacheDir, 0777, true) && !is_dir($cacheDir)) {
120-
throw new \RuntimeException(sprintf('Class Collection Loader was not able to create directory "%s"', $cacheDir));
140+
$c = self::fixNamespaceDeclarations('<?php '.$c);
141+
$c = preg_replace('/^\s*<\?php/', '', $c);
142+
}
143+
144+
$content .= $c;
121145
}
122146
self::writeCacheFile($cache, '<?php '.$content);
123147

src/Symfony/Component/ClassLoader/Tests/ClassCollectionLoaderTest.php

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -216,18 +216,20 @@ public function testUnableToLoadClassException()
216216

217217
public function testCommentStripping()
218218
{
219-
if (is_file($file = sys_get_temp_dir().'/bar.php')) {
219+
if (is_file($file = __DIR__.'/bar.php')) {
220220
unlink($file);
221221
}
222222
spl_autoload_register($r = function ($class) {
223223
if (0 === strpos($class, 'Namespaced') || 0 === strpos($class, 'Pearlike_')) {
224-
require_once __DIR__.'/Fixtures/'.str_replace(array('\\', '_'), '/', $class).'.php';
224+
@require_once __DIR__.'/Fixtures/'.str_replace(array('\\', '_'), '/', $class).'.php';
225225
}
226226
});
227227

228+
$strictTypes = defined('HHVM_VERSION') ? '' : "\nnamespace {require __DIR__.'/Fixtures/Namespaced/WithStrictTypes.php';}";
229+
228230
ClassCollectionLoader::load(
229-
array('Namespaced\\WithComments', 'Pearlike_WithComments'),
230-
sys_get_temp_dir(),
231+
array('Namespaced\\WithComments', 'Pearlike_WithComments', $strictTypes ? 'Namespaced\\WithStrictTypes' : 'Namespaced\\WithComments'),
232+
__DIR__,
231233
'bar',
232234
false
233235
);
@@ -267,7 +269,9 @@ class Pearlike_WithComments
267269
}
268270
}
269271
EOF
270-
, str_replace("<?php \n", '', file_get_contents($file)));
272+
.$strictTypes,
273+
str_replace(array("<?php \n", '\\\\'), array('', '/'), file_get_contents($file))
274+
);
271275

272276
unlink($file);
273277
}

src/Symfony/Component/ClassLoader/Tests/ClassMapGeneratorTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ public function getTestCreateMapTests()
7676
'Namespaced\\Foo' => realpath(__DIR__).'/Fixtures/Namespaced/Foo.php',
7777
'Namespaced\\Baz' => realpath(__DIR__).'/Fixtures/Namespaced/Baz.php',
7878
'Namespaced\\WithComments' => realpath(__DIR__).'/Fixtures/Namespaced/WithComments.php',
79+
'Namespaced\WithStrictTypes' => realpath(__DIR__).'/Fixtures/Namespaced/WithStrictTypes.php',
7980
),
8081
),
8182
array(__DIR__.'/Fixtures/beta/NamespaceCollision', array(
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
/*
4+
* foo
5+
*/
6+
7+
declare (strict_types = 1);
8+
9+
namespace Namespaced;
10+
11+
class WithStrictTypes
12+
{
13+
}

src/Symfony/Component/DependencyInjection/Compiler/RepeatedPass.php

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,12 @@ public function __construct(array $passes)
5656
*/
5757
public function process(ContainerBuilder $container)
5858
{
59-
$this->repeat = false;
60-
foreach ($this->passes as $pass) {
61-
$pass->process($container);
62-
}
63-
64-
if ($this->repeat) {
65-
$this->process($container);
66-
}
59+
do {
60+
$this->repeat = false;
61+
foreach ($this->passes as $pass) {
62+
$pass->process($container);
63+
}
64+
} while ($this->repeat);
6765
}
6866

6967
/**

src/Symfony/Component/DependencyInjection/ContainerBuilder.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,8 @@ public function set($id, $service)
341341
{
342342
$id = strtolower($id);
343343

344-
if ($this->isFrozen() && (!isset($this->definitions[$id]) || !$this->definitions[$id]->isSynthetic())) {
344+
if ($this->isFrozen() && (isset($this->definitions[$id]) && !$this->definitions[$id]->isSynthetic())) {
345+
// setting a synthetic service on a frozen container is alright
345346
throw new BadMethodCallException(sprintf('Setting service "%s" for an unknown or non-synthetic service definition on a frozen container is not allowed.', $id));
346347
}
347348

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ private function addEdges()
128128
*
129129
* @return array An array of edges
130130
*/
131-
private function findEdges($id, $arguments, $required, $name)
131+
private function findEdges($id, array $arguments, $required, $name)
132132
{
133133
$edges = array();
134134
foreach ($arguments as $argument) {
@@ -241,7 +241,7 @@ private function endDot()
241241
*
242242
* @return string A comma separated list of attributes
243243
*/
244-
private function addAttributes($attributes)
244+
private function addAttributes(array $attributes)
245245
{
246246
$code = array();
247247
foreach ($attributes as $k => $v) {
@@ -258,7 +258,7 @@ private function addAttributes($attributes)
258258
*
259259
* @return string A space separated list of options
260260
*/
261-
private function addOptions($options)
261+
private function addOptions(array $options)
262262
{
263263
$code = array();
264264
foreach ($options as $k => $v) {

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,7 @@ private function addServiceReturn($id, $definition)
374374
* @throws InvalidArgumentException
375375
* @throws RuntimeException
376376
*/
377-
private function addServiceInstance($id, $definition)
377+
private function addServiceInstance($id, Definition $definition)
378378
{
379379
$class = $definition->getClass();
380380

@@ -422,7 +422,7 @@ private function addServiceInstance($id, $definition)
422422
*
423423
* @return bool
424424
*/
425-
private function isSimpleInstance($id, $definition)
425+
private function isSimpleInstance($id, Definition $definition)
426426
{
427427
foreach (array_merge(array($definition), $this->getInlinedDefinitions($definition)) as $sDefinition) {
428428
if ($definition !== $sDefinition && !$this->hasReference($id, $sDefinition->getMethodCalls())) {
@@ -446,7 +446,7 @@ private function isSimpleInstance($id, $definition)
446446
*
447447
* @return string
448448
*/
449-
private function addServiceMethodCalls($id, $definition, $variableName = 'instance')
449+
private function addServiceMethodCalls($id, Definition $definition, $variableName = 'instance')
450450
{
451451
$calls = '';
452452
foreach ($definition->getMethodCalls() as $call) {
@@ -461,7 +461,7 @@ private function addServiceMethodCalls($id, $definition, $variableName = 'instan
461461
return $calls;
462462
}
463463

464-
private function addServiceProperties($id, $definition, $variableName = 'instance')
464+
private function addServiceProperties($id, Definition $definition, $variableName = 'instance')
465465
{
466466
$code = '';
467467
foreach ($definition->getProperties() as $name => $value) {
@@ -481,7 +481,7 @@ private function addServiceProperties($id, $definition, $variableName = 'instanc
481481
*
482482
* @throws ServiceCircularReferenceException when the container contains a circular reference
483483
*/
484-
private function addServiceInlinedDefinitionsSetup($id, $definition)
484+
private function addServiceInlinedDefinitionsSetup($id, Definition $definition)
485485
{
486486
$this->referenceVariables[$id] = new Variable('instance');
487487

@@ -525,7 +525,7 @@ private function addServiceInlinedDefinitionsSetup($id, $definition)
525525
*
526526
* @return string
527527
*/
528-
private function addServiceConfigurator($id, $definition, $variableName = 'instance')
528+
private function addServiceConfigurator($id, Definition $definition, $variableName = 'instance')
529529
{
530530
if (!$callable = $definition->getConfigurator()) {
531531
return '';
@@ -561,7 +561,7 @@ private function addServiceConfigurator($id, $definition, $variableName = 'insta
561561
*
562562
* @return string
563563
*/
564-
private function addService($id, $definition)
564+
private function addService($id, Definition $definition)
565565
{
566566
$this->definitionVariables = new \SplObjectStorage();
567567
$this->referenceVariables = array();
@@ -1032,7 +1032,7 @@ protected function getDefaultParameters()
10321032
*
10331033
* @throws InvalidArgumentException
10341034
*/
1035-
private function exportParameters($parameters, $path = '', $indent = 12)
1035+
private function exportParameters(array $parameters, $path = '', $indent = 12)
10361036
{
10371037
$php = array();
10381038
foreach ($parameters as $key => $value) {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ private function addServices(\DOMElement $parent)
271271
* @param \DOMElement $parent
272272
* @param string $keyAttribute
273273
*/
274-
private function convertParameters($parameters, $type, \DOMElement $parent, $keyAttribute = 'key')
274+
private function convertParameters(array $parameters, $type, \DOMElement $parent, $keyAttribute = 'key')
275275
{
276276
$withKeys = array_keys($parameters) !== range(0, count($parameters) - 1);
277277
foreach ($parameters as $key => $value) {
@@ -317,7 +317,7 @@ private function convertParameters($parameters, $type, \DOMElement $parent, $key
317317
*
318318
* @return array
319319
*/
320-
private function escape($arguments)
320+
private function escape(array $arguments)
321321
{
322322
$args = array();
323323
foreach ($arguments as $k => $v) {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ private function getExpressionCall($expression)
307307
*
308308
* @return array
309309
*/
310-
private function prepareParameters($parameters, $escape = true)
310+
private function prepareParameters(array $parameters, $escape = true)
311311
{
312312
$filtered = array();
313313
foreach ($parameters as $key => $value) {
@@ -330,7 +330,7 @@ private function prepareParameters($parameters, $escape = true)
330330
*
331331
* @return array
332332
*/
333-
private function escape($arguments)
333+
private function escape(array $arguments)
334334
{
335335
$args = array();
336336
foreach ($arguments as $k => $v) {

src/Symfony/Component/DependencyInjection/Loader/YamlFileLoader.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -425,9 +425,9 @@ private function resolveServices($value)
425425
{
426426
if (is_array($value)) {
427427
$value = array_map(array($this, 'resolveServices'), $value);
428-
} elseif (is_string($value) && 0 === strpos($value, '@=')) {
428+
} elseif (is_string($value) && 0 === strpos($value, '@=')) {
429429
return new Expression(substr($value, 2));
430-
} elseif (is_string($value) && 0 === strpos($value, '@')) {
430+
} elseif (is_string($value) && 0 === strpos($value, '@')) {
431431
if (0 === strpos($value, '@@')) {
432432
$value = substr($value, 1);
433433
$invalidBehavior = null;

src/Symfony/Component/DependencyInjection/Tests/ContainerBuilderTest.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -626,14 +626,12 @@ public function testThrowsExceptionWhenSetServiceOnAFrozenContainer()
626626
$container->set('a', new \stdClass());
627627
}
628628

629-
/**
630-
* @expectedException \BadMethodCallException
631-
*/
632629
public function testThrowsExceptionWhenAddServiceOnAFrozenContainer()
633630
{
634631
$container = new ContainerBuilder();
635632
$container->compile();
636-
$container->set('a', new \stdClass());
633+
$container->set('a', $foo = new \stdClass());
634+
$this->assertSame($foo, $container->get('a'));
637635
}
638636

639637
public function testNoExceptionWhenSetSyntheticServiceOnAFrozenContainer()

src/Symfony/Component/DependencyInjection/Tests/ContainerTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ public function testGetCircularReference()
210210

211211
/**
212212
* @expectedException \Symfony\Component\DependencyInjection\Exception\RuntimeException
213-
* @expectedExcepionMessage You have requested a synthetic service ("request"). The DIC does not know how to construct this service.
213+
* @expectedExceptionMessage You have requested a synthetic service ("request"). The DIC does not know how to construct this service.
214214
*/
215215
public function testGetSyntheticServiceAlwaysThrows()
216216
{

src/Symfony/Component/Form/Extension/Core/Type/FormType.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,13 @@ public function configureOptions(OptionsResolver $resolver)
142142
};
143143
};
144144

145+
// Wrap "post_max_size_message" in a closure to translate it lazily
146+
$uploadMaxSizeMessage = function (Options $options) {
147+
return function () use ($options) {
148+
return $options['post_max_size_message'];
149+
};
150+
};
151+
145152
// For any form that is not represented by a single HTML control,
146153
// errors should bubble up by default
147154
$errorBubbling = function (Options $options) {
@@ -172,9 +179,11 @@ public function configureOptions(OptionsResolver $resolver)
172179
'action' => '',
173180
'attr' => array(),
174181
'post_max_size_message' => 'The uploaded file was too large. Please try to upload a smaller file.',
182+
'upload_max_size_message' => $uploadMaxSizeMessage, // internal
175183
));
176184

177185
$resolver->setAllowedTypes('label_attr', 'array');
186+
$resolver->setAllowedTypes('upload_max_size_message', array('callable'));
178187
}
179188

180189
/**

src/Symfony/Component/Form/Extension/HttpFoundation/HttpFoundationRequestHandler.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public function handleRequest(FormInterface $form, $request = null)
7878
$form->submit(null, false);
7979

8080
$form->addError(new FormError(
81-
$form->getConfig()->getOption('post_max_size_message'),
81+
call_user_func($form->getConfig()->getOption('upload_max_size_message')),
8282
null,
8383
array('{{ max }}' => $this->serverParams->getNormalizedIniPostMaxSize())
8484
));

src/Symfony/Component/Form/Extension/Validator/Type/UploadValidatorExtension.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,10 @@ public function configureOptions(OptionsResolver $resolver)
4242
{
4343
$translator = $this->translator;
4444
$translationDomain = $this->translationDomain;
45-
46-
$resolver->setNormalizer('post_max_size_message', function (Options $options, $errorMessage) use ($translator, $translationDomain) {
47-
return $translator->trans($errorMessage, array(), $translationDomain);
45+
$resolver->setNormalizer('upload_max_size_message', function (Options $options, $message) use ($translator, $translationDomain) {
46+
return function () use ($translator, $translationDomain, $message) {
47+
return $translator->trans(call_user_func($message), array(), $translationDomain);
48+
};
4849
});
4950
}
5051

0 commit comments

Comments
 (0)