vendor/symfony/validator/Mapping/Factory/LazyLoadingMetadataFactory.php line 52

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\Validator\Mapping\Factory;
  11. use Psr\Cache\CacheItemPoolInterface;
  12. use Symfony\Component\Validator\Exception\NoSuchMetadataException;
  13. use Symfony\Component\Validator\Mapping\ClassMetadata;
  14. use Symfony\Component\Validator\Mapping\Loader\LoaderInterface;
  15. use Symfony\Component\Validator\Mapping\MetadataInterface;
  16. /**
  17.  * Creates new {@link ClassMetadataInterface} instances.
  18.  *
  19.  * Whenever {@link getMetadataFor()} is called for the first time with a given
  20.  * class name or object of that class, a new metadata instance is created and
  21.  * returned. On subsequent requests for the same class, the same metadata
  22.  * instance will be returned.
  23.  *
  24.  * You can optionally pass a {@link LoaderInterface} instance to the constructor.
  25.  * Whenever a new metadata instance is created, it is passed to the loader,
  26.  * which can configure the metadata based on configuration loaded from the
  27.  * filesystem or a database. If you want to use multiple loaders, wrap them in a
  28.  * {@link LoaderChain}.
  29.  *
  30.  * You can also optionally pass a {@link CacheInterface} instance to the
  31.  * constructor. This cache will be used for persisting the generated metadata
  32.  * between multiple PHP requests.
  33.  *
  34.  * @author Bernhard Schussek <bschussek@gmail.com>
  35.  */
  36. class LazyLoadingMetadataFactory implements MetadataFactoryInterface
  37. {
  38.     protected $loader;
  39.     protected $cache;
  40.     /**
  41.      * The loaded metadata, indexed by class name.
  42.      *
  43.      * @var ClassMetadata[]
  44.      */
  45.     protected $loadedClasses = [];
  46.     public function __construct(LoaderInterface $loader nullCacheItemPoolInterface $cache null)
  47.     {
  48.         $this->loader $loader;
  49.         $this->cache $cache;
  50.     }
  51.     /**
  52.      * {@inheritdoc}
  53.      *
  54.      * If the method was called with the same class name (or an object of that
  55.      * class) before, the same metadata instance is returned.
  56.      *
  57.      * If the factory was configured with a cache, this method will first look
  58.      * for an existing metadata instance in the cache. If an existing instance
  59.      * is found, it will be returned without further ado.
  60.      *
  61.      * Otherwise, a new metadata instance is created. If the factory was
  62.      * configured with a loader, the metadata is passed to the
  63.      * {@link LoaderInterface::loadClassMetadata()} method for further
  64.      * configuration. At last, the new object is returned.
  65.      */
  66.     public function getMetadataFor(mixed $value): MetadataInterface
  67.     {
  68.         if (!\is_object($value) && !\is_string($value)) {
  69.             throw new NoSuchMetadataException(sprintf('Cannot create metadata for non-objects. Got: "%s".'get_debug_type($value)));
  70.         }
  71.         $class ltrim(\is_object($value) ? \get_class($value) : $value'\\');
  72.         if (isset($this->loadedClasses[$class])) {
  73.             return $this->loadedClasses[$class];
  74.         }
  75.         if (!class_exists($class) && !interface_exists($classfalse)) {
  76.             throw new NoSuchMetadataException(sprintf('The class or interface "%s" does not exist.'$class));
  77.         }
  78.         $cacheItem $this->cache?->getItem($this->escapeClassName($class));
  79.         if ($cacheItem?->isHit()) {
  80.             $metadata $cacheItem->get();
  81.             // Include constraints from the parent class
  82.             $this->mergeConstraints($metadata);
  83.             return $this->loadedClasses[$class] = $metadata;
  84.         }
  85.         $metadata = new ClassMetadata($class);
  86.         $this->loader?->loadClassMetadata($metadata);
  87.         if (null !== $cacheItem) {
  88.             $this->cache->save($cacheItem->set($metadata));
  89.         }
  90.         // Include constraints from the parent class
  91.         $this->mergeConstraints($metadata);
  92.         return $this->loadedClasses[$class] = $metadata;
  93.     }
  94.     private function mergeConstraints(ClassMetadata $metadata)
  95.     {
  96.         if ($metadata->getReflectionClass()->isInterface()) {
  97.             return;
  98.         }
  99.         // Include constraints from the parent class
  100.         if ($parent $metadata->getReflectionClass()->getParentClass()) {
  101.             $metadata->mergeConstraints($this->getMetadataFor($parent->name));
  102.         }
  103.         // Include constraints from all directly implemented interfaces
  104.         foreach ($metadata->getReflectionClass()->getInterfaces() as $interface) {
  105.             if ('Symfony\Component\Validator\GroupSequenceProviderInterface' === $interface->name) {
  106.                 continue;
  107.             }
  108.             if ($parent && \in_array($interface->getName(), $parent->getInterfaceNames(), true)) {
  109.                 continue;
  110.             }
  111.             $metadata->mergeConstraints($this->getMetadataFor($interface->name));
  112.         }
  113.     }
  114.     /**
  115.      * {@inheritdoc}
  116.      */
  117.     public function hasMetadataFor(mixed $value): bool
  118.     {
  119.         if (!\is_object($value) && !\is_string($value)) {
  120.             return false;
  121.         }
  122.         $class ltrim(\is_object($value) ? \get_class($value) : $value'\\');
  123.         return class_exists($class) || interface_exists($classfalse);
  124.     }
  125.     /**
  126.      * Replaces backslashes by dots in a class name.
  127.      */
  128.     private function escapeClassName(string $class): string
  129.     {
  130.         if (str_contains($class'@')) {
  131.             // anonymous class: replace all PSR6-reserved characters
  132.             return str_replace(["\0"'\\''/''@'':''{''}''('')'], '.'$class);
  133.         }
  134.         return str_replace('\\''.'$class);
  135.     }
  136. }