php - 无法覆盖 Symfony ConstraintViolationList 的 JMS 序列化程序的默认处理程序

标签 php symfony serialization jmsserializerbundle jms-serializer

我无法覆盖 jms 序列化程序包中的默认处理程序。

我想更改 Symfony\Component\Validator\ConstraintViolationList 的序列化方式,所以我编写了自己的自定义处理程序。并按照文档(以及各种 stackoverflow 答案)中的描述正确标记它。

但是,我的处理程序一直被 JMS 序列化程序包附带的 ConstraintViolationList 的默认处理程序覆盖。

我已正确标记我的处理程序服务。事实上,当我注释掉 vendor/jms/serializer-bundle/JMS/SerializerBundle/Resources/config/services.xml 中的 ms_serializer.constraint_violation_handler 服务定义时,我的处理程序服务被正确检测和使用

如何阻止默认处理程序覆盖我的自定义处理程序?

我什至尝试过覆盖我自己的包中的 jms_serializer.constraint_violation_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 的版本覆盖。

解决方案:将您的包放在 AppKernel.php 的最底部,如下所示:

public function registerBundles()
{
    $bundles = [
        // .......
        new JMS\SerializerBundle\JMSSerializerBundle(),
        new My\Bundle\MyAwesomeBundle()
    ];

    return $bundles;
}

关于php - 无法覆盖 Symfony ConstraintViolationList 的 JMS 序列化程序的默认处理程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37977816/

相关文章:

c# - EF4 将 DynamicProxies 转换为基础对象

php - Eloquent Laravel - 仅当关系存在时才检索结果

php - 有没有一种方法可以在上传到服务器时自行编码视频

xml - Symfony2 - 为什么使用 XML 进行设置/配置?

database - 我可以使用数据转换器来组合 symfony2 中表单中的字段吗

Symfony 与 docker,编辑麻烦

java - 套接字编程序列化对象

Java 将对象映射到 JSON 的几种变体

php - Laravel Backpack 进入新的时间领域 从 1970 年开始日历

php - 用符号替换数据库查询中的冗余日期(MySQL/PHP)