riverwaysoft/php-converter
Composer 安装命令:
composer require riverwaysoft/php-converter
包简介
PHP converter to TypeScript / Dart
README 文档
README
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
- Installation
composer require riverwaysoft/php-converter --dev
If the installation leads to dependency conflicts, consider using the standalone Phar version of the package.
- 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; }
- 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
riverwaysoft/php-converter 适用场景与选型建议
riverwaysoft/php-converter 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 55.65k 次下载、GitHub Stars 达 9, 最近一次更新时间为 2021 年 08 月 21 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 riverwaysoft/php-converter 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 riverwaysoft/php-converter 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 55.65k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 9
- 点击次数: 16
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: GPL-3.0-or-later
- 更新时间: 2021-08-21
