From a1f4b35749574baf1bf8d57445124c81ac5b5d25 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20B=C3=B6hmer?= Date: Mon, 13 Mar 2023 00:35:31 +0100 Subject: [PATCH] Explicitly mark our normalizers as cachabel or not --- src/Serializer/BigNumberSerializer.php | 13 ++++++++++--- src/Serializer/PartNormalizer.php | 16 +++++++++++----- .../StructuralElementFromNameDenormalizer.php | 12 ++++++++++-- src/Serializer/StructuralElementNormalizer.php | 10 ++++++++-- 4 files changed, 39 insertions(+), 12 deletions(-) diff --git a/src/Serializer/BigNumberSerializer.php b/src/Serializer/BigNumberSerializer.php index 0ea479af..afdcaee6 100644 --- a/src/Serializer/BigNumberSerializer.php +++ b/src/Serializer/BigNumberSerializer.php @@ -22,17 +22,19 @@ namespace App\Serializer; use Brick\Math\BigDecimal; use Brick\Math\BigNumber; +use Symfony\Component\Serializer\Normalizer\CacheableSupportsMethodInterface; use Symfony\Component\Serializer\Normalizer\ContextAwareNormalizerInterface; +use Symfony\Component\Serializer\Normalizer\NormalizerInterface; -class BigNumberSerializer implements ContextAwareNormalizerInterface +class BigNumberSerializer implements NormalizerInterface, CacheableSupportsMethodInterface { - public function supportsNormalization($data, string $format = null, array $context = []) + public function supportsNormalization($data, string $format = null): bool { return $data instanceof BigNumber; } - public function normalize($object, string $format = null, array $context = []) + public function normalize($object, string $format = null, array $context = []): string { if (!$object instanceof BigNumber) { throw new \InvalidArgumentException('This normalizer only supports BigNumber objects!'); @@ -40,4 +42,9 @@ class BigNumberSerializer implements ContextAwareNormalizerInterface return (string) $object; } + + public function hasCacheableSupportsMethod(): bool + { + return true; + } } \ No newline at end of file diff --git a/src/Serializer/PartNormalizer.php b/src/Serializer/PartNormalizer.php index c89eb0b3..54846684 100644 --- a/src/Serializer/PartNormalizer.php +++ b/src/Serializer/PartNormalizer.php @@ -23,12 +23,12 @@ namespace App\Serializer; use App\Entity\Parts\Part; use App\Entity\Parts\PartLot; use App\Entity\Parts\Storelocation; -use Symfony\Component\Serializer\Normalizer\ContextAwareDenormalizerInterface; -use Symfony\Component\Serializer\Normalizer\ContextAwareNormalizerInterface; +use Symfony\Component\Serializer\Normalizer\CacheableSupportsMethodInterface; +use Symfony\Component\Serializer\Normalizer\DenormalizerInterface; use Symfony\Component\Serializer\Normalizer\NormalizerInterface; use Symfony\Component\Serializer\Normalizer\ObjectNormalizer; -class PartNormalizer implements ContextAwareNormalizerInterface, ContextAwareDenormalizerInterface +class PartNormalizer implements NormalizerInterface, DenormalizerInterface, CacheableSupportsMethodInterface { private NormalizerInterface $normalizer; @@ -40,7 +40,7 @@ class PartNormalizer implements ContextAwareNormalizerInterface, ContextAwareDen $this->locationDenormalizer = $locationDenormalizer; } - public function supportsNormalization($data, string $format = null, array $context = []) + public function supportsNormalization($data, string $format = null): bool { return $data instanceof Part; } @@ -63,7 +63,7 @@ class PartNormalizer implements ContextAwareNormalizerInterface, ContextAwareDen return $data; } - public function supportsDenormalization($data, string $type, string $format = null, array $context = []) + public function supportsDenormalization($data, string $type, string $format = null): bool { return is_array($data) && is_a($type, Part::class, true); } @@ -98,4 +98,10 @@ class PartNormalizer implements ContextAwareNormalizerInterface, ContextAwareDen return $object; } + + public function hasCacheableSupportsMethod(): bool + { + //Must be false, because we rely on is_array($data) in supportsDenormalization() + return false; + } } \ No newline at end of file diff --git a/src/Serializer/StructuralElementFromNameDenormalizer.php b/src/Serializer/StructuralElementFromNameDenormalizer.php index 37c9bfbe..ac035946 100644 --- a/src/Serializer/StructuralElementFromNameDenormalizer.php +++ b/src/Serializer/StructuralElementFromNameDenormalizer.php @@ -24,9 +24,11 @@ use App\Entity\Base\AbstractStructuralDBElement; use App\Form\Type\StructuralEntityType; use App\Repository\StructuralDBElementRepository; use Doctrine\ORM\EntityManagerInterface; +use Symfony\Component\Serializer\Normalizer\CacheableSupportsMethodInterface; use Symfony\Component\Serializer\Normalizer\ContextAwareDenormalizerInterface; +use Symfony\Component\Serializer\Normalizer\DenormalizerInterface; -class StructuralElementFromNameDenormalizer implements ContextAwareDenormalizerInterface +class StructuralElementFromNameDenormalizer implements DenormalizerInterface, CacheableSupportsMethodInterface { private EntityManagerInterface $em; @@ -35,7 +37,7 @@ class StructuralElementFromNameDenormalizer implements ContextAwareDenormalizerI $this->em = $em; } - public function supportsDenormalization($data, string $type, string $format = null, array $context = []) + public function supportsDenormalization($data, string $type, string $format = null) { return is_string($data) && is_subclass_of($type, AbstractStructuralDBElement::class); } @@ -66,4 +68,10 @@ class StructuralElementFromNameDenormalizer implements ContextAwareDenormalizerI } return end($elements); } + + public function hasCacheableSupportsMethod(): bool + { + //Must be false, because we do a is_string check on data in supportsDenormalization + return false; + } } \ No newline at end of file diff --git a/src/Serializer/StructuralElementNormalizer.php b/src/Serializer/StructuralElementNormalizer.php index ae880b93..c412fabd 100644 --- a/src/Serializer/StructuralElementNormalizer.php +++ b/src/Serializer/StructuralElementNormalizer.php @@ -21,12 +21,13 @@ namespace App\Serializer; use App\Entity\Base\AbstractStructuralDBElement; +use Symfony\Component\Serializer\Normalizer\CacheableSupportsMethodInterface; use Symfony\Component\Serializer\Normalizer\ContextAwareDenormalizerInterface; use Symfony\Component\Serializer\Normalizer\ContextAwareNormalizerInterface; use Symfony\Component\Serializer\Normalizer\NormalizerInterface; use Symfony\Component\Serializer\Normalizer\ObjectNormalizer; -class StructuralElementNormalizer implements ContextAwareNormalizerInterface +class StructuralElementNormalizer implements NormalizerInterface, CacheableSupportsMethodInterface { private NormalizerInterface $normalizer; @@ -35,7 +36,7 @@ class StructuralElementNormalizer implements ContextAwareNormalizerInterface $this->normalizer = $normalizer; } - public function supportsNormalization($data, string $format = null, array $context = []) + public function supportsNormalization($data, string $format = null): bool { return $data instanceof AbstractStructuralDBElement; } @@ -57,4 +58,9 @@ class StructuralElementNormalizer implements ContextAwareNormalizerInterface return $data; } + + public function hasCacheableSupportsMethod(): bool + { + return true; + } } \ No newline at end of file