我想用一个自定义的反规整器来装饰Symfony序列化器的ArrayDenormalizer,但它似乎在DependencyInjection中遇到了一个循环引用问题,因为Nginx正在崩溃502.
自定义的Denormalizer实现了DenormalizerAwareInterface,所以我实际上期望Symfony会通过Autowiring自动处理依赖注入。
<?php
namespace AppSerializer;
use AppMyEntity;
use SymfonyComponentSerializerNormalizerDenormalizerAwareInterface;
use SymfonyComponentSerializerNormalizerDenormalizerAwareTrait;
use SymfonyComponentSerializerNormalizerDenormalizerInterface;
class PreCheckRequestDenormalizer implements DenormalizerInterface, DenormalizerAwareInterface
{
use DenormalizerAwareTrait;
public function denormalize(mixed $data, string $type, string $format = null, array $context = [])
{
if (in_array($data['locale'], ['de', 'en']) === false) {
$data['locale'] = 'en';
}
return $this->denormalizer->denormalize($data, $type, $format, $context);
}
public function supportsDenormalization(mixed $data, string $type, string $format = null)
{
return is_array($data) && $type === MyEntity::class;
}
}
我在这里错过了什么?顺便说一下,它是Symfony 6.1。
似乎这是Symfony 6.1中自动安装NormalizeAwareInterface的错误:https://github.com/symfony/maker-bundle/issues/1252 issuecomment - 1342478104
这个错误导致了一个循环引用问题。
我解决了这个问题,不使用DenormalizerAwareInterface和DenormalizerAwareTrait,并通过禁用自定义Denormalizer的自动连接并显式声明服务:
AppSerializerPreCheckRequestDenormalizer:
autowire: false
arguments:
$denormalizer: '@serializer.normalizer.object'
$allowedLocales: '%app.allowed_locales%'
$defaultLocale: '%env(string:DEFAULT_LOCALE)%'
问题的注释描述了可能的其他方法,但这对我来说解决了它。