default services conflit ?

This commit is contained in:
armansansd
2022-04-27 11:30:43 +02:00
parent 28190a5749
commit 8bb1064a3b
8132 changed files with 900138 additions and 426 deletions

View File

@@ -0,0 +1,30 @@
<?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;
/**
* Gives access to the class, the format and the context in the property name converters.
*
* @author Kévin Dunglas <dunglas@gmail.com>
*/
interface AdvancedNameConverterInterface extends NameConverterInterface
{
/**
* {@inheritdoc}
*/
public function normalize($propertyName, string $class = null, string $format = null, array $context = []);
/**
* {@inheritdoc}
*/
public function denormalize($propertyName, string $class = null, string $format = null, array $context = []);
}

View File

@@ -0,0 +1,65 @@
<?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;
/**
* CamelCase to Underscore name converter.
*
* @author Kévin Dunglas <dunglas@gmail.com>
*/
class CamelCaseToSnakeCaseNameConverter implements NameConverterInterface
{
private $attributes;
private $lowerCamelCase;
/**
* @param array|null $attributes The list of attributes to rename or null for all attributes
* @param bool $lowerCamelCase Use lowerCamelCase style
*/
public function __construct(array $attributes = null, bool $lowerCamelCase = true)
{
$this->attributes = $attributes;
$this->lowerCamelCase = $lowerCamelCase;
}
/**
* {@inheritdoc}
*/
public function normalize($propertyName)
{
if (null === $this->attributes || \in_array($propertyName, $this->attributes)) {
return strtolower(preg_replace('/[A-Z]/', '_\\0', lcfirst($propertyName)));
}
return $propertyName;
}
/**
* {@inheritdoc}
*/
public function denormalize($propertyName)
{
$camelCasedName = preg_replace_callback('/(^|_|\.)+(.)/', function ($match) {
return ('.' === $match[1] ? '_' : '').strtoupper($match[2]);
}, $propertyName);
if ($this->lowerCamelCase) {
$camelCasedName = lcfirst($camelCasedName);
}
if (null === $this->attributes || \in_array($camelCasedName, $this->attributes)) {
return $camelCasedName;
}
return $propertyName;
}
}

View File

@@ -0,0 +1,144 @@
<?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\Mapping\Factory\ClassMetadataFactoryInterface;
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
/**
* @author Fabien Bourigault <bourigaultfabien@gmail.com>
*/
final class MetadataAwareNameConverter implements AdvancedNameConverterInterface
{
private $metadataFactory;
/**
* @var NameConverterInterface|AdvancedNameConverterInterface|null
*/
private $fallbackNameConverter;
private static $normalizeCache = [];
private static $denormalizeCache = [];
private static $attributesMetadataCache = [];
public function __construct(ClassMetadataFactoryInterface $metadataFactory, NameConverterInterface $fallbackNameConverter = null)
{
$this->metadataFactory = $metadataFactory;
$this->fallbackNameConverter = $fallbackNameConverter;
}
/**
* {@inheritdoc}
*/
public function normalize($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);
}
/**
* {@inheritdoc}
*/
public function denormalize($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;
}
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;
}
private function getCacheValueForAttributesMetadata(string $class, array $context): array
{
if (!$this->metadataFactory->hasMetadataFor($class)) {
return [];
}
$classMetadata = $this->metadataFactory->getMetadataFor($class);
$cache = [];
foreach ($classMetadata->getAttributesMetadata() as $name => $metadata) {
if (null === $metadata->getSerializedName()) {
continue;
}
$groups = $metadata->getGroups();
if (!$groups && ($context[AbstractNormalizer::GROUPS] ?? [])) {
continue;
}
if ($groups && !array_intersect($groups, (array) ($context[AbstractNormalizer::GROUPS] ?? []))) {
continue;
}
$cache[$metadata->getSerializedName()] = $name;
}
return $cache;
}
private function getCacheKey(string $class, array $context): string
{
if (isset($context['cache_key'])) {
return $class.'-'.$context['cache_key'];
}
return $class.md5(serialize($context[AbstractNormalizer::GROUPS] ?? []));
}
}

View File

@@ -0,0 +1,38 @@
<?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;
/**
* Defines the interface for property name converters.
*
* @author Kévin Dunglas <dunglas@gmail.com>
*/
interface NameConverterInterface
{
/**
* Converts a property name to its normalized value.
*
* @param string $propertyName
*
* @return string
*/
public function normalize($propertyName);
/**
* Converts a property name to its denormalized value.
*
* @param string $propertyName
*
* @return string
*/
public function denormalize($propertyName);
}