无法为Symfony ConstraintViolationList覆盖JMS序列化器的默认处理程序 [英] Unable to override JMS Serializer's default handler for Symfony ConstraintViolationList
问题描述
我无法覆盖jms序列化程序包中的默认处理程序.
I am unable to override default handlers in jms serializer bundle.
我想更改Symfony\Component\Validator\ConstraintViolationList
的序列化方式,所以我编写了自己的自定义处理程序.并按照文档(以及各种stackoverflow答案)中所述正确标记它.
I'd like to change the way Symfony\Component\Validator\ConstraintViolationList
is serialized so I wrote my own custom handler. And tagged it correctly as described in the documentation (and in various stackoverflow answers).
但是,我的处理程序一直被JMS序列化程序捆绑包附带的ConstraintViolationList的默认处理程序覆盖.
However, my handler keeps being overridden by the default handler for ConstraintViolationList that JMS Serializer bundle ships with.
我已正确标记我的处理程序服务.实际上,当我从vendor/jms/serializer-bundle/JMS/SerializerBundle/Resources/config/services.xml
I've tagged my handler service correctly. In fact, my handler service is detected and used correctly when I comment out ms_serializer.constraint_violation_handler
service definition from vendor/jms/serializer-bundle/JMS/SerializerBundle/Resources/config/services.xml
如何阻止默认处理程序覆盖我的自定义处理程序?
我什至尝试从自己的包中覆盖jms_serializer.constraint_violation_handler.class
参数,但还是没有运气.
I've even tried overriding jms_serializer.constraint_violation_handler.class
parameter from my own bundle but still no luck.
这是我的Handler类:
Here is my Handler class:
<?php
namespace Coanda\Bridge\JMSSerializer\Handler;
use JMS\Serializer\Context;
use JMS\Serializer\GraphNavigator;
use JMS\Serializer\Handler\SubscribingHandlerInterface;
use JMS\Serializer\JsonSerializationVisitor;
use Symfony\Component\Validator\ConstraintViolation;
use Symfony\Component\Validator\ConstraintViolationList;
class ConstraintViolationHandler implements SubscribingHandlerInterface
{
public static function getSubscribingMethods()
{
$methods = [];
$methods[] = [
'direction' => GraphNavigator::DIRECTION_SERIALIZATION,
'type' => ConstraintViolationList::class,
'format' => 'json',
'method' => 'serializeListToJson'
];
return $methods;
}
public function serializeListToJson(
JsonSerializationVisitor $visitor,
ConstraintViolationList $list,
array $type,
Context $context
) {
$violations = [];
foreach ($list as $item) {
$violations[$item->getPropertyPath()][] = $item->getMessage();
}
if (null === $visitor->getRoot()) {
$visitor->setRoot($violations);
}
return $violations;
}
}
我已经在services.xml
<service id="coanda.serializer.constraint_violation_handler"
class="Coanda\Bridge\JMSSerializer\Handler\ConstraintViolationHandler">
<tag name="jms_serializer.subscribing_handler"
type="Symfony\Component\Validator\ConstraintViolationList"
direction="serialization" format="json" method="serializeListToJson" />
</service>
推荐答案
之所以发生这种情况,是因为JMSSerializerBundle是在我的捆绑软件在AppKernel中注册之后注册的,这意味着我定义的任何服务都将被JMS Serializer的版本覆盖.
This was happening because JMSSerializerBundle was registered after my bundle in AppKernel which meant that whatever service I define will be overridden by JMS Serializer's version of it.
解决方案:将捆绑包放在AppKernel.php的最底部,如下所示:
Solution: put your bundle at the very bottom in AppKernel.php like so:
public function registerBundles()
{
$bundles = [
// .......
new JMS\SerializerBundle\JMSSerializerBundle(),
new My\Bundle\MyAwesomeBundle()
];
return $bundles;
}
这篇关于无法为Symfony ConstraintViolationList覆盖JMS序列化器的默认处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!