owlnext-fr/dto-export 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

owlnext-fr/dto-export

最新稳定版本:1.0.2

Composer 安装命令:

composer require owlnext-fr/dto-export

包简介

Symfony + API Platform vendor for exporting API DTOs into other languages for front-end easy integration.

README 文档

README

Symfony + API Platform vendor for exporting API DTOs into other languages for front-end easy integration.

Installation

1. Install the bundle

composer require owlnext-fr/dto-export

2. Configure twig paths

# config/packages/twig.yaml
twig:
  #...
  paths:
    '%kernel.project_dir%/vendor/owlnext-fr/dto-export/templates': 'owlnext_fr.dto_export'

Usage

1. Create a DTO class

# src/Dto/Output/UserOutputDTO.php
<?php

namespace App\Dto\Output;

class UserOutputDTO {
    public int $id;
    public string $name;
}

2. Add the tag app.exportable_dto to your class

With annotation

# src/Dto/Output/UserOutputDTO.php

use Symfony\Component\DependencyInjection\Attribute\AutoconfigureTag;

#[AutoconfigureTag('app.exportable_dto')]
class UserOutputDTO {
    public int $id;
    public string $name;
}

With interface implementation

To avoid the use of the AutoconfigureTag annotation in each of your DTOs, you can create a ExportableDTOInterface interface and implement it in your DTOs.

# src/Dto/Impl/ExportableDTOInterface.php

namespace App\Dto\Impl;

use Symfony\Component\DependencyInjection\Attribute\AutoconfigureTag;

#[AutoconfigureTag('app.exportable_dto')]
interface ExportableDTOInterface {}
# src/Dto/Output/UserOutputDTO.php

use App\Dto\Impl\ExportableDTOInterface;

class UserOutputDTO implements ExportableDTOInterface {
    public int $id;
    public string $name;
}

handle array of something

For array of primitives, scalars and object, you will have to add another attribute to your field using an array. It will help the exporter to know what type of data is in the array and transform it correctly.

Here is an example with an array of strings:

# src/Dto/Output/UserOutputDTO.php

# ...
use OwlnextFr\DtoExport\Attribute\ListOf;

class UserOutputDTO implements ExportableDTOInterface {
    #...
    
    #[ListOf(type: 'string')]
    /** @var string[] $roles list roles for this user. */
    public array $roles;
}

Here is an example with an array of objects:

# src/Dto/Output/UserOutputDTO.php

# ...
use OwlnextFr\DtoExport\Attribute\ListOf;
use App\DTO\Output\SkillOutputDTO;

class UserOutputDTO implements ExportableDTOInterface {
    #...
    
    #[ListOf(type: SkillOutputDTO::class)]
    /** @var SkillOutputDTO[] $roles list skills of this user. */
    public array $skills;
}

3. Export your DTOs

Basic usage:

symfony console dto:export ...

For dart language:

symfony console dto:export <path to export> --type=dart --project-name=<project name>

For typescript language:

symfony console dto:export <path to export> --type=typescript

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-10-18

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固