vendor/symfony/serializer/Debug/TraceableNormalizer.php line 45

  1. <?php
  2. /*
  3.  * This file is part of the Symfony package.
  4.  *
  5.  * (c) Fabien Potencier <fabien@symfony.com>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace Symfony\Component\Serializer\Debug;
  11. use Symfony\Component\Serializer\DataCollector\SerializerDataCollector;
  12. use Symfony\Component\Serializer\Normalizer\CacheableSupportsMethodInterface;
  13. use Symfony\Component\Serializer\Normalizer\DenormalizerAwareInterface;
  14. use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
  15. use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface;
  16. use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
  17. use Symfony\Component\Serializer\SerializerAwareInterface;
  18. use Symfony\Component\Serializer\SerializerInterface;
  19. /**
  20.  * Collects some data about normalization.
  21.  *
  22.  * @author Mathias Arlaud <mathias.arlaud@gmail.com>
  23.  *
  24.  * @internal
  25.  */
  26. class TraceableNormalizer implements NormalizerInterfaceDenormalizerInterfaceSerializerAwareInterfaceNormalizerAwareInterfaceDenormalizerAwareInterfaceCacheableSupportsMethodInterface
  27. {
  28.     public function __construct(
  29.         private NormalizerInterface|DenormalizerInterface $normalizer,
  30.         private SerializerDataCollector $dataCollector,
  31.     ) {
  32.     }
  33.     public function normalize(mixed $objectstring $format null, array $context = []): array|string|int|float|bool|\ArrayObject|null
  34.     {
  35.         if (!$this->normalizer instanceof NormalizerInterface) {
  36.             throw new \BadMethodCallException(sprintf('The "%s()" method cannot be called as nested normalizer doesn\'t implements "%s".'__METHOD__NormalizerInterface::class));
  37.         }
  38.         $startTime microtime(true);
  39.         $normalized $this->normalizer->normalize($object$format$context);
  40.         $time microtime(true) - $startTime;
  41.         if ($traceId = ($context[TraceableSerializer::DEBUG_TRACE_ID] ?? null)) {
  42.             $this->dataCollector->collectNormalization($traceId\get_class($this->normalizer), $time);
  43.         }
  44.         return $normalized;
  45.     }
  46.     public function supportsNormalization(mixed $datastring $format null, array $context = []): bool
  47.     {
  48.         if (!$this->normalizer instanceof NormalizerInterface) {
  49.             return false;
  50.         }
  51.         return $this->normalizer->supportsNormalization($data$format$context);
  52.     }
  53.     public function denormalize(mixed $datastring $typestring $format null, array $context = []): mixed
  54.     {
  55.         if (!$this->normalizer instanceof DenormalizerInterface) {
  56.             throw new \BadMethodCallException(sprintf('The "%s()" method cannot be called as nested normalizer doesn\'t implements "%s".'__METHOD__DenormalizerInterface::class));
  57.         }
  58.         $startTime microtime(true);
  59.         $denormalized $this->normalizer->denormalize($data$type$format$context);
  60.         $time microtime(true) - $startTime;
  61.         if ($traceId = ($context[TraceableSerializer::DEBUG_TRACE_ID] ?? null)) {
  62.             $this->dataCollector->collectDenormalization($traceId\get_class($this->normalizer), $time);
  63.         }
  64.         return $denormalized;
  65.     }
  66.     public function supportsDenormalization(mixed $datastring $typestring $format null, array $context = []): bool
  67.     {
  68.         if (!$this->normalizer instanceof DenormalizerInterface) {
  69.             return false;
  70.         }
  71.         return $this->normalizer->supportsDenormalization($data$type$format$context);
  72.     }
  73.     public function setSerializer(SerializerInterface $serializer)
  74.     {
  75.         if (!$this->normalizer instanceof SerializerAwareInterface) {
  76.             return;
  77.         }
  78.         $this->normalizer->setSerializer($serializer);
  79.     }
  80.     public function setNormalizer(NormalizerInterface $normalizer)
  81.     {
  82.         if (!$this->normalizer instanceof NormalizerAwareInterface) {
  83.             return;
  84.         }
  85.         $this->normalizer->setNormalizer($normalizer);
  86.     }
  87.     public function setDenormalizer(DenormalizerInterface $denormalizer)
  88.     {
  89.         if (!$this->normalizer instanceof DenormalizerAwareInterface) {
  90.             return;
  91.         }
  92.         $this->normalizer->setDenormalizer($denormalizer);
  93.     }
  94.     public function hasCacheableSupportsMethod(): bool
  95.     {
  96.         return $this->normalizer instanceof CacheableSupportsMethodInterface && $this->normalizer->hasCacheableSupportsMethod();
  97.     }
  98.     /**
  99.      * Proxies all method calls to the original normalizer.
  100.      */
  101.     public function __call(string $method, array $arguments): mixed
  102.     {
  103.         return $this->normalizer->{$method}(...$arguments);
  104.     }
  105. }