CompiledClassMetadataFactory.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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\Factory;
  11. use Symfony\Component\Serializer\Mapping\AttributeMetadata;
  12. use Symfony\Component\Serializer\Mapping\ClassDiscriminatorMapping;
  13. use Symfony\Component\Serializer\Mapping\ClassMetadata;
  14. /**
  15. * @author Fabien Bourigault <bourigaultfabien@gmail.com>
  16. */
  17. final class CompiledClassMetadataFactory implements ClassMetadataFactoryInterface
  18. {
  19. private $compiledClassMetadata = [];
  20. private $loadedClasses = [];
  21. private $classMetadataFactory;
  22. public function __construct(string $compiledClassMetadataFile, ClassMetadataFactoryInterface $classMetadataFactory)
  23. {
  24. if (!file_exists($compiledClassMetadataFile)) {
  25. throw new \RuntimeException("File \"{$compiledClassMetadataFile}\" could not be found.");
  26. }
  27. $compiledClassMetadata = require $compiledClassMetadataFile;
  28. if (!\is_array($compiledClassMetadata)) {
  29. throw new \RuntimeException(sprintf('Compiled metadata must be of the type array, %s given.', \gettype($compiledClassMetadata)));
  30. }
  31. $this->compiledClassMetadata = $compiledClassMetadata;
  32. $this->classMetadataFactory = $classMetadataFactory;
  33. }
  34. /**
  35. * {@inheritdoc}
  36. */
  37. public function getMetadataFor($value)
  38. {
  39. $className = \is_object($value) ? \get_class($value) : $value;
  40. if (!isset($this->compiledClassMetadata[$className])) {
  41. return $this->classMetadataFactory->getMetadataFor($value);
  42. }
  43. if (!isset($this->loadedClasses[$className])) {
  44. $classMetadata = new ClassMetadata($className);
  45. foreach ($this->compiledClassMetadata[$className][0] as $name => $compiledAttributesMetadata) {
  46. $classMetadata->attributesMetadata[$name] = $attributeMetadata = new AttributeMetadata($name);
  47. [$attributeMetadata->groups, $attributeMetadata->maxDepth, $attributeMetadata->serializedName] = $compiledAttributesMetadata;
  48. }
  49. $classMetadata->classDiscriminatorMapping = $this->compiledClassMetadata[$className][1]
  50. ? new ClassDiscriminatorMapping(...$this->compiledClassMetadata[$className][1])
  51. : null
  52. ;
  53. $this->loadedClasses[$className] = $classMetadata;
  54. }
  55. return $this->loadedClasses[$className];
  56. }
  57. /**
  58. * {@inheritdoc}
  59. */
  60. public function hasMetadataFor($value)
  61. {
  62. $className = \is_object($value) ? \get_class($value) : $value;
  63. return isset($this->compiledClassMetadata[$className]) || $this->classMetadataFactory->hasMetadataFor($value);
  64. }
  65. }