Skip to content

Commit e48911d

Browse files
committed
Merge branch '6.1' into 6.2
* 6.1: [LokaliseBridge] Fix push command --delete-missing options when there are no missing messages fix bad help message in cache warmup command [Console] Fix OutputFormatterStyleStack::getCurrent return type Count cookie parts before accessing the second Fix RequestStack state if throwable is thrown [Serializer] Fix caching context-aware encoders/decoders in ChainEncoder/ChainDecoder [Serializer] Revert deprecation of `ContextAwareEncoderInterface` and `ContextAwareDecoderInterface`
2 parents 5892b06 + 03e5987 commit e48911d

15 files changed

+67
-57
lines changed

CHANGELOG.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ CHANGELOG
1616
* Set `Context` annotation as not final
1717
* Deprecate `ContextAwareNormalizerInterface`, use `NormalizerInterface` instead
1818
* Deprecate `ContextAwareDenormalizerInterface`, use `DenormalizerInterface` instead
19-
* Deprecate `ContextAwareEncoderInterface`, use `EncoderInterface` instead
20-
* Deprecate `ContextAwareDecoderInterface`, use `DecoderInterface` instead
2119
* Deprecate supporting denormalization for `AbstractUid` in `UidNormalizer`, use one of `AbstractUid` child class instead
2220
* Deprecate denormalizing to an abstract class in `UidNormalizer`
2321
* Add support for `can*()` methods to `ObjectNormalizer`

Encoder/ChainDecoder.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,13 @@ private function getDecoder(string $format, array $context): DecoderInterface
6161
return $this->decoders[$this->decoderByFormat[$format]];
6262
}
6363

64+
$cache = true;
6465
foreach ($this->decoders as $i => $decoder) {
66+
$cache = $cache && !$decoder instanceof ContextAwareDecoderInterface;
6567
if ($decoder->supportsDecoding($format, $context)) {
66-
$this->decoderByFormat[$format] = $i;
68+
if ($cache) {
69+
$this->decoderByFormat[$format] = $i;
70+
}
6771

6872
return $decoder;
6973
}

Encoder/ChainEncoder.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,13 @@ private function getEncoder(string $format, array $context): EncoderInterface
8484
return $this->encoders[$this->encoderByFormat[$format]];
8585
}
8686

87+
$cache = true;
8788
foreach ($this->encoders as $i => $encoder) {
89+
$cache = $cache && !$encoder instanceof ContextAwareEncoderInterface;
8890
if ($encoder->supportsEncoding($format, $context)) {
89-
$this->encoderByFormat[$format] = $i;
91+
if ($cache) {
92+
$this->encoderByFormat[$format] = $i;
93+
}
9094

9195
return $encoder;
9296
}

Encoder/ContextAwareDecoderInterface.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@
1515
* Adds the support of an extra $context parameter for the supportsDecoding method.
1616
*
1717
* @author Kévin Dunglas <[email protected]>
18-
*
19-
* @deprecated since symfony/serializer 6.1, use DecoderInterface instead
2018
*/
2119
interface ContextAwareDecoderInterface extends DecoderInterface
2220
{

Encoder/ContextAwareEncoderInterface.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@
1515
* Adds the support of an extra $context parameter for the supportsEncoding method.
1616
*
1717
* @author Kévin Dunglas <[email protected]>
18-
*
19-
* @deprecated since symfony/serializer 6.1, use EncoderInterface instead
2018
*/
2119
interface ContextAwareEncoderInterface extends EncoderInterface
2220
{

Encoder/CsvEncoder.php

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -119,10 +119,7 @@ public function encode(mixed $data, string $format, array $context = []): string
119119
return $value;
120120
}
121121

122-
/**
123-
* @param array $context
124-
*/
125-
public function supportsEncoding(string $format /* , array $context = [] */): bool
122+
public function supportsEncoding(string $format): bool
126123
{
127124
return self::FORMAT === $format;
128125
}
@@ -202,10 +199,7 @@ public function decode(string $data, string $format, array $context = []): mixed
202199
return $result[0];
203200
}
204201

205-
/**
206-
* @param array $context
207-
*/
208-
public function supportsDecoding(string $format /* , array $context = [] */): bool
202+
public function supportsDecoding(string $format): bool
209203
{
210204
return self::FORMAT === $format;
211205
}

Encoder/DecoderInterface.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,9 @@ public function decode(string $data, string $format, array $context = []);
3939
/**
4040
* Checks whether the deserializer can decode from given format.
4141
*
42-
* @param string $format Format name
43-
* @param array $context Options that decoders have access to
42+
* @param string $format Format name
4443
*
4544
* @return bool
4645
*/
47-
public function supportsDecoding(string $format /* , array $context = [] */);
46+
public function supportsDecoding(string $format);
4847
}

Encoder/EncoderInterface.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,7 @@ public function encode(mixed $data, string $format, array $context = []): string
3232
/**
3333
* Checks whether the serializer can encode to given format.
3434
*
35-
* @param string $format Format name
36-
* @param array $context Options that normalizers/encoders have access to
35+
* @param string $format Format name
3736
*/
38-
public function supportsEncoding(string $format /* , array $context = [] */): bool;
37+
public function supportsEncoding(string $format): bool;
3938
}

