| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
namespace Symfony\Component\Serializer\Mapping\Factory; |
| 13 |
|
| 14 |
use Symfony\Component\Serializer\Mapping\AttributeMetadata; |
| 15 |
use Symfony\Component\Serializer\Mapping\ClassDiscriminatorMapping; |
| 16 |
use Symfony\Component\Serializer\Mapping\ClassMetadata; |
| 17 |
use Symfony\Component\Serializer\Mapping\ClassMetadataInterface; |
| 18 |
|
| 19 |
trigger_deprecation('symfony/serializer', '7.3', 'The "%s" class is deprecated.', CompiledClassMetadataFactory::class); |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
final class CompiledClassMetadataFactory implements ClassMetadataFactoryInterface |
| 27 |
{ |
| 28 |
private array $compiledClassMetadata = []; |
| 29 |
|
| 30 |
private array $loadedClasses = []; |
| 31 |
|
| 32 |
public function __construct( |
| 33 |
string $compiledClassMetadataFile, |
| 34 |
private readonly ClassMetadataFactoryInterface $classMetadataFactory, |
| 35 |
) { |
| 36 |
if (!file_exists($compiledClassMetadataFile)) { |
| 37 |
throw new \RuntimeException("File \"{$compiledClassMetadataFile}\" could not be found."); |
| 38 |
} |
| 39 |
|
| 40 |
$compiledClassMetadata = require $compiledClassMetadataFile; |
| 41 |
if (!\is_array($compiledClassMetadata)) { |
| 42 |
throw new \RuntimeException(\sprintf('Compiled metadata must be of the type array, %s given.', \gettype($compiledClassMetadata))); |
| 43 |
} |
| 44 |
|
| 45 |
$this->compiledClassMetadata = $compiledClassMetadata; |
| 46 |
} |
| 47 |
|
| 48 |
public function getMetadataFor(string|object $value): ClassMetadataInterface |
| 49 |
{ |
| 50 |
$className = \is_object($value) ? $value::class : $value; |
| 51 |
|
| 52 |
if (!isset($this->compiledClassMetadata[$className])) { |
| 53 |
return $this->classMetadataFactory->getMetadataFor($value); |
| 54 |
} |
| 55 |
|
| 56 |
if (!isset($this->loadedClasses[$className])) { |
| 57 |
$classMetadata = new ClassMetadata($className); |
| 58 |
foreach ($this->compiledClassMetadata[$className][0] as $name => $compiledAttributesMetadata) { |
| 59 |
$classMetadata->addAttributeMetadata($attributeMetadata = new AttributeMetadata($name)); |
| 60 |
foreach ($compiledAttributesMetadata[0] as $group) { |
| 61 |
$attributeMetadata->addGroup($group); |
| 62 |
} |
| 63 |
$attributeMetadata->setMaxDepth($compiledAttributesMetadata[1]); |
| 64 |
$attributeMetadata->setSerializedName($compiledAttributesMetadata[2]); |
| 65 |
} |
| 66 |
$classMetadata->setClassDiscriminatorMapping($this->compiledClassMetadata[$className][1] |
| 67 |
? new ClassDiscriminatorMapping(...$this->compiledClassMetadata[$className][1]) |
| 68 |
: null |
| 69 |
); |
| 70 |
|
| 71 |
$this->loadedClasses[$className] = $classMetadata; |
| 72 |
} |
| 73 |
|
| 74 |
return $this->loadedClasses[$className]; |
| 75 |
} |
| 76 |
|
| 77 |
public function hasMetadataFor(mixed $value): bool |
| 78 |
{ |
| 79 |
$className = \is_object($value) ? $value::class : $value; |
| 80 |
|
| 81 |
return isset($this->compiledClassMetadata[$className]) || $this->classMetadataFactory->hasMetadataFor($value); |
| 82 |
} |
| 83 |
} |