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

  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.      * If the method was called with the same class name (or an object of that
  53.      * class) before, the same metadata instance is returned.
  54.      *
  55.      * If the factory was configured with a cache, this method will first look
  56.      * for an existing metadata instance in the cache. If an existing instance
  57.      * is found, it will be returned without further ado.
  58.      *
  59.      * Otherwise, a new metadata instance is created. If the factory was
  60.      * configured with a loader, the metadata is passed to the
  61.      * {@link LoaderInterface::loadClassMetadata()} method for further
  62.      * configuration. At last, the new object is returned.
  63.      */
  64.     public function getMetadataFor(mixed $value): MetadataInterface
  65.     {
  66.         if (!\is_object($value) && !\is_string($value)) {
  67.             throw new NoSuchMetadataException(sprintf('Cannot create metadata for non-objects. Got: "%s".'get_debug_type($value)));
  68.         }
  69.         $class ltrim(\is_object($value) ? $value::class : $value'\\');
  70.         if (isset($this->loadedClasses[$class])) {
  71.             return $this->loadedClasses[$class];
  72.         }
  73.         if (!class_exists($class) && !interface_exists($classfalse)) {
  74.             throw new NoSuchMetadataException(sprintf('The class or interface "%s" does not exist.'$class));
  75.         }
  76.         $cacheItem $this->cache?->getItem($this->escapeClassName($class));
  77.         if ($cacheItem?->isHit()) {
  78.             $metadata $cacheItem->get();
  79.             // Include constraints from the parent class
  80.             $this->mergeConstraints($metadata);
  81.             return $this->loadedClasses[$class] = $metadata;
  82.         }
  83.         $metadata = new ClassMetadata($class);
  84.         $this->loader?->loadClassMetadata($metadata);
  85.         if (null !== $cacheItem) {
  86.             $this->cache->save($cacheItem->set($metadata));
  87.         }
  88.         // Include constraints from the parent class
  89.         $this->mergeConstraints($metadata);
  90.         return $this->loadedClasses[$class] = $metadata;
  91.     }
  92.     private function mergeConstraints(ClassMetadata $metadata)
  93.     {
  94.         if ($metadata->getReflectionClass()->isInterface()) {
  95.             return;
  96.         }
  97.         // Include constraints from the parent class
  98.         if ($parent $metadata->getReflectionClass()->getParentClass()) {
  99.             $metadata->mergeConstraints($this->getMetadataFor($parent->name));
  100.         }
  101.         // Include constraints from all directly implemented interfaces
  102.         foreach ($metadata->getReflectionClass()->getInterfaces() as $interface) {
  103.             if ('Symfony\Component\Validator\GroupSequenceProviderInterface' === $interface->name) {
  104.                 continue;
  105.             }
  106.             if ($parent && \in_array($interface->getName(), $parent->getInterfaceNames(), true)) {
  107.                 continue;
  108.             }
  109.             $metadata->mergeConstraints($this->getMetadataFor($interface->name));
  110.         }
  111.     }
  112.     public function hasMetadataFor(mixed $value): bool
  113.     {
  114.         if (!\is_object($value) && !\is_string($value)) {
  115.             return false;
  116.         }
  117.         $class ltrim(\is_object($value) ? $value::class : $value'\\');
  118.         return class_exists($class) || interface_exists($classfalse);
  119.     }
  120.     /**
  121.      * Replaces backslashes by dots in a class name.
  122.      */
  123.     private function escapeClassName(string $class): string
  124.     {
  125.         if (str_contains($class'@')) {
  126.             // anonymous class: replace all PSR6-reserved characters
  127.             return str_replace(["\0"'\\''/''@'':''{''}''('')'], '.'$class);
  128.         }
  129.         return str_replace('\\''.'$class);
  130.     }
  131. }