Deprecated: Constant E_STRICT is deprecated in /home/normanv/www/annuairepro/vendor/symfony/error-handler/ErrorHandler.php on line 58

Deprecated: Constant E_STRICT is deprecated in /home/normanv/www/annuairepro/vendor/symfony/error-handler/ErrorHandler.php on line 76
Symfony Profiler

vendor/symfony/serializer/Mapping/Loader/AnnotationLoader.php line 46

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\Mapping\Loader;
  11. use Doctrine\Common\Annotations\Reader;
  12. use Symfony\Component\Serializer\Annotation\Context;
  13. use Symfony\Component\Serializer\Annotation\DiscriminatorMap;
  14. use Symfony\Component\Serializer\Annotation\Groups;
  15. use Symfony\Component\Serializer\Annotation\Ignore;
  16. use Symfony\Component\Serializer\Annotation\MaxDepth;
  17. use Symfony\Component\Serializer\Annotation\SerializedName;
  18. use Symfony\Component\Serializer\Exception\MappingException;
  19. use Symfony\Component\Serializer\Mapping\AttributeMetadata;
  20. use Symfony\Component\Serializer\Mapping\AttributeMetadataInterface;
  21. use Symfony\Component\Serializer\Mapping\ClassDiscriminatorMapping;
  22. use Symfony\Component\Serializer\Mapping\ClassMetadataInterface;
  23. /**
  24.  * Loader for Doctrine annotations and PHP 8 attributes.
  25.  *
  26.  * @author Kévin Dunglas <dunglas@gmail.com>
  27.  * @author Alexander M. Turek <me@derrabus.de>
  28.  */
  29. class AnnotationLoader implements LoaderInterface
  30. {
  31.     private const KNOWN_ANNOTATIONS = [
  32.         DiscriminatorMap::class,
  33.         Groups::class,
  34.         Ignore::class,
  35.         MaxDepth::class,
  36.         SerializedName::class,
  37.         Context::class,
  38.     ];
  39.     private $reader;
  40.     public function __construct(Reader $reader null)
  41.     {
  42.         $this->reader $reader;
  43.     }
  44.     /**
  45.      * {@inheritdoc}
  46.      */
  47.     public function loadClassMetadata(ClassMetadataInterface $classMetadata)
  48.     {
  49.         $reflectionClass $classMetadata->getReflectionClass();
  50.         $className $reflectionClass->name;
  51.         $loaded false;
  52.         $attributesMetadata $classMetadata->getAttributesMetadata();
  53.         foreach ($this->loadAnnotations($reflectionClass) as $annotation) {
  54.             if ($annotation instanceof DiscriminatorMap) {
  55.                 $classMetadata->setClassDiscriminatorMapping(new ClassDiscriminatorMapping(
  56.                     $annotation->getTypeProperty(),
  57.                     $annotation->getMapping()
  58.                 ));
  59.             }
  60.         }
  61.         foreach ($reflectionClass->getProperties() as $property) {
  62.             if (!isset($attributesMetadata[$property->name])) {
  63.                 $attributesMetadata[$property->name] = new AttributeMetadata($property->name);
  64.                 $classMetadata->addAttributeMetadata($attributesMetadata[$property->name]);
  65.             }
  66.             if ($property->getDeclaringClass()->name === $className) {
  67.                 foreach ($this->loadAnnotations($property) as $annotation) {
  68.                     if ($annotation instanceof Groups) {
  69.                         foreach ($annotation->getGroups() as $group) {
  70.                             $attributesMetadata[$property->name]->addGroup($group);
  71.                         }
  72.                     } elseif ($annotation instanceof MaxDepth) {
  73.                         $attributesMetadata[$property->name]->setMaxDepth($annotation->getMaxDepth());
  74.                     } elseif ($annotation instanceof SerializedName) {
  75.                         $attributesMetadata[$property->name]->setSerializedName($annotation->getSerializedName());
  76.                     } elseif ($annotation instanceof Ignore) {
  77.                         $attributesMetadata[$property->name]->setIgnore(true);
  78.                     } elseif ($annotation instanceof Context) {
  79.                         $this->setAttributeContextsForGroups($annotation$attributesMetadata[$property->name]);
  80.                     }
  81.                     $loaded true;
  82.                 }
  83.             }
  84.         }
  85.         foreach ($reflectionClass->getMethods() as $method) {
  86.             if ($method->getDeclaringClass()->name !== $className) {
  87.                 continue;
  88.             }
  89.             if (=== stripos($method->name'get') && $method->getNumberOfRequiredParameters()) {
  90.                 continue; /*  matches the BC behavior in `Symfony\Component\Serializer\Normalizer\ObjectNormalizer::extractAttributes` */
  91.             }
  92.             $accessorOrMutator preg_match('/^(get|is|has|set)(.+)$/i'$method->name$matches);
  93.             if ($accessorOrMutator) {
  94.                 $attributeName lcfirst($matches[2]);
  95.                 if (isset($attributesMetadata[$attributeName])) {
  96.                     $attributeMetadata $attributesMetadata[$attributeName];
  97.                 } else {
  98.                     $attributesMetadata[$attributeName] = $attributeMetadata = new AttributeMetadata($attributeName);
  99.                     $classMetadata->addAttributeMetadata($attributeMetadata);
  100.                 }
  101.             }
  102.             foreach ($this->loadAnnotations($method) as $annotation) {
  103.                 if ($annotation instanceof Groups) {
  104.                     if (!$accessorOrMutator) {
  105.                         throw new MappingException(sprintf('Groups on "%s::%s()" cannot be added. Groups can only be added on methods beginning with "get", "is", "has" or "set".'$className$method->name));
  106.                     }
  107.                     foreach ($annotation->getGroups() as $group) {
  108.                         $attributeMetadata->addGroup($group);
  109.                     }
  110.                 } elseif ($annotation instanceof MaxDepth) {
  111.                     if (!$accessorOrMutator) {
  112.                         throw new MappingException(sprintf('MaxDepth on "%s::%s()" cannot be added. MaxDepth can only be added on methods beginning with "get", "is", "has" or "set".'$className$method->name));
  113.                     }
  114.                     $attributeMetadata->setMaxDepth($annotation->getMaxDepth());
  115.                 } elseif ($annotation instanceof SerializedName) {
  116.                     if (!$accessorOrMutator) {
  117.                         throw new MappingException(sprintf('SerializedName on "%s::%s()" cannot be added. SerializedName can only be added on methods beginning with "get", "is", "has" or "set".'$className$method->name));
  118.                     }
  119.                     $attributeMetadata->setSerializedName($annotation->getSerializedName());
  120.                 } elseif ($annotation instanceof Ignore) {
  121.                     if (!$accessorOrMutator) {
  122.                         throw new MappingException(sprintf('Ignore on "%s::%s()" cannot be added. Ignore can only be added on methods beginning with "get", "is", "has" or "set".'$className$method->name));
  123.                     }
  124.                     $attributeMetadata->setIgnore(true);
  125.                 } elseif ($annotation instanceof Context) {
  126.                     if (!$accessorOrMutator) {
  127.                         throw new MappingException(sprintf('Context on "%s::%s()" cannot be added. Context can only be added on methods beginning with "get", "is", "has" or "set".'$className$method->name));
  128.                     }
  129.                     $this->setAttributeContextsForGroups($annotation$attributeMetadata);
  130.                 }
  131.                 $loaded true;
  132.             }
  133.         }
  134.         return $loaded;
  135.     }
  136.     /**
  137.      * @param \ReflectionClass|\ReflectionMethod|\ReflectionProperty $reflector
  138.      */
  139.     public function loadAnnotations(object $reflector): iterable
  140.     {
  141.         if (\PHP_VERSION_ID >= 80000) {
  142.             foreach ($reflector->getAttributes() as $attribute) {
  143.                 if ($this->isKnownAttribute($attribute->getName())) {
  144.                     yield $attribute->newInstance();
  145.                 }
  146.             }
  147.         }
  148.         if (null === $this->reader) {
  149.             return;
  150.         }
  151.         if ($reflector instanceof \ReflectionClass) {
  152.             yield from $this->reader->getClassAnnotations($reflector);
  153.         }
  154.         if ($reflector instanceof \ReflectionMethod) {
  155.             yield from $this->reader->getMethodAnnotations($reflector);
  156.         }
  157.         if ($reflector instanceof \ReflectionProperty) {
  158.             yield from $this->reader->getPropertyAnnotations($reflector);
  159.         }
  160.     }
  161.     private function setAttributeContextsForGroups(Context $annotationAttributeMetadataInterface $attributeMetadata): void
  162.     {
  163.         if ($annotation->getContext()) {
  164.             $attributeMetadata->setNormalizationContextForGroups($annotation->getContext(), $annotation->getGroups());
  165.             $attributeMetadata->setDenormalizationContextForGroups($annotation->getContext(), $annotation->getGroups());
  166.         }
  167.         if ($annotation->getNormalizationContext()) {
  168.             $attributeMetadata->setNormalizationContextForGroups($annotation->getNormalizationContext(), $annotation->getGroups());
  169.         }
  170.         if ($annotation->getDenormalizationContext()) {
  171.             $attributeMetadata->setDenormalizationContextForGroups($annotation->getDenormalizationContext(), $annotation->getGroups());
  172.         }
  173.     }
  174.     private function isKnownAttribute(string $attributeName): bool
  175.     {
  176.         foreach (self::KNOWN_ANNOTATIONS as $knownAnnotation) {
  177.             if (is_a($attributeName$knownAnnotationtrue)) {
  178.                 return true;
  179.             }
  180.         }
  181.         return false;
  182.     }
  183. }