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

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\Cache\CacheInterface;
  14. use Symfony\Component\Validator\Mapping\ClassMetadata;
  15. use Symfony\Component\Validator\Mapping\Loader\LoaderInterface;
  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.     /**
  47.      * Creates a new metadata factory.
  48.      *
  49.      * @param CacheItemPoolInterface|null $cache The cache for persisting metadata
  50.      *                                           between multiple PHP requests
  51.      */
  52.     public function __construct(LoaderInterface $loader null$cache null)
  53.     {
  54.         if ($cache instanceof CacheInterface) {
  55.             @trigger_error(sprintf('Passing a "%s" to "%s" is deprecated in Symfony 4.4 and will trigger a TypeError in 5.0. Please pass an implementation of "%s" instead.', \get_class($cache), __METHOD__CacheItemPoolInterface::class), \E_USER_DEPRECATED);
  56.         } elseif (!$cache instanceof CacheItemPoolInterface && null !== $cache) {
  57.             throw new \TypeError(sprintf('Expected an instance of "%s", got "%s".'CacheItemPoolInterface::class, \is_object($cache) ? \get_class($cache) : \gettype($cache)));
  58.         }
  59.         $this->loader $loader;
  60.         $this->cache $cache;
  61.     }
  62.     /**
  63.      * {@inheritdoc}
  64.      *
  65.      * If the method was called with the same class name (or an object of that
  66.      * class) before, the same metadata instance is returned.
  67.      *
  68.      * If the factory was configured with a cache, this method will first look
  69.      * for an existing metadata instance in the cache. If an existing instance
  70.      * is found, it will be returned without further ado.
  71.      *
  72.      * Otherwise, a new metadata instance is created. If the factory was
  73.      * configured with a loader, the metadata is passed to the
  74.      * {@link LoaderInterface::loadClassMetadata()} method for further
  75.      * configuration. At last, the new object is returned.
  76.      */
  77.     public function getMetadataFor($value)
  78.     {
  79.         if (!\is_object($value) && !\is_string($value)) {
  80.             throw new NoSuchMetadataException(sprintf('Cannot create metadata for non-objects. Got: "%s".', \gettype($value)));
  81.         }
  82.         $class ltrim(\is_object($value) ? \get_class($value) : $value'\\');
  83.         if (isset($this->loadedClasses[$class])) {
  84.             return $this->loadedClasses[$class];
  85.         }
  86.         if (!class_exists($class) && !interface_exists($classfalse)) {
  87.             throw new NoSuchMetadataException(sprintf('The class or interface "%s" does not exist.'$class));
  88.         }
  89.         $cacheItem null;
  90.         if ($this->cache instanceof CacheInterface) {
  91.             if ($metadata $this->cache->read($class)) {
  92.                 // Include constraints from the parent class
  93.                 $this->mergeConstraints($metadata);
  94.                 return $this->loadedClasses[$class] = $metadata;
  95.             }
  96.         } elseif (null !== $this->cache) {
  97.             $cacheItem $this->cache->getItem($this->escapeClassName($class));
  98.             if ($cacheItem->isHit()) {
  99.                 $metadata $cacheItem->get();
  100.                 // Include constraints from the parent class
  101.                 $this->mergeConstraints($metadata);
  102.                 return $this->loadedClasses[$class] = $metadata;
  103.             }
  104.         }
  105.         $metadata = new ClassMetadata($class);
  106.         if (null !== $this->loader) {
  107.             $this->loader->loadClassMetadata($metadata);
  108.         }
  109.         if ($this->cache instanceof CacheInterface) {
  110.             $this->cache->write($metadata);
  111.         } elseif (null !== $cacheItem) {
  112.             $this->cache->save($cacheItem->set($metadata));
  113.         }
  114.         // Include constraints from the parent class
  115.         $this->mergeConstraints($metadata);
  116.         return $this->loadedClasses[$class] = $metadata;
  117.     }
  118.     private function mergeConstraints(ClassMetadata $metadata)
  119.     {
  120.         if ($metadata->getReflectionClass()->isInterface()) {
  121.             return;
  122.         }
  123.         // Include constraints from the parent class
  124.         if ($parent $metadata->getReflectionClass()->getParentClass()) {
  125.             $metadata->mergeConstraints($this->getMetadataFor($parent->name));
  126.         }
  127.         // Include constraints from all directly implemented interfaces
  128.         foreach ($metadata->getReflectionClass()->getInterfaces() as $interface) {
  129.             if ('Symfony\Component\Validator\GroupSequenceProviderInterface' === $interface->name) {
  130.                 continue;
  131.             }
  132.             if ($parent && \in_array($interface->getName(), $parent->getInterfaceNames(), true)) {
  133.                 continue;
  134.             }
  135.             $metadata->mergeConstraints($this->getMetadataFor($interface->name));
  136.         }
  137.     }
  138.     /**
  139.      * {@inheritdoc}
  140.      */
  141.     public function hasMetadataFor($value)
  142.     {
  143.         if (!\is_object($value) && !\is_string($value)) {
  144.             return false;
  145.         }
  146.         $class ltrim(\is_object($value) ? \get_class($value) : $value'\\');
  147.         return class_exists($class) || interface_exists($classfalse);
  148.     }
  149.     /**
  150.      * Replaces backslashes by dots in a class name.
  151.      */
  152.     private function escapeClassName(string $class): string
  153.     {
  154.         if (false !== strpos($class'@')) {
  155.             // anonymous class: replace all PSR6-reserved characters
  156.             return str_replace(["\0"'\\''/''@'':''{''}''('')'], '.'$class);
  157.         }
  158.         return str_replace('\\''.'$class);
  159.     }
  160. }