定制 componenta/serialize-interceptor 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

componenta/serialize-interceptor

Composer 安装命令:

composer require componenta/serialize-interceptor

包简介

Symfony Serializer result interceptor for Componenta

README 文档

README

Result interceptor for componenta/interceptor that serializes callable return values through Symfony\Component\Serializer\SerializerInterface.

Русская документация

Boundary

This package contains only the #[Serialize] attribute and SerializeInterceptor. It does not configure Symfony Serializer for the application: the container must provide SerializerInterface.

Use componenta/interceptor directly when you only need the callable interceptor pipeline.

Installation

composer require componenta/serialize-interceptor

Requirements

  • PHP 8.4+
  • componenta/interceptor
  • symfony/serializer

Quick Start

use Componenta\Interceptor\Http\Attribute\Respond;
use Componenta\Interceptor\Serialization\Attribute\Serialize;

final class UserController
{
    #[Respond(200, 'application/json')]
    #[Serialize(context: ['groups' => ['user:read']])]
    public function show(): User
    {
        return $this->user;
    }
}

#[Serialize] extends Componenta\Interceptor\Attribute\Intercept, so it is read by the standard AttributeInterceptor. SerializeInterceptor is created through the DI factory, and SerializerInterface is resolved from the container.

Dynamic Context

Context can be built from already resolved method parameters. The array key is the parameter name, and the value is the public property name:

use Componenta\Interceptor\Serialization\Attribute\Serialize;

final class PostController
{
    #[Serialize(context: [
        'query' => 'includeAuthor',
        Serialize::ATTR_MAP => ['includeAuthor' => 'with_author'],
    ])]
    public function index(PostListQuery $query): array
    {
        return $this->posts->list($query);
    }
}

If $query->includeAuthor is true, serializer context receives ['with_author' => true].

Return-Level Context

A method may return special keys when context depends on the result:

use Componenta\Interceptor\Serialization\Attribute\Serialize;

return [
    Serialize::ATTR_DATA => $post,
    Serialize::ATTR_CONTEXT => ['groups' => ['post:detail']],
];

ATTR_CONTEXT is merged with attribute context and wins on duplicate keys.

Ordering With Other Interceptors

If the result must become an HTTP response, place the response interceptor above #[Serialize]:

#[Respond(200, 'application/json')] // outer layer
#[Serialize]                        // inner layer, receives the raw result first
public function show(): User {}

Details: componenta/interceptor describes layer ordering and AttributeInterceptor.

License

MIT

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-06-17

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固