8000 [Serializer] ensure ChainEncoder/ChainDecoder properly consider the context by Guite · Pull Request #43231 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Serializer] ensure ChainEncoder/ChainDecoder properly consider the context #43231

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
keep caching mechanism unless dynamic context is given
  • Loading branch information
Guite committed Oct 1, 2021
commit 42f0d6ec3ad535ee639e01c7f8a8468f4627c919
15 changes: 15 additions & 0 deletions src/Symfony/Component/Serializer/Encoder/ChainDecoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
class ChainDecoder implements ContextAwareDecoderInterface
{
protected $decoders = [];
protected $decoderByFormat = [];

public function __construct(array $decoders = [])
{
Expand Down Expand Up @@ -60,8 +61,22 @@ public function supportsDecoding($format, array $context = []): bool
*/
public function getDecoder(string $format, array $context): DecoderInterface
{
$hasContext = 0 < \count($context);
if (
true !== $hasContext
&& isset($this->decoderByFormat[$format])
&& isset($this->decoders[$this->decoderByFormat[$format]])
) {
return $this->decoders[$this->decoderByFormat[$format]];
}

foreach ($this->decoders as $i => $decoder) {
if ($decoder->supportsDecoding($format, $context)) {
if (true !== $hasContext) {
// cache decoder if no dynamic context is given
$this->decoderByFormat[$format] = $i;
}

return $decoder;
}
}
Expand Down
15 changes: 15 additions & 0 deletions src/Symfony/Component/Serializer/Encoder/ChainEncoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
class ChainEncoder implements ContextAwareEncoderInterface
{
protected $encoders = [];
protected $encoderByFormat = [];

public function __construct(array $encoders = [])
{
Expand Down Expand Up @@ -78,8 +79,22 @@ public function needsNormalization(string $format, array $context = []): bool
*/
public function getEncoder(string $format, array $context): EncoderInterface
{
$hasContext = 0 < \count($context);
if (
true !== $hasContext
&& isset($this->encoderByFormat[$format])
&& isset($this->encoders[$this->encoderByFormat[$format]])
) {
return $this->encoders[$this->encoderByFormat[$format]];
}

foreach ($this->encoders as $i => $encoder) {
if ($encoder->supportsEncoding($format, $context)) {
if (true !== $hasContext) {
// cache encoder if no dynamic context is given
$this->encoderByFormat[$format] = $i;
}

return $encoder;
}
}
Expand Down
0