| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
namespace Symfony\Component\Serializer\Mapping\Loader; |
| 13 |
|
| 14 |
use Symfony\Component\Serializer\Attribute\Context; |
| 15 |
use Symfony\Component\Serializer\Attribute\DiscriminatorMap; |
| 16 |
use Symfony\Component\Serializer\Attribute\Groups; |
| 17 |
use Symfony\Component\Serializer\Attribute\Ignore; |
| 18 |
use Symfony\Component\Serializer\Attribute\MaxDepth; |
| 19 |
use Symfony\Component\Serializer\Attribute\SerializedName; |
| 20 |
use Symfony\Component\Serializer\Attribute\SerializedPath; |
| 21 |
use Symfony\Component\Serializer\Exception\MappingException; |
| 22 |
use Symfony\Component\Serializer\Mapping\AttributeMetadata; |
| 23 |
use Symfony\Component\Serializer\Mapping\AttributeMetadataInterface; |
| 24 |
use Symfony\Component\Serializer\Mapping\ClassDiscriminatorMapping; |
| 25 |
use Symfony\Component\Serializer\Mapping\ClassMetadataInterface; |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
> |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
class AttributeLoader implements LoaderInterface |
| 35 |
{ |
| 36 |
use AccessorCollisionResolverTrait; |
| 37 |
|
| 38 |
private const KNOWN_ATTRIBUTES = [ |
| 39 |
DiscriminatorMap::class, |
| 40 |
Groups::class, |
| 41 |
Ignore::class, |
| 42 |
MaxDepth::class, |
| 43 |
SerializedName::class, |
| 44 |
SerializedPath::class, |
| 45 |
Context::class, |
| 46 |
]; |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
public function __construct( |
| 53 |
private ?bool $allowAnyClass = true, |
| 54 |
private array $mappedClasses = [], |
| 55 |
) { |
| 56 |
$this->allowAnyClass ??= true; |
| 57 |
} |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
public function getMappedClasses(): array |
| 63 |
{ |
| 64 |
return array_keys($this->mappedClasses); |
| 65 |
} |
| 66 |
|
| 67 |
public function loadClassMetadata(ClassMetadataInterface $classMetadata): bool |
| 68 |
{ |
| 69 |
if (!$sourceClasses = $this->mappedClasses[$classMetadata->getName()] ??= $this->allowAnyClass ? [$classMetadata->getName()] : []) { |
| 70 |
return false; |
| 71 |
} |
| 72 |
|
| 73 |
$success = false; |
| 74 |
foreach ($sourceClasses as $sourceClass) { |
| 75 |
$reflectionClass = $classMetadata->getName() === $sourceClass ? $classMetadata->getReflectionClass() : new \ReflectionClass($sourceClass); |
| 76 |
$success = $this->doLoadClassMetadata($reflectionClass, $classMetadata) || $success; |
| 77 |
} |
| 78 |
|
| 79 |
return $success; |
| 80 |
} |
| 81 |
|
| 82 |
private function doLoadClassMetadata(\ReflectionClass $reflectionClass, ClassMetadataInterface $classMetadata): bool |
| 83 |
{ |
| 84 |
$className = $reflectionClass->name; |
| 85 |
$loaded = false; |
| 86 |
$classGroups = []; |
| 87 |
$classContextAttribute = null; |
| 88 |
|
| 89 |
$attributesMetadata = $classMetadata->getAttributesMetadata(); |
| 90 |
|
| 91 |
foreach ($this->loadAttributes($reflectionClass) as $attribute) { |
| 92 |
match (true) { |
| 93 |
$attribute instanceof DiscriminatorMap => $classMetadata->setClassDiscriminatorMapping(new ClassDiscriminatorMapping($attribute->typeProperty, $attribute->mapping, $attribute->defaultType)), |
| 94 |
$attribute instanceof Groups => $classGroups = $attribute->groups, |
| 95 |
$attribute instanceof Context => $classContextAttribute = $attribute, |
| 96 |
default => null, |
| 97 |
}; |
| 98 |
} |
| 99 |
|
| 100 |
foreach ($reflectionClass->getProperties() as $property) { |
| 101 |
if (!isset($attributesMetadata[$property->name])) { |
| 102 |
$attributesMetadata[$property->name] = new AttributeMetadata($property->name); |
| 103 |
$classMetadata->addAttributeMetadata($attributesMetadata[$property->name]); |
| 104 |
} |
| 105 |
|
| 106 |
$attributeMetadata = $attributesMetadata[$property->name]; |
| 107 |
if ($property->getDeclaringClass()->name === $className) { |
| 108 |
if ($classContextAttribute) { |
| 109 |
$this->setAttributeContextsForGroups($classContextAttribute, $attributeMetadata); |
| 110 |
} |
| 111 |
|
| 112 |
foreach ($classGroups as $group) { |
| 113 |
$attributeMetadata->addGroup($group); |
| 114 |
} |
| 115 |
|
| 116 |
foreach ($this->loadAttributes($property) as $attribute) { |
| 117 |
$loaded = true; |
| 118 |
|
| 119 |
if ($attribute instanceof Groups) { |
| 120 |
foreach ($attribute->groups as $group) { |
| 121 |
$attributeMetadata->addGroup($group); |
| 122 |
} |
| 123 |
|
| 124 |
continue; |
| 125 |
} |
| 126 |
|
| 127 |
match (true) { |
| 128 |
$attribute instanceof MaxDepth => $attributeMetadata->setMaxDepth($attribute->maxDepth), |
| 129 |
$attribute instanceof SerializedName => $attributeMetadata->setSerializedName($attribute->serializedName), |
| 130 |
$attribute instanceof SerializedPath => $attributeMetadata->setSerializedPath($attribute->serializedPath), |
| 131 |
$attribute instanceof Ignore => $attributeMetadata->setIgnore(true), |
| 132 |
$attribute instanceof Context => $this->setAttributeContextsForGroups($attribute, $attributeMetadata), |
| 133 |
default => null, |
| 134 |
}; |
| 135 |
} |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
foreach ($reflectionClass->getMethods() as $method) { |
| 140 |
if ($method->getDeclaringClass()->name !== $className) { |
| 141 |
continue; |
| 142 |
} |
| 143 |
$name = $method->name; |
| 144 |
|
| 145 |
if (0 === stripos($name, 'get') && $method->getNumberOfRequiredParameters()) { |
| 146 |
continue; |
| 147 |
} |
| 148 |
|
| 149 |
$attributeName = $this->getAttributeNameFromAccessor($reflectionClass, $method, true); |
| 150 |
$accessorOrMutator = null !== $attributeName; |
| 151 |
$hasProperty = $this->hasPropertyForAccessor($method->getDeclaringClass(), $name); |
| 152 |
$attributeMetadata = null; |
| 153 |
|
| 154 |
if ($hasProperty || $accessorOrMutator) { |
| 155 |
if (null === $attributeName || 's' !== $name[0] && $hasProperty && $this->hasAttributeNameCollision($reflectionClass, $attributeName, $name)) { |
| 156 |
$attributeName = $name; |
| 157 |
} |
| 158 |
|
| 159 |
if (isset($attributesMetadata[$attributeName])) { |
| 160 |
$attributeMetadata = $attributesMetadata[$attributeName]; |
| 161 |
} else { |
| 162 |
$attributesMetadata[$attributeName] = $attributeMetadata = new AttributeMetadata($attributeName); |
| 163 |
$classMetadata->addAttributeMetadata($attributeMetadata); |
| 164 |
} |
| 165 |
} |
| 166 |
|
| 167 |
foreach ($this->loadAttributes($method) as $attribute) { |
| 168 |
if ($attribute instanceof Groups) { |
| 169 |
if (!$attributeMetadata) { |
| 170 |
throw new MappingException(\sprintf('Groups on "%s::%s()" cannot be added. Groups can only be added on methods beginning with "get", "is", "has", "can" or "set".', $className, $method->name)); |
| 171 |
} |
| 172 |
|
| 173 |
foreach ($attribute->groups as $group) { |
| 174 |
$attributeMetadata->addGroup($group); |
| 175 |
} |
| 176 |
} elseif ($attribute instanceof MaxDepth) { |
| 177 |
if (!$attributeMetadata) { |
| 178 |
throw new MappingException(\sprintf('MaxDepth on "%s::%s()" cannot be added. MaxDepth can only be added on methods beginning with "get", "is", "has", "can" or "set".', $className, $method->name)); |
| 179 |
} |
| 180 |
|
| 181 |
$attributeMetadata->setMaxDepth($attribute->maxDepth); |
| 182 |
} elseif ($attribute instanceof SerializedName) { |
| 183 |
if (!$attributeMetadata) { |
| 184 |
throw new MappingException(\sprintf('SerializedName on "%s::%s()" cannot be added. SerializedName can only be added on methods beginning with "get", "is", "has", "can" or "set".', $className, $method->name)); |
| 185 |
} |
| 186 |
|
| 187 |
$attributeMetadata->setSerializedName($attribute->serializedName); |
| 188 |
} elseif ($attribute instanceof SerializedPath) { |
| 189 |
if (!$attributeMetadata) { |
| 190 |
throw new MappingException(\sprintf('SerializedPath on "%s::%s()" cannot be added. SerializedPath can only be added on methods beginning with "get", "is", "has", "can" or "set".', $className, $method->name)); |
| 191 |
} |
| 192 |
|
| 193 |
$attributeMetadata->setSerializedPath($attribute->serializedPath); |
| 194 |
} elseif ($attribute instanceof Ignore) { |
| 195 |
if ($attributeMetadata) { |
| 196 |
$attributeMetadata->setIgnore(true); |
| 197 |
} |
| 198 |
} elseif ($attribute instanceof Context) { |
| 199 |
if (!$attributeMetadata) { |
| 200 |
throw new MappingException(\sprintf('Context on "%s::%s()" cannot be added. Context can only be added on methods beginning with "get", "is", "has", "can" or "set".', $className, $method->name)); |
| 201 |
} |
| 202 |
|
| 203 |
$this->setAttributeContextsForGroups($attribute, $attributeMetadata); |
| 204 |
} |
| 205 |
|
| 206 |
$loaded = true; |
| 207 |
} |
| 208 |
} |
| 209 |
|
| 210 |
return $loaded; |
| 211 |
} |
| 212 |
|
| 213 |
private function loadAttributes(\ReflectionMethod|\ReflectionClass|\ReflectionProperty $reflector): iterable |
| 214 |
{ |
| 215 |
foreach ($reflector->getAttributes() as $attribute) { |
| 216 |
if ($this->isKnownAttribute($attribute->getName())) { |
| 217 |
try { |
| 218 |
yield $attribute->newInstance(); |
| 219 |
} catch (\Error $e) { |
| 220 |
if (\Error::class !== $e::class) { |
| 221 |
throw $e; |
| 222 |
} |
| 223 |
$on = match (true) { |
| 224 |
$reflector instanceof \ReflectionClass => ' on class '.$reflector->name, |
| 225 |
$reflector instanceof \ReflectionMethod => \sprintf(' on "%s::%s()"', $reflector->getDeclaringClass()->name, $reflector->name), |
| 226 |
$reflector instanceof \ReflectionProperty => \sprintf(' on "%s::$%s"', $reflector->getDeclaringClass()->name, $reflector->name), |
| 227 |
default => '', |
| 228 |
}; |
| 229 |
|
| 230 |
throw new MappingException(\sprintf('Could not instantiate attribute "%s"%s.', $attribute->getName(), $on), 0, $e); |
| 231 |
} |
| 232 |
} |
| 233 |
} |
| 234 |
} |
| 235 |
|
| 236 |
private function setAttributeContextsForGroups(Context $attribute, AttributeMetadataInterface $attributeMetadata): void |
| 237 |
{ |
| 238 |
$context = $attribute->context; |
| 239 |
$groups = $attribute->groups; |
| 240 |
$normalizationContext = $attribute->normalizationContext; |
| 241 |
$denormalizationContext = $attribute->denormalizationContext; |
| 242 |
|
| 243 |
if ($normalizationContext || $context) { |
| 244 |
$attributeMetadata->setNormalizationContextForGroups($normalizationContext ?: $context, $groups); |
| 245 |
} |
| 246 |
|
| 247 |
if ($denormalizationContext || $context) { |
| 248 |
$attributeMetadata->setDenormalizationContextForGroups($denormalizationContext ?: $context, $groups); |
| 249 |
} |
| 250 |
} |
| 251 |
|
| 252 |
private function isKnownAttribute(string $attributeName): bool |
| 253 |
{ |
| 254 |
foreach (self::KNOWN_ATTRIBUTES as $knownAttribute) { |
| 255 |
if (is_a($attributeName, $knownAttribute, true)) { |
| 256 |
return true; |
| 257 |
} |
| 258 |
} |
| 259 |
|
| 260 |
return false; |
| 261 |
} |
| 262 |
} |