Encoder/JsonDecode.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,10 +93,7 @@ public function decode(string $data, string $format, array $context = []): mixed
9393
return $decodedData;
9494
}
9595

96-
/**
97-
* @param array $context
98-
*/
99-
public function supportsDecoding(string $format /* , array $context = [] */): bool
96+
public function supportsDecoding(string $format): bool
10097
{
10198
return JsonEncoder::FORMAT === $format;
10299
}

Encoder/JsonEncode.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,7 @@ public function encode(mixed $data, string $format, array $context = []): string
5252
return $encodedJson;
5353
}
5454

55-
/**
56-
* @param array $context
57-
*/
58-
public function supportsEncoding(string $format /* , array $context = [] */): bool
55+
public function supportsEncoding(string $format): bool
5956
{
6057
return JsonEncoder::FORMAT === $format;
6158
}

Encoder/JsonEncoder.php

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,18 +39,12 @@ public function decode(string $data, string $format, array $context = []): mixed
3939
return $this->decodingImpl->decode($data, self::FORMAT, $context);
4040
}
4141

42-
/**
43-
* @param array $context
44-
*/
45-
public function supportsEncoding(string $format /* , array $context = [] */): bool
42+
public function supportsEncoding(string $format): bool
4643
{
4744
return self::FORMAT === $format;
4845
}
4946

50-
/**
51-
* @param array $context
52-
*/
53-
public function supportsDecoding(string $format /* , array $context = [] */): bool
47+
public function supportsDecoding(string $format): bool
5448
{
5549
return self::FORMAT === $format;
5650
}

Encoder/XmlEncoder.php

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -158,18 +158,12 @@ public function decode(string $data, string $format, array $context = []): mixed
158158
return $data;
159159
}
160160

161-
/**
162-
* @param array $context
163-
*/
164-
public function supportsEncoding(string $format /* , array $context = [] */): bool
161+
public function supportsEncoding(string $format): bool
165162
{
166163
return self::FORMAT === $format;
167164
}
168165

169-
/**
170-
* @param array $context
171-
*/
172-
public function supportsDecoding(string $format /* , array $context = [] */): bool
166+
public function supportsDecoding(string $format): bool
173167
{
174168
return self::FORMAT === $format;
175169
}

Encoder/YamlEncoder.php

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,7 @@ public function encode(mixed $data, string $format, array $context = []): string
6262
return $this->dumper->dump($data, $context[self::YAML_INLINE], $context[self::YAML_INDENT], $context[self::YAML_FLAGS]);
6363
}
6464

65-
/**
66-
* @param array $context
67-
*/
68-
public function supportsEncoding(string $format /* , array $context = [] */): bool
65+
public function supportsEncoding(string $format): bool
6966
{
7067
return self::FORMAT === $format || self::ALTERNATIVE_FORMAT === $format;
7168
}
@@ -77,10 +74,7 @@ public function decode(string $data, string $format, array $context = []): mixed
7774
return $this->parser->parse($data, $context[self::YAML_FLAGS]);
7875
}
7976

80-
/**
81-
* @param array $context
82-
*/
83-
public function supportsDecoding(string $format /* , array $context = [] */): bool
77+
public function supportsDecoding(string $format): bool
8478
{
8579
return self::FORMAT === $format || self::ALTERNATIVE_FORMAT === $format;
8680
}

Tests/Encoder/ChainDecoderTest.php

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use PHPUnit\Framework\TestCase;
1515
use Symfony\Component\Serializer\Encoder\ChainDecoder;
16+
use Symfony\Component\Serializer\Encoder\ContextAwareDecoderInterface;
1617
use Symfony\Component\Serializer\Encoder\DecoderInterface;
1718
use Symfony\Component\Serializer\Exception\RuntimeException;
1819

@@ -28,14 +29,15 @@ class ChainDecoderTest extends TestCase
2829

2930
protected function setUp(): void
3031
{
31-
$this->decoder1 = $this->createMock(DecoderInterface::class);
32+
$this->decoder1 = $this->createMock(ContextAwareDecoderInterface::class);
3233
$this->decoder1
3334
->method('supportsDecoding')
3435
->willReturnMap([
3536
[self::FORMAT_1, [], true],
3637
[self::FORMAT_2, [], false],
3738
[self::FORMAT_3, [], false],
3839
[self::FORMAT_3, ['foo' => 'bar'], true],
40+
[self::FORMAT_3, ['foo' => 'bar2'], false],
3941
]);
4042

4143
$this->decoder2 = $this->createMock(DecoderInterface::class);
@@ -45,17 +47,35 @@ protected function setUp(): void
4547
[self::FORMAT_1, [], false],
4648
[self::FORMAT_2, [], true],
4749
[self::FORMAT_3, [], false],
50+
[self::FORMAT_3, ['foo' => 'bar'], false],
51+
[self::FORMAT_3, ['foo' => 'bar2'], true],
4852
]);
4953

5054
$this->chainDecoder = new ChainDecoder([$this->decoder1, $this->decoder2]);
5155
}
5256

5357
public function testSupportsDecoding()
5458
{
59+
$this->decoder1
60+
->method('decode')
61+
->willReturn('result1');
62+
$this->decoder2
63+
->method('decode')
64+
->willReturn('result2');
65+
5566
$this->assertTrue($this->chainDecoder->supportsDecoding(self::FORMAT_1));
67+
$this->assertEquals('result1', $this->chainDecoder->decode('', self::FORMAT_1, []));
68+
5669
$this->assertTrue($this->chainDecoder->supportsDecoding(self::FORMAT_2));
70+
$this->assertEquals('result2', $this->chainDecoder->decode('', self::FORMAT_2, []));
71+
5772
$this->assertFalse($this->chainDecoder->supportsDecoding(self::FORMAT_3));
73+
5874
$this->assertTrue($this->chainDecoder->supportsDecoding(self::FORMAT_3, ['foo' => 'bar']));
75+
$this->assertEquals('result1', $this->chainDecoder->decode('', self::FORMAT_3, ['foo' => 'bar']));
76+
77+
$this->assertTrue($this->chainDecoder->supportsDecoding(self::FORMAT_3, ['foo' => 'bar2']));
78+
$this->assertEquals('result2', $this->chainDecoder->decode('', self::FORMAT_3, ['foo' => 'bar2']));
5979
}
6080

6181
public function testDecode()

Tests/Encoder/ChainEncoderTest.php

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use PHPUnit\Framework\TestCase;
1515
use Symfony\Component\Serializer\Debug\TraceableEncoder;
1616
use Symfony\Component\Serializer\Encoder\ChainEncoder;
17+
use Symfony\Component\Serializer\Encoder\ContextAwareEncoderInterface;
1718
use Symfony\Component\Serializer\Encoder\EncoderInterface;
1819
use Symfony\Component\Serializer\Encoder\NormalizationAwareInterface;
1920
use Symfony\Component\Serializer\Exception\RuntimeException;
@@ -30,14 +31,15 @@ class ChainEncoderTest extends TestCase
3031

3132
protected function setUp(): void
3233
{
33-
$this->encoder1 = $this->createMock(EncoderInterface::class);
34+
$this->encoder1 = $this->createMock(ContextAwareEncoderInterface::class);
3435
$this->encoder1
3536
->method('supportsEncoding')
3637
->willReturnMap([
3738
[self::FORMAT_1, [], true],
3839
[self::FORMAT_2, [], false],
3940
[self::FORMAT_3, [], false],
4041
[self::FORMAT_3, ['foo' => 'bar'], true],
42+
[self::FORMAT_3, ['foo' => 'bar2'], false],
4143
]);
4244

4345
$this->encoder2 = $this->createMock(EncoderInterface::class);
@@ -47,17 +49,35 @@ protected function setUp(): void
4749
[self::FORMAT_1, [], false],
4850
[self::FORMAT_2, [], true],
4951
[self::FORMAT_3, [], false],
52+
[self::FORMAT_3, ['foo' => 'bar'], false],
53+
[self::FORMAT_3, ['foo' => 'bar2'], true],
5054
]);
5155

5256
$this->chainEncoder = new ChainEncoder([$this->encoder1, $this->encoder2]);
5357
}
5458

5559
public function testSupportsEncoding()
5660
{
61+
$this->encoder1
62+
->method('encode')
63+
->willReturn('result1');
64+
$this->encoder2
65+
->method('encode')
66+
->willReturn('result2');
67+
5768
$this->assertTrue($this->chainEncoder->supportsEncoding(self::FORMAT_1));
69+
$this->assertEquals('result1', $this->chainEncoder->encode('', self::FORMAT_1, []));
70+
5871
$this->assertTrue($this->chainEncoder->supportsEncoding(self::FORMAT_2));
72+
$this->assertEquals('result2', $this->chainEncoder->encode('', self::FORMAT_2, []));
73+
5974
$this->assertFalse($this->chainEncoder->supportsEncoding(self::FORMAT_3));
75+
6076
$this->assertTrue($this->chainEncoder->supportsEncoding(self::FORMAT_3, ['foo' => 'bar']));
77+
$this->assertEquals('result1', $this->chainEncoder->encode('', self::FORMAT_3, ['foo' => 'bar']));
78+
79+
$this->assertTrue($this->chainEncoder->supportsEncoding(self::FORMAT_3, ['foo' => 'bar2']));
80+
$this->assertEquals('result2', $this->chainEncoder->encode('', self::FORMAT_3, ['foo' => 'bar2']));
6181
}
6282

6383
public function testEncode()
@@ -106,7 +126,7 @@ public function testNeedsNormalizationTraceableEncoder()
106126

107127
class NormalizationAwareEncoder implements EncoderInterface, NormalizationAwareInterface
108128
{
109-
public function supportsEncoding(string $format, array $context = []): bool
129+
public function supportsEncoding(string $format): bool
110130
{
111131
return true;
112132
}

0 commit comments

Comments
 (0)