承接 bluepsyduck/jms-serializer-factory 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

bluepsyduck/jms-serializer-factory

Composer 安装命令:

composer require bluepsyduck/jms-serializer-factory

包简介

A Laminas factory to initialize the JMS serializer through the config.

README 文档

README

GitHub release GitHub build Codecov

This library provides a Laminas-compatible factory to create JMS serializer instances from the config without the need to write an actual factory.

Usage

Install the package through composer with:

composer require bluepsyduck/jms-serializer-factory

The first step is to add the settings to your Laminas config. Use the ConfigKey interface to get the names of the config options. A full list of options can be found below.

<?php
// config/serializers.php

use BluePsyduck\JmsSerializerFactory\Constant\ConfigKey;
use JMS\Serializer\Naming\IdenticalPropertyNamingStrategy;

return [
    'serializers' => [
        'my-fancy-serializer' => [
            ConfigKey::PROPERTY_NAMING_STRATEGY => IdenticalPropertyNamingStrategy::class,
            ConfigKey::HANDLERS => [
                MyFancyHandler::class,
            ],
            ConfigKey::METADATA_DIRS => [
                'My\Fancy\Namespace' => __DIR__ . '/../serializer',
            ],
            ConfigKey::CACHE_DIR => __DIR__ . '/../../data/cache',
        ],
    ],
];

The JMS Serializer Factory will request any dependencies from the container, so make sure to register all of them. If they do not have any dependencies themselves, use the InvokableFactory to register them.

<?php
// config/dependencies.php

use BluePsyduck\JmsSerializerFactory\JmsSerializerFactory;
use Laminas\ServiceManager\Factory\InvokableFactory;
use JMS\Serializer\Naming\IdenticalPropertyNamingStrategy;

return [
    'dependencies' => [
        'factories' => [
            // Add the services used in the serializer config to the container.
            IdenticalPropertyNamingStrategy::class => InvokableFactory::class,
            MyFancyHandler::class => InvokableFactory::class,
            
            // Add the actual serializer instance to the container.
            'MyFancySerializer' => new JmsSerializerFactory('serializers', 'my-fancy-serializer'),
            // This will take the config for the serializer from $config['serializers']['my-fancy-serializer']
        ],
    ],
];

With this configuration, you now can get your serializer instance from the container:

<?php

/* @var \JMS\Serializer\SerializerInterface $myFancySerializer */
$myFancySerializer = $container->get('MyFancySerializer');

// Use it as usual.
$json = $myFancySerializer->serialize($data, 'json');

Alternatively, if the Laminas AutoWire Factory is set up, the Attribute UseJmsSerializer can be used to resolve a parameter of the constructor to the configuration of the serializer:

use BluePsyduck\JmsSerializerFactory\Attribute\UseJmsSerializer;
use JMS\Serializer\SerializerInterface;

class MyFancyClass {
    public function __construct(
        #[UseJmsSerializer('serializers', 'my-fancy-serializer')] // The keys to the configuration of the serializer.
        private SerializerInterface $serializer,
    ) {
    }
}

All config options

The following table shows the full list of config values supported by the factory. Constant refers to the name of the constant in the ConfigKey interface, and the SerializerBuilder method column refers to the method of the builder used for that config value. For further details, please check the method signatures and doc-blocks of the builder.

Constant Expected value SerializerBuilder method
ACCESSOR_STRATEGY container alias ->setAccessorStrategy()
EXPRESSION_EVALUATOR container alias ->setExpressionEvaluator()
TYPE_PARSER container alias ->setTypeParser()
ANNOTATION_READER container alias ->setAnnotationReader()
DEBUG bool ->setDebug()
CACHE_DIR string ->setCacheDir()
ADD_DEFAULT_HANDLERS true ->addDefaultHandlers()
HANDLERS array<container alias> ->configureHandlers()
ADD_DEFAULT_LISTENERS true ->addDefaultListeners()
LISTENERS array<container alias> ->configureListeners()
OBJECT_CONSTRUCTOR container alias ->setObjectConstructor()
PROPERTY_NAMING_STRATEGY container alias ->setPropertyNamingStrategy()
SERIALIZATION_VISITORS array<string, container alias> ->setSerializationVisitor()
DESERIALIZATION_VISITORS array<string, container alias> ->setDeserializationVisitor()
ADD_DEFAULT_SERIALIZATION_VISITORS true ->addDefaultSerializationVisitors()
ADD_DEFAULT_DESERIALIZATION_VISITORS true ->addDefaultDeserializationVisitors()
INCLUDE_INTERFACE_METADATA bool ->includeInterfaceMetadata()
METADATA_DIRS array<string, string> ->setMetadataDirs()
METADATA_DRIVER_FACTORY container alias ->setMetadataDriverFactory()
SERIALIZATION_CONTEXT_FACTORY container alias ->setSerializationContextFactory()
DESERIALIZATION_CONTEXT_FACTORY container alias ->setDeserializationContextFactory()
METADATA_CACHE container alias ->setMetadataCache()
DOC_BLOCK_TYPE_RESOLVER bool ->setDocBlockTypeResolver()
Notes:
  • The expected value container alias means that a string is expected, which is used in the container to retrieve an actual instance to set to the serializer builder. The actually needed type of the instance can be checked in the related method of the SerializerBuilder.
  • The serialization and deserialization visitors get added to the builder together with their format string. The config is expecting the format as key, and an alias to the visitor factory as value.
  • METADATA_DIRS expects the array key to be the namespace and the value to be the directory with the meta config files.
  • The factory does only support classes implementing the SubscribingHandlerInterface as handlers, and classes implementing the EventSubscriberInterface as listeners. It is not possible to use callables for these two cases.

bluepsyduck/jms-serializer-factory 适用场景与选型建议

bluepsyduck/jms-serializer-factory 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 6.34k 次下载、GitHub Stars 达 1, 最近一次更新时间为 2020 年 12 月 31 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「jms」 「serializer」 「factory」 「BluePsyduck」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 bluepsyduck/jms-serializer-factory 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 bluepsyduck/jms-serializer-factory 我们能提供哪些服务?
定制开发 / 二次开发

基于 bluepsyduck/jms-serializer-factory 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 6.34k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 27
  • 依赖项目数: 5
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: GPL-3.0-or-later
  • 更新时间: 2020-12-31