wayofdev / laravel-symfony-serializer
📦 Laravel 对 Symfony Serializer 的封装。
Requires
- php: ^8.2
- illuminate/contracts: ^11.0
- illuminate/http: ^11.0
- illuminate/support: ^11.0
- ramsey/uuid: ^4.7
- symfony/property-access: ^7.1
- symfony/property-info: ^7.1
- symfony/serializer: ^7.1
Requires (Dev)
- ergebnis/phpunit-slow-test-detector: ^2.14
- larastan/larastan: ^2.9
- orchestra/testbench: ^9.1
- pestphp/pest: ^2.34
- pestphp/pest-plugin-laravel: ^2.4
- phpstan/extension-installer: ^1.4
- phpstan/phpstan: ^1.11
- phpstan/phpstan-deprecation-rules: ^1.2
- phpstan/phpstan-phpunit: ^1.4
- phpstan/phpstan-strict-rules: ^1.6
- phpunit/phpunit: ^10.5
- psalm/plugin-laravel: ^2.11
- psalm/plugin-phpunit: ^0.19
- rector/rector: ^1.1
- roave/infection-static-analysis-plugin: ^1.35
- symfony/yaml: ^7.1
- vimeo/psalm: ^5.24
- wayofdev/cs-fixer-config: ^1.5
Suggests
- symfony/yaml: Required only if YamlEncoder support is needed.
- dev-master
- v2.1.1
- v2.1.0
- v2.0.0
- 1.x-dev
- v1.2.60
- v1.2.59
- v1.2.58
- v1.2.57
- v1.2.56
- v1.2.55
- v1.2.54
- v1.2.53
- v1.2.52
- v1.2.51
- v1.2.50
- v1.2.49
- v1.2.48
- v1.2.47
- v1.2.46
- v1.2.45
- v1.2.44
- v1.2.43
- v1.2.42
- v1.2.41
- v1.2.40
- v1.2.39
- v1.2.38
- v1.2.37
- v1.2.36
- v1.2.35
- v1.2.34
- v1.2.33
- v1.2.32
- v1.2.31
- v1.2.30
- v1.2.29
- v1.2.28
- v1.2.27
- v1.2.26
- v1.2.25
- v1.2.24
- v1.2.23
- v1.2.22
- v1.2.21
- v1.2.20
- v1.2.19
- v1.2.18
- v1.2.17
- v1.2.16
- v1.2.15
- v1.2.14
- v1.2.13
- v1.2.12
- v1.2.11
- v1.2.10
- v1.2.9
- v1.2.8
- v1.2.7
- v1.2.6
- v1.2.5
- v1.2.4
- v1.2.3
- v1.2.2
- v1.2.1
- v1.2.0
- v1.1.25
- v1.1.24
- v1.1.23
- v1.1.22
- v1.1.21
- v1.1.20
- v1.1.19
- v1.1.18
- v1.1.17
- v1.1.16
- v1.1.15
- v1.1.14
- v1.1.13
- v1.1.12
- v1.1.11
- v1.1.10
- v1.1.9
- v1.1.8
- v1.1.7
- v1.1.6
- v1.1.5
- v1.1.4
- v1.1.3
- v1.1.2
- v1.1.1
- v1.1.0
- v1.0.3
- v1.0.2
- v1.0.1
- v1.0.0
- dev-renovate/symfony
- dev-renovate/phpstan-packages
- dev-renovate/wayofdev-gh-actions-3.x
- dev-renovate/wagoid-commitlint-github-action-6.x
- dev-renovate/ergebnis-phpunit-slow-test-detector-2.x-lockfile
- dev-renovate/pestphp-pest-2.x-lockfile
- dev-renovate/phpunit-phpunit-11.x
- dev-renovate/shivammathur-setup-php-2.x
- dev-release-please--branches--master--components--laravel-symfony-serializer
- dev-renovate/rector-rector-1.x-lockfile
- dev-renovate/orchestra-testbench-9.x-lockfile
This package is auto-updated.
Last update: 2024-09-21 08:24:20 UTC
README
Laravel Symfony Serializer
此包将 Symfony Serializer 组件集成到 Laravel 中,提供了一个强大的工具,可以将对象序列化和反序列化为各种格式,如 JSON、XML、CSV 和 YAML。
有关 Symfony Serializer 的详细文档可以在其 官方页面 上找到。
🗂️ 目录
🤔 目的
此包将 Symfony Serializer 组件的强大功能带入 Laravel。虽然 Laravel 没有内置的序列化器,通常依赖于数组或 JSON 转换,但此包提供了更高级的序列化功能。这包括对象归一化、处理循环引用、属性分组和特定格式的编码器。
如果您正在构建 REST API、处理队列或具有复杂的序列化需求,此包将特别有用。它允许您使用对象而不是简单的数组作为有效负载,并支持各种格式,如 JSON、XML、CSV 和 YAML。此文档将指导您完成安装过程,并提供如何使用此包进行对象序列化和反序列化的示例。
🙏 如果您认为此存储库很有用,请考虑给它一个 ⭐️。谢谢!
💿 安装
将包作为依赖项要求
composer require wayofdev/laravel-symfony-serializer
您可以使用以下命令发布配置文件
$ php artisan vendor:publish \ --provider="WayOfDev\Serializer\Bridge\Laravel\Providers\SerializerServiceProvider" \ --tag="config"
🔧 配置
包配置文件允许您自定义序列化过程的各个方面。
以下是包提供的默认配置
<?php declare(strict_types=1); use Symfony\Component\Serializer\Mapping\Loader\LoaderInterface; use WayOfDev\Serializer\Contracts\EncoderRegistrationStrategy; use WayOfDev\Serializer\Contracts\NormalizerRegistrationStrategy; use WayOfDev\Serializer\DefaultEncoderRegistrationStrategy; use WayOfDev\Serializer\DefaultNormalizerRegistrationStrategy; /** * @return array{ * default: string, * debug: bool, * normalizerRegistrationStrategy: class-string<NormalizerRegistrationStrategy>, * encoderRegistrationStrategy: class-string<EncoderRegistrationStrategy>, * metadataLoader: class-string<LoaderInterface>|null, * } */ return [ 'default' => env('SERIALIZER_DEFAULT_FORMAT', 'symfony-json'), 'debug' => env('SERIALIZER_DEBUG_MODE', env('APP_DEBUG', false)), 'normalizerRegistrationStrategy' => DefaultNormalizerRegistrationStrategy::class, 'encoderRegistrationStrategy' => DefaultEncoderRegistrationStrategy::class, 'metadataLoader' => null, ];
→ 配置选项
default
:指定默认序列化器格式。可以通过设置SERIALIZER_DEFAULT_FORMAT
环境变量来覆盖。默认为symfony-json
。debug
:为ProblemNormalizer
启用调试模式。可以使用SERIALIZER_DEBUG_MODE
环境变量设置。默认为APP_DEBUG
的值。normalizerRegistrationStrategy
:指定注册归一化器的策略类。默认策略为WayOfDev\Serializer\DefaultNormalizerRegistrationStrategy
。encoderRegistrationStrategy
:指定注册编码器的策略类。默认策略为WayOfDev\Serializer\DefaultEncoderRegistrationStrategy
。metadataLoader
:允许注册自定义元数据加载器。默认情况下,使用Symfony\Component\Serializer\Mapping\Loader\AttributeLoader
。
→ 自定义策略
由于 Laravel 的缓存限制,其中配置无法实例化对象,因此此包使用策略来注册归一化器和编码器。[链接](https://elliotderhay.com/blog/caching-laravel-configs-that-use-objects)
您可以通过实现相应的接口来创建自定义的标准化器或编码器注册策略。
标准化器注册策略
要创建自定义的标准化器注册策略
-
实现
NormalizerRegistrationStrategy
接口<?php declare(strict_types=1); namespace Infrastructure\Serializer; use Symfony\Component\Serializer\Mapping\Loader\LoaderInterface; use Symfony\Component\Serializer\Normalizer; use Symfony\Component\Serializer\Normalizer\DenormalizerInterface; use Symfony\Component\Serializer\Normalizer\NormalizerInterface; use WayOfDev\Serializer\Contracts\NormalizerRegistrationStrategy; // ... final readonly class CustomNormalizerRegistrationStrategy implements NormalizerRegistrationStrategy { public function __construct( private LoaderInterface $loader, private bool $debugMode = false, ) { } /** * @return iterable<array{normalizer: NormalizerInterface|DenormalizerInterface, priority: int<0, max>}> */ public function normalizers(): iterable { // ... } }
-
修改
serializer.php
配置以使用您的自定义策略'normalizerRegistrationStrategy' => CustomNormalizerRegistrationStrategy::class,
编码器注册策略
要创建自定义的编码器注册策略
-
实现
EncoderRegistrationStrategy
接口<?php declare(strict_types=1); namespace Infrastructure\Serializer; use Symfony\Component\Serializer\Encoder; use Symfony\Component\Serializer\Encoder\DecoderInterface; use Symfony\Component\Serializer\Encoder\EncoderInterface; use Symfony\Component\Yaml\Dumper; use function class_exists; final class CustomEncoderRegistrationStrategy implements Contracts\EncoderRegistrationStrategy { /** * @return iterable<array{encoder: EncoderInterface|DecoderInterface}> */ public function encoders(): iterable { // Register your encoders here... yield ['encoder' => new Encoder\JsonEncoder()]; yield ['encoder' => new Encoder\CsvEncoder()]; yield ['encoder' => new Encoder\XmlEncoder()]; if (class_exists(Dumper::class)) { yield ['encoder' => new Encoder\YamlEncoder()]; } } }
-
修改
serializer.php
配置以使用您的自定义策略'encoderRegistrationStrategy' => CustomEncoderRegistrationStrategy::class,
💻 使用方法
该软件包提供了一系列序列化器,可用于序列化和反序列化对象。
本软件包中可用的默认序列化器有:symfony-json
、symfony-csv
、symfony-xml
、symfony-yaml
。
警告
要使用 yaml
编码器,需要安装 symfony/yaml
包,且在包未安装时默认禁用。安装 symfony/yaml
包后,编码器将自动启用。
→ 组件
SerializerManager
SerializerManager
处理本软件包中可用的不同序列化器。它可以用于序列化和反序列化对象。
ResponseFactory
ResponseFactory
用于在 Laravel 控制器中创建响应,使将序列化数据包含在 HTTP 响应中变得简单。
Facades
本软件包包括两个 Laravel Facades
Manager
- 用于访问底层的SerializerManager
Serializer
- 用于访问绑定和配置的原始 Symfony 序列化器实例。
→ 示例 DTO
我们将使用此示例 DTO 进行序列化
<?php namespace Application\User; use Symfony\Component\Serializer\Annotation\Groups; use Symfony\Component\Serializer\Annotation\SerializedName; class UserDTO { #[Groups(['public'])] #[SerializedName('id')] private int $id; #[Groups(['public'])] #[SerializedName('name')] private string $name; #[Groups(['private', 'public'])] #[SerializedName('emailAddress')] private string $email; public function __construct(int $id, string $name, string $email) { $this->id = $id; $this->name = $name; $this->email = $email; } public function id(): int { return $this->id; } public function name(): string { return $this->name; } public function email(): string { return $this->email; } }
→ 在服务类中使用 SerializerManager
<?php namespace Application\Services; use WayOfDev\Serializer\Manager\SerializerManager; use Application\User\UserDTO; class ProductService { public function __construct( private readonly SerializerManager $serializer, ) { } public function someMethod(): void { $serializer = $this->serializer->serializer('symfony-json'); $dto = new UserDTO(1, 'John Doe', 'john@example.com'); $serialized = $serializer->serialize( payload: $dto, context: ['groups' => ['private']] ); } }
→ 在 Laravel 控制器中使用 ResponseFactory
以下是如何在 Laravel 控制器中使用 ResponseFactory
的示例
示例控制器
<?php namespace Bridge\Laravel\Public\Product\Controllers; use Application\User\UserDTO; use Illuminate\Http\Request; use WayOfDev\Serializer\Bridge\Laravel\Http\HttpCode; use WayOfDev\Serializer\Bridge\Laravel\Http\ResponseFactory; class UserController extends Controller { public function __construct(private ResponseFactory $response) { } public function index() { $dto = new UserDTO(1, 'John Doe', 'john@example.com'); $this->response->withContext(['groups' => ['private']]); $this->response->withStatusCode(HttpCode::HTTP_OK); return $this->response->create($dto); } }
→ 在 Laravel 队列中使用
要切换到 Laravel 默认序列化与该实现之间的队列,您可以在您的队列作业中覆盖 __serialize
和 __unserialize
方法。以下是一个示例
<?php declare(strict_types=1); namespace Bridge\Laravel\Public\Product\Jobs; use Domain\Product\Models\Product; use Domain\Product\ProductProcessor; use Illuminate\Bus\Queueable; use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Foundation\Bus\Dispatchable; use Illuminate\Queue\InteractsWithQueue; use Illuminate\Queue\SerializesModels; use WayOfDev\Serializer\Bridge\Laravel\Facades\Manager; /** * This Job class shows how Symfony Serializer can be used with Laravel Queues. */ class ProcessProductJob implements ShouldQueue { use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; public Product $product; public function __construct(Product $product) { $this->product = $product; } public function handle(ProductProcessor $processor): void { $processor->process($this->product); } public function __serialize(): array { return [ 'product' => Manager::serialize($this->product), ]; } public function __unserialize(array $values): void { $this->product = Manager::deserialize($values['product'], Product::class); } }
🔒 安全策略
该项目有一个 安全策略。
🙌 想要贡献?
感谢您考虑为 wayofdev 社区做出贡献!我们欢迎所有类型的贡献。如果您想
您非常欢迎。在贡献之前,请查看我们的 贡献指南。
🫡 贡献者
🌐 社交链接
📜 许可证
🧱 信用和有用资源
此存储库受到以下项目的启发