riverwaysoft/php-converter 问题修复 & 功能扩展

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

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

riverwaysoft/php-converter

最新稳定版本:0.9.1

Composer 安装命令:

composer require riverwaysoft/php-converter

包简介

PHP converter to TypeScript / Dart

README 文档

README

Screen Shot 2022-10-07 at 09 04 35

Generates TypeScript & Dart out of your PHP DTO classes.

Why?

Statically typed languages like TypeScript or Dart are great because they allow catching bugs without even running your code. But unless there are well-defined contracts between the API and consumer apps, you may find yourself frequently adjusting outdated typings whenever the API changes. This library generates types for you, enabling you to move faster and encounter fewer bugs.

Requirements

PHP 8.0 or above

Quick start

  1. Installation
composer require riverwaysoft/php-converter --dev

If the installation leads to dependency conflicts, consider using the standalone Phar version of the package.

  1. Mark a few classes with the #[Dto] annotation to convert them into TypeScript or Dart.
use Riverwaysoft\PhpConverter\Filter\Attributes\Dto;

#[Dto]
class UserOutput
{
    public string $id;
    public int $age;
    public ?UserOutput $bestFriend;
    /** @var UserOutput[] */
    public array $friends;
}
  1. Run the CLI command to generate TypeScript
vendor/bin/php-converter --from=/path/to/project/src --to=.

This will generate a file generated.ts with the following content:

type UserOutput = {
  id: string;
  age: number;
  bestFriend: UserOutput | null;
  friends: UserOutput[];
}

Features

  • Supports all PHP data types including union types, nullable types, and enums.
  • Supports PHP DocBlock types, e.g., User[], int[][]|null, and generics thanks to phpstan/phpdoc-parser
  • Custom type resolvers (for instance, for DateTimeImmutable).
  • Generate a single output file or multiple files (1 type per file).
  • Option to override the generation logic.
  • Flexible class filters with the option to use your own filters.
  • Generate API client from Symfony or API Platform code.

Customization

If you'd like to customize the conversion process, you need to copy the config script to your project folder:

cp vendor/riverwaysoft/php-converter/bin/default-config.php config/ts-config.php

Now you can customize this config and run the php-converter using the following script:

vendor/bin/php-converter --from=/path/to/project/src --to=. --config=config/ts-config.php

Documentation

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: GPL-3.0-or-later
  • 更新时间: 2021-08-21

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固