LazyLoadingMetadataFactory.php 5.7 KB

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