Explicitly mark our normalizers as cachabel or not

This commit is contained in:
Jan Böhmer 2023-03-13 00:35:31 +01:00
parent b38f49a90e
commit a1f4b35749
4 changed files with 39 additions and 12 deletions

View file

@ -22,17 +22,19 @@ namespace App\Serializer;
use Brick\Math\BigDecimal; use Brick\Math\BigDecimal;
use Brick\Math\BigNumber; use Brick\Math\BigNumber;
use Symfony\Component\Serializer\Normalizer\CacheableSupportsMethodInterface;
use Symfony\Component\Serializer\Normalizer\ContextAwareNormalizerInterface; 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; 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) { if (!$object instanceof BigNumber) {
throw new \InvalidArgumentException('This normalizer only supports BigNumber objects!'); throw new \InvalidArgumentException('This normalizer only supports BigNumber objects!');
@ -40,4 +42,9 @@ class BigNumberSerializer implements ContextAwareNormalizerInterface
return (string) $object; return (string) $object;
} }
public function hasCacheableSupportsMethod(): bool
{
return true;
}
} }

View file

@ -23,12 +23,12 @@ namespace App\Serializer;
use App\Entity\Parts\Part; use App\Entity\Parts\Part;
use App\Entity\Parts\PartLot; use App\Entity\Parts\PartLot;
use App\Entity\Parts\Storelocation; use App\Entity\Parts\Storelocation;
use Symfony\Component\Serializer\Normalizer\ContextAwareDenormalizerInterface; use Symfony\Component\Serializer\Normalizer\CacheableSupportsMethodInterface;
use Symfony\Component\Serializer\Normalizer\ContextAwareNormalizerInterface; use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface; use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer; use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
class PartNormalizer implements ContextAwareNormalizerInterface, ContextAwareDenormalizerInterface class PartNormalizer implements NormalizerInterface, DenormalizerInterface, CacheableSupportsMethodInterface
{ {
private NormalizerInterface $normalizer; private NormalizerInterface $normalizer;
@ -40,7 +40,7 @@ class PartNormalizer implements ContextAwareNormalizerInterface, ContextAwareDen
$this->locationDenormalizer = $locationDenormalizer; $this->locationDenormalizer = $locationDenormalizer;
} }
public function supportsNormalization($data, string $format = null, array $context = []) public function supportsNormalization($data, string $format = null): bool
{ {
return $data instanceof Part; return $data instanceof Part;
} }
@ -63,7 +63,7 @@ class PartNormalizer implements ContextAwareNormalizerInterface, ContextAwareDen
return $data; 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); return is_array($data) && is_a($type, Part::class, true);
} }
@ -98,4 +98,10 @@ class PartNormalizer implements ContextAwareNormalizerInterface, ContextAwareDen
return $object; return $object;
} }
public function hasCacheableSupportsMethod(): bool
{
//Must be false, because we rely on is_array($data) in supportsDenormalization()
return false;
}
} }

View file

@ -24,9 +24,11 @@ use App\Entity\Base\AbstractStructuralDBElement;
use App\Form\Type\StructuralEntityType; use App\Form\Type\StructuralEntityType;
use App\Repository\StructuralDBElementRepository; use App\Repository\StructuralDBElementRepository;
use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Serializer\Normalizer\CacheableSupportsMethodInterface;
use Symfony\Component\Serializer\Normalizer\ContextAwareDenormalizerInterface; use Symfony\Component\Serializer\Normalizer\ContextAwareDenormalizerInterface;
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
class StructuralElementFromNameDenormalizer implements ContextAwareDenormalizerInterface class StructuralElementFromNameDenormalizer implements DenormalizerInterface, CacheableSupportsMethodInterface
{ {
private EntityManagerInterface $em; private EntityManagerInterface $em;
@ -35,7 +37,7 @@ class StructuralElementFromNameDenormalizer implements ContextAwareDenormalizerI
$this->em = $em; $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); return is_string($data) && is_subclass_of($type, AbstractStructuralDBElement::class);
} }
@ -66,4 +68,10 @@ class StructuralElementFromNameDenormalizer implements ContextAwareDenormalizerI
} }
return end($elements); return end($elements);
} }
public function hasCacheableSupportsMethod(): bool
{
//Must be false, because we do a is_string check on data in supportsDenormalization
return false;
}
} }

View file

@ -21,12 +21,13 @@
namespace App\Serializer; namespace App\Serializer;
use App\Entity\Base\AbstractStructuralDBElement; use App\Entity\Base\AbstractStructuralDBElement;
use Symfony\Component\Serializer\Normalizer\CacheableSupportsMethodInterface;
use Symfony\Component\Serializer\Normalizer\ContextAwareDenormalizerInterface; use Symfony\Component\Serializer\Normalizer\ContextAwareDenormalizerInterface;
use Symfony\Component\Serializer\Normalizer\ContextAwareNormalizerInterface; use Symfony\Component\Serializer\Normalizer\ContextAwareNormalizerInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface; use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer; use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
class StructuralElementNormalizer implements ContextAwareNormalizerInterface class StructuralElementNormalizer implements NormalizerInterface, CacheableSupportsMethodInterface
{ {
private NormalizerInterface $normalizer; private NormalizerInterface $normalizer;
@ -35,7 +36,7 @@ class StructuralElementNormalizer implements ContextAwareNormalizerInterface
$this->normalizer = $normalizer; $this->normalizer = $normalizer;
} }
public function supportsNormalization($data, string $format = null, array $context = []) public function supportsNormalization($data, string $format = null): bool
{ {
return $data instanceof AbstractStructuralDBElement; return $data instanceof AbstractStructuralDBElement;
} }
@ -57,4 +58,9 @@ class StructuralElementNormalizer implements ContextAwareNormalizerInterface
return $data; return $data;
} }
public function hasCacheableSupportsMethod(): bool
{
return true;
}
} }