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

Open in your IDE?
  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.     /**
  34.      * {@inheritDoc}
  35.      */
  36.     public function normalize(mixed $objectstring $format null, array $context = []): array|string|int|float|bool|\ArrayObject|null
  37.     {
  38.         if (!$this->normalizer instanceof NormalizerInterface) {
  39.             throw new \BadMethodCallException(sprintf('The "%s()" method cannot be called as nested normalizer doesn\'t implements "%s".'__METHOD__NormalizerInterface::class));
  40.         }
  41.         $startTime microtime(true);
  42.         $normalized $this->normalizer->normalize($object$format$context);
  43.         $time microtime(true) - $startTime;
  44.         if ($traceId = ($context[TraceableSerializer::DEBUG_TRACE_ID] ?? null)) {
  45.             $this->dataCollector->collectNormalization($traceId\get_class($this->normalizer), $time);
  46.         }
  47.         return $normalized;
  48.     }
  49.     /**
  50.      * {@inheritDoc}
  51.      */
  52.     public function supportsNormalization(mixed $datastring $format null, array $context = []): bool
  53.     {
  54.         if (!$this->normalizer instanceof NormalizerInterface) {
  55.             return false;
  56.         }
  57.         return $this->normalizer->supportsNormalization($data$format$context);
  58.     }
  59.     /**
  60.      * {@inheritDoc}
  61.      */
  62.     public function denormalize(mixed $datastring $typestring $format null, array $context = []): mixed
  63.     {
  64.         if (!$this->normalizer instanceof DenormalizerInterface) {
  65.             throw new \BadMethodCallException(sprintf('The "%s()" method cannot be called as nested normalizer doesn\'t implements "%s".'__METHOD__DenormalizerInterface::class));
  66.         }
  67.         $startTime microtime(true);
  68.         $denormalized $this->normalizer->denormalize($data$type$format$context);
  69.         $time microtime(true) - $startTime;
  70.         if ($traceId = ($context[TraceableSerializer::DEBUG_TRACE_ID] ?? null)) {
  71.             $this->dataCollector->collectDenormalization($traceId\get_class($this->normalizer), $time);
  72.         }
  73.         return $denormalized;
  74.     }
  75.     /**
  76.      * {@inheritDoc}
  77.      */
  78.     public function supportsDenormalization(mixed $datastring $typestring $format null, array $context = []): bool
  79.     {
  80.         if (!$this->normalizer instanceof DenormalizerInterface) {
  81.             return false;
  82.         }
  83.         return $this->normalizer->supportsDenormalization($data$type$format$context);
  84.     }
  85.     /**
  86.      * {@inheritDoc}
  87.      */
  88.     public function setSerializer(SerializerInterface $serializer)
  89.     {
  90.         if (!$this->normalizer instanceof SerializerAwareInterface) {
  91.             return;
  92.         }
  93.         $this->normalizer->setSerializer($serializer);
  94.     }
  95.     /**
  96.      * {@inheritDoc}
  97.      */
  98.     public function setNormalizer(NormalizerInterface $normalizer)
  99.     {
  100.         if (!$this->normalizer instanceof NormalizerAwareInterface) {
  101.             return;
  102.         }
  103.         $this->normalizer->setNormalizer($normalizer);
  104.     }
  105.     /**
  106.      * {@inheritDoc}
  107.      */
  108.     public function setDenormalizer(DenormalizerInterface $denormalizer)
  109.     {
  110.         if (!$this->normalizer instanceof DenormalizerAwareInterface) {
  111.             return;
  112.         }
  113.         $this->normalizer->setDenormalizer($denormalizer);
  114.     }
  115.     /**
  116.      * {@inheritDoc}
  117.      */
  118.     public function hasCacheableSupportsMethod(): bool
  119.     {
  120.         return $this->normalizer instanceof CacheableSupportsMethodInterface && $this->normalizer->hasCacheableSupportsMethod();
  121.     }
  122.     /**
  123.      * Proxies all method calls to the original normalizer.
  124.      */
  125.     public function __call(string $method, array $arguments): mixed
  126.     {
  127.         return $this->normalizer->{$method}(...$arguments);
  128.     }
  129. }