Some checks failed
CI / PHP 8.3 / mariadb:10.6 (pull_request) Successful in 1m14s
CI / PHP 8.4 / mariadb:10.6 (pull_request) Successful in 1m12s
CI / PHP 8.4 / mariadb:11.4 (pull_request) Successful in 1m7s
CI / PHP 8.3 / mariadb:11.4 (pull_request) Successful in 1m19s
CI / PHP 8.3 / mysql:8.0 (pull_request) Successful in 1m32s
CI / PHP 8.4 / mysql:8.0 (pull_request) Successful in 1m22s
CI / PHP 8.4 / mysql:8.4 (pull_request) Successful in 1m32s
CI / PHP 8.3 / mysql:8.4 (pull_request) Successful in 1m35s
CI / Build-Artefakte aktuell (pull_request) Failing after 1m7s
- Laravel 13 mit Filament 5 (Admin), Inertia 3 + Vue 3 + Vuetify 4 (Material 3) - Anmeldung über Fortify (Login, Passwort vergessen/ändern), keine Selbstregistrierung - Profil mit Profilbild (WebP in festen Größen), Personenseiten, Header mit Avatar-Menü - Material-3-Farbschema aus der Primärfarbe, hell/dunkel/System pro Person - Basis-Branding (Portalname, Logo, Primärfarbe) im Admin unter „Erscheinungsbild“ - Benutzerverwaltung im Admin, Zugang vorläufig über is_admin - vendor/ (ohne Dev-Pakete) und Frontend-Build mitversioniert, Dev-Pakete in vendor-dev/ - PHPUnit-Tests gegen MySQL/MariaDB, Forgejo-CI-Matrix, Docker-Entwicklungsumgebung - VERSION, CHANGELOG.md und CONTRIBUTING.md mit Release-Ablauf Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
160 lines
6.3 KiB
PHP
160 lines
6.3 KiB
PHP
<?php
|
|
|
|
/*
|
|
* This file is part of the Symfony package.
|
|
*
|
|
* (c) Fabien Potencier <fabien@symfony.com>
|
|
*
|
|
* For the full copyright and license information, please view the LICENSE
|
|
* file that was distributed with this source code.
|
|
*/
|
|
|
|
namespace Symfony\Component\Serializer\NameConverter;
|
|
|
|
use Symfony\Component\Serializer\Exception\LogicException;
|
|
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactoryInterface;
|
|
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
|
|
|
|
/**
|
|
* @author Fabien Bourigault <bourigaultfabien@gmail.com>
|
|
*/
|
|
final class MetadataAwareNameConverter implements AdvancedNameConverterInterface
|
|
{
|
|
/**
|
|
* @var array<string, array<string, string|null>>
|
|
*/
|
|
private static array $normalizeCache = [];
|
|
|
|
/**
|
|
* @var array<string, array<string, string|null>>
|
|
*/
|
|
private static array $denormalizeCache = [];
|
|
|
|
/**
|
|
* @var array<string, array<string, string>>
|
|
*/
|
|
private static array $attributesMetadataCache = [];
|
|
|
|
public function __construct(
|
|
private readonly ClassMetadataFactoryInterface $metadataFactory,
|
|
private readonly ?NameConverterInterface $fallbackNameConverter = null,
|
|
) {
|
|
}
|
|
|
|
public function normalize(string $propertyName, ?string $class = null, ?string $format = null, array $context = []): string
|
|
{
|
|
if (null === $class) {
|
|
return $this->normalizeFallback($propertyName, $class, $format, $context);
|
|
}
|
|
|
|
if (!\array_key_exists($class, self::$normalizeCache) || !\array_key_exists($propertyName, self::$normalizeCache[$class])) {
|
|
self::$normalizeCache[$class][$propertyName] = $this->getCacheValueForNormalization($propertyName, $class);
|
|
}
|
|
|
|
return self::$normalizeCache[$class][$propertyName] ?? $this->normalizeFallback($propertyName, $class, $format, $context);
|
|
}
|
|
|
|
public function denormalize(string $propertyName, ?string $class = null, ?string $format = null, array $context = []): string
|
|
{
|
|
if (null === $class) {
|
|
return $this->denormalizeFallback($propertyName, $class, $format, $context);
|
|
}
|
|
|
|
$cacheKey = $this->getCacheKey($class, $context);
|
|
if (!\array_key_exists($cacheKey, self::$denormalizeCache) || !\array_key_exists($propertyName, self::$denormalizeCache[$cacheKey])) {
|
|
self::$denormalizeCache[$cacheKey][$propertyName] = $this->getCacheValueForDenormalization($propertyName, $class, $context);
|
|
}
|
|
|
|
return self::$denormalizeCache[$cacheKey][$propertyName] ?? $this->denormalizeFallback($propertyName, $class, $format, $context);
|
|
}
|
|
|
|
private function getCacheValueForNormalization(string $propertyName, string $class): ?string
|
|
{
|
|
if (!$this->metadataFactory->hasMetadataFor($class)) {
|
|
return null;
|
|
}
|
|
|
|
$attributesMetadata = $this->metadataFactory->getMetadataFor($class)->getAttributesMetadata();
|
|
if (!\array_key_exists($propertyName, $attributesMetadata)) {
|
|
return null;
|
|
}
|
|
|
|
if (null !== $attributesMetadata[$propertyName]->getSerializedName() && null !== $attributesMetadata[$propertyName]->getSerializedPath()) {
|
|
throw new LogicException(\sprintf('Found SerializedName and SerializedPath attributes on property "%s" of class "%s".', $propertyName, $class));
|
|
}
|
|
|
|
return $attributesMetadata[$propertyName]->getSerializedName() ?? null;
|
|
}
|
|
|
|
private function normalizeFallback(string $propertyName, ?string $class = null, ?string $format = null, array $context = []): string
|
|
{
|
|
return $this->fallbackNameConverter ? $this->fallbackNameConverter->normalize($propertyName, $class, $format, $context) : $propertyName;
|
|
}
|
|
|
|
private function getCacheValueForDenormalization(string $propertyName, string $class, array $context): ?string
|
|
{
|
|
$cacheKey = $this->getCacheKey($class, $context);
|
|
if (!\array_key_exists($cacheKey, self::$attributesMetadataCache)) {
|
|
self::$attributesMetadataCache[$cacheKey] = $this->getCacheValueForAttributesMetadata($class, $context);
|
|
}
|
|
|
|
return self::$attributesMetadataCache[$cacheKey][$propertyName] ?? null;
|
|
}
|
|
|
|
private function denormalizeFallback(string $propertyName, ?string $class = null, ?string $format = null, array $context = []): string
|
|
{
|
|
return $this->fallbackNameConverter ? $this->fallbackNameConverter->denormalize($propertyName, $class, $format, $context) : $propertyName;
|
|
}
|
|
|
|
/**
|
|
* @return array<string, string>
|
|
*/
|
|
private function getCacheValueForAttributesMetadata(string $class, array $context): array
|
|
{
|
|
if (!$this->metadataFactory->hasMetadataFor($class)) {
|
|
return [];
|
|
}
|
|
|
|
$attributesMetadata = $this->metadataFactory->getMetadataFor($class)->getAttributesMetadata();
|
|
$contextGroups = (array) ($context[AbstractNormalizer::GROUPS] ?? []);
|
|
|
|
$cache = [];
|
|
foreach ($attributesMetadata as $name => $metadata) {
|
|
if (null === $serializedName = $metadata->getSerializedName()) {
|
|
continue;
|
|
}
|
|
|
|
if (null !== $metadata->getSerializedPath()) {
|
|
throw new LogicException(\sprintf('Found SerializedName and SerializedPath attributes on property "%s" of class "%s".', $name, $class));
|
|
}
|
|
|
|
$metadataGroups = $metadata->getGroups();
|
|
|
|
if (!$contextGroups) {
|
|
$sameNameMetadata = $attributesMetadata[$serializedName] ?? null;
|
|
|
|
// without groups to tell them apart, the attribute using that name for itself wins
|
|
if ($metadataGroups && $sameNameMetadata && null === $sameNameMetadata->getSerializedName()) {
|
|
continue;
|
|
}
|
|
} elseif (!$metadataGroups) {
|
|
continue;
|
|
} elseif (!array_intersect($metadataGroups, $contextGroups) && !\in_array('*', $contextGroups, true)) {
|
|
continue;
|
|
}
|
|
|
|
$cache[$serializedName] = $name;
|
|
}
|
|
|
|
return $cache;
|
|
}
|
|
|
|
private function getCacheKey(string $class, array $context): string
|
|
{
|
|
if (isset($context['cache_key'])) {
|
|
return $class.'-'.$context['cache_key'];
|
|
}
|
|
|
|
return $class.hash('xxh128', serialize($context[AbstractNormalizer::GROUPS] ?? []));
|
|
}
|
|
}
|