8000 Merge branch '4.4' into 5.4 · zimny9932/symfony@cbb5926 · GitHub
[go: up one dir, main page]

Skip to content

Commit cbb5926

Browse files
committed
Merge branch '4.4' into 5.4
* 4.4: Fix CS quote address names if they contain parentheses [FrameworkBundle] Fail gracefully when forms use disabled CSRF [Mime] Fix inline parts when added via attachPart() Fail gracefully when attempting to autowire composite types [VarDumper] Add a test case for nesting intersection and union types
2 parents a1fa390 + 080b8f3 commit cbb5926

File tree

15 files changed

+288
-24
lines changed

15 files changed

+288
-24
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -669,6 +669,10 @@ private function registerFormConfiguration(array $config, ContainerBuilder $cont
669669
}
670670

671671
if ($this->isConfigEnabled($container, $config['form']['csrf_protection'])) {
672+
if (!$container->hasDefinition('security.csrf.token_generator')) {
673+
throw new \LogicException('To use form CSRF protection, "framework.csrf_protection" must be enabled.');
674+
}
675+
672676
$loader->load('form_csrf.php');
673677

674678
$container->setParameter('form.type_extension.csrf.enabled', true);
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
$container->loadFromExtension('framework', [
4+
'csrf_protection' => false,
5+
'form' => [
6+
'csrf_protection' => true,
7+
],
8+
]);
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" ?>
2+
3+
<container xmlns="http://symfony.com/schema/dic/services"
4+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5+
xmlns:framework="http://symfony.com/schema/dic/symfony"
6+
xsi:schemaLocation="http://symfony.com/schema/dic/services
7+
https://symfony.com/schema/dic/services/services-1.0.xsd
8+
http://symfony.com/schema/dic/symfony
9+
https://symfony.com/schema/dic/symfony/symfony-1.0.xsd"
10+
>
11+
<framework:config>
12+
<framework:csrf-protection enabled="false"/>
13+
<framework:form enabled="true">
14+
<framework:csrf-protection enabled="true"/>
15+
</framework:form>
16+
</framework:config>
17+
</container>
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
framework:
2+
csrf_protection: false
3+
form:
4+
csrf_protection: true

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,14 @@ public function testFormCsrfProtection()
9797
$this->assertEquals('%form.type_extension.csrf.field_name%', $def->getArgument(2));
9898
}
9999

100+
public function testFormCsrfProtectionWithCsrfDisabled()
101+
{
102+
$this->expectException(\LogicException::class);
103+
$this->expectExceptionMessage('To use form CSRF protection `framework.csrf_protection` must be enabled.');
104+
105+
$this->createContainerFromFile('form_csrf_disabled');
106+
}
107+
100108
public function testPropertyAccessWithDefaultValue()
101109
{
102110
$container = $this->createContainerFromFile('full');

src/Symfony/Component/DependencyInjection/LazyProxy/ProxyHelper.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ public static function getTypeHint(\ReflectionFunctionAbstract $r, \ReflectionPa
3232
return null;
3333
}
3434

35+
return self::getTypeHintForType($type, $r, $noBuiltin);
36+
}
37+
38+
private static function getTypeHintForType(\ReflectionType $type, \ReflectionFunctionAbstract $r, bool $noBuiltin): ?string
39+
{
3540
$types = [];
3641
$glue = '|';
3742
if ($type instanceof \ReflectionUnionType) {
@@ -46,6 +51,17 @@ public static function getTypeHint(\ReflectionFunctionAbstract $r, \ReflectionPa
4651
}
4752

4853
foreach ($reflectionTypes as $type) {
54+
if ($type instanceof \ReflectionIntersectionType) {
55+
$typeHint = self::getTypeHintForType($type, $r, $noBuiltin);
56+
if (null === $typeHint) {
57+
return null;
58+
}
59+
60+
$types[] = sprintf('(%s)', $typeHint);
61+
62+
continue;
63+
}
64+
4965
if ($type->isBuiltin()) {
5066
if (!$noBuiltin) {
5167
$types[] = $type->getName();

src/Symfony/Component/DependencyInjection/Tests/Compiler/AutowirePassTest.php

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -244,8 +244,6 @@ public function testTypeNotGuessableNoServicesFound()
244244
*/
245245
public function testTypeNotGuessableUnionType()
246246
{
247-
$this->expectException(AutowiringFailedException::class);
248-
$this->expectExceptionMessage('Cannot autowire service "a": argument "$collision" of method "Symfony\Component\DependencyInjection\Tests\Compiler\UnionClasses::__construct()" has type "Symfony\Component\DependencyInjection\Tests\Compiler\CollisionA|Symfony\Component\DependencyInjection\Tests\Compiler\CollisionB" but this class was not found.');
249247
$container = new ContainerBuilder();
250248

251249
$container->register(CollisionA::class);
@@ -255,6 +253,9 @@ public function testTypeNotGuessableUnionType()
255253
$aDefinition->setAutowired(true);
256254

257255
$pass = new AutowirePass();
256+
257+
$this->expectException(AutowiringFailedException::class);
258+
$this->expectExceptionMessage('Cannot autowire service "a": argument "$collision" of method "Symfony\Component\DependencyInjection\Tests\Compiler\UnionClasses::__construct()" has type "Symfony\Component\DependencyInjection\Tests\Compiler\CollisionA|Symfony\Component\DependencyInjection\Tests\Compiler\CollisionB" but this class was not found.');
258259
$pass->process($container);
259260
}
260261

@@ -294,7 +295,27 @@ public function testTypeNotGuessableIntersectionType()
294295
$pass = new AutowirePass();
295296

296297
$this->expectException(AutowiringFailedException::class);
297-
$this->expectExceptionMessage('Cannot autowire service "a": argument "$collision" of method "Symfony\Component\DependencyInjection\Tests\Compiler\IntersectionClasses::__construct()" has type "Symfony\Component\DependencyInjection\Tests\Compiler\AnotherInterface&Symfony\Component\DependencyInjection\Tests\Compiler\CollisionInterface" but this class was not found.');
298+
$this->expectExceptionMessage('Cannot autowire service "a": argument "$collision" of method "Symfony\Component\DependencyInjection\Tests\Compiler\IntersectionClasses::__construct()" has type "Symfony\Component\DependencyInjection\Tests\Compiler\CollisionInterface&Symfony\Component\DependencyInjection\Tests\Compiler\AnotherInterface" but this class was not found.');
299+
$pass->process($container);
300+
}
301+
302+
/**
303+
* @requires PHP 8.2
304+
*/
305+
public function testTypeNotGuessableCompositeType()
306+
{
307+
$container = new ContainerBuilder();
308+
309+
$container->register(CollisionInterface::class);
310+
$container->register(AnotherInterface::class);
311+
312+
$aDefinition = $container->register('a', CompositeTypeClasses::class);
313+
$aDefinition->setAutowired(true);
314+
315+
$pass = new AutowirePass();
316+
317+
$this->expectException(AutowiringFailedException::class);
318+
$this->expectExceptionMessage('Cannot autowire service "a": argument "$collision" of method "Symfony\Component\DependencyInjection\Tests\Compiler\CompositeTypeClasses::__construct()" has type "(Symfony\Component\DependencyInjection\Tests\Compiler\CollisionInterface&Symfony\Component\DependencyInjection\Tests\Compiler\AnotherInterface)|Symfony\Component\DependencyInjection\Tests\Compiler\YetAnotherInterface" but this class was not found.');
298319
$pass->process($container);
299320
}
300321

@@ -418,6 +439,22 @@ public function testParameterWithNullUnionIsSkipped()
418439
$this->assertNull($definition->getArgument(0));
419440
}
420441

442+
/**
443+
* @requires PHP 8.2
444+
*/
445+
public function testParameterWithNullableIntersectionIsSkipped()
446+
{
447+
$container = new ContainerBuilder();
448+
449+
$optDefinition = $container->register('opt', NullableIntersection::class);
450+
$optDefinition->setAutowired(true);
451+
452+
(new AutowirePass())->process($container);
453+
454+
$definition = $container->getDefinition('opt');
455+
$this->assertNull($definition->getArgument(0));
456+
}
457+
421458
/**
422459
* @requires PHP 8
423460
*/

src/Symfony/Component/DependencyInjection/Tests/Fixtures/includes/autowiring_classes.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
if (\PHP_VERSION_ID >= 80100) {
1212
require __DIR__.'/intersectiontype_classes.php';
1313
}
14+
if (\PHP_VERSION_ID >= 80200) {
15+
require __DIR__.'/compositetype_classes.php';
16+
}
1417

1518
class Foo
1619
{
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace Symfony\Component\DependencyInjection\Tests\Compiler;
4+
5+
interface YetAnotherInterface
6+
{
7+
}
8+
9+
class CompositeTypeClasses
10+
{
11+
public function __construct((CollisionInterface&AnotherInterface)|YetAnotherInterface $collision)
12+
{
13+
}
14+
}
15+
16+
class NullableIntersection
17+
{
18+
public function __construct((< F438 span class=pl-smi>CollisionInterface&AnotherInterface)|null $a)
19+
{
20+
}
21+
}

src/Symfony/Component/Mime/Email.php

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -501,25 +501,37 @@ private function prepareParts(): ?array
501501
$names = array_filter(array_unique(array_merge($names[2], $names[3])));
502502
}
503503

504+
// usage of reflection is a temporary workaround for missing getters that will be added in 6.2
505+
$dispositionRef = new \ReflectionProperty(TextPart::class, 'disposition');
506+
$dispositionRef->setAccessible(true);
507+
$nameRef = new \ReflectionProperty(TextPart::class, 'name');
508+
$nameRef->setAccessible(true);
504509
$attachmentParts = $inlineParts = [];
505510
foreach ($this->attachments as $attachment) {
511+
$part = $this->createDataPart($attachment);
512+
if (isset($attachment['part'])) {
513+
$attachment['name'] = $nameRef->getValue($part);
514+
}
515+
506516
foreach ($names as $name) {
507-
if (isset($attachment['part'])) {
508-
continue;
509-
}
510517
if ($name !== $attachment['name']) {
511518
continue;
512519
}
513520
if (isset($inlineParts[$name])) {
514521
continue 2;
515522
}
516-
$attachment['inline'] = true;
517-
$inlineParts[$name] = $part = $this->createDataPart($attachment);
523+
$part->setDisposition('inline');
518524
$html = str_replace('cid:'.$name, 'cid:'.$part->getContentId(), $html);
519525
$part->setName($part->getContentId());
520-
continue 2;
526+
527+
break;
528+
}
529+
530+
if ('inline' === $dispositionRef->getValue($part)) {
531+
$inlineParts[$attachment['name']] = $part;
532+
} else {
533+
$attachmentParts[] = $part;
521534
}
522-
$attachmentParts[] = $this->createDataPart($attachment);
523535
}
524536
if (null !== $htmlPart) {
525537
$htmlPart = new TextPart($html, $this->htmlCharset, 'html');

0 commit comments

Comments
 (0)
0