k-adam / entity-transpiler
该包已被废弃且不再维护。未建议替代包。
将 PHP 类转换为 TypeScript 类/接口
0.0.1
2020-09-13 17:33 UTC
Requires
- php: ^7.2
- doctrine/annotations: ^1.5
- hanneskod/classtools: ^1.2
- splitbrain/php-cli: ^1.1
Requires (Dev)
- phpspec/prophecy: ~1.0
- phpunit/php-code-coverage: 7.0.10
- phpunit/phpunit: 8.2
This package is auto-updated.
Last update: 2021-12-13 21:15:01 UTC
README
EntityTranspiler 将您的 PHP 类转换为前端项目所需的类,因此您无需手动定义它们。例如,它可以生成从 Doctrine 实体到 TypeScript 类的类,但它独立于任何 PHP 框架。
该项目仍在开发中。API 可能会根据反馈进行更改,并简化以方便使用。我旨在在 1.0
版本发布前达到 100% 的测试覆盖率。
安装
使用 Composer
composer require k-adam/entity-transpiler --dev
使用方法
注释您的 PHP 类及其属性,从这些属性中生成前端类定义
use EntityTranspiler\Annotations as ET; /** * @ET\Entity */ class User { /** * @ET\Property(type="int") */ private $id; /** * @ET\Property(type="string") */ private $name; }
创建一个配置文件
<?php use EntityTranspiler\Generators\Utils\ClassResolver\PathResolver; use EntityTranspiler\Utils\ClassRef\Transformer; use EntityTranspiler\Utils\NameFormat\Writer; return [ // Location of your php entity classes "sourceExplorer"=>[ "class" => \EntityTranspiler\SourceExplorers\ClassFinder::class, "config" => ["path"=>"src"] ], // Use docblock annotations, to define entities and properties "loader"=>[ "class" => \EntityTranspiler\Loaders\Annotation::class, "config" => [] ], // Export options "generator"=>[ // Generate typescript classes "class" => \EntityTranspiler\Generators\Typescript::class, "config" => [ // Define associations between source namespace+class names, and the target namespace+class names // Multiple resolve rules can be defined, with different sources "classResolver" => [ [ // Filter source namespace/class // "source" => "App\\*" "source" => "*", // Output options "pathResolver" => [ "type" => PathResolver::TYPE_DIRECTORY, "path" => "output", "dirNameFormat" => Writer::KEBAB_CASE, "fileNameFormat" => Writer::KEBAB_CASE ], // Class/enum name format "classNameResolver" => ["format"=>Writer::PASCAL_CASE], "enumResolver" => ["propertyNameFormat"=>Writer::PASCAL_CASE], // Transform namespace+class names before export "transformer" => [ "type" => Transformer::TYPE_COMPOSITION, "transformers" => [ [ // Skip top level ( App ) namespace in path "type" => Transformer::TYPE_SLICE_NAMESPACE, "offset" => 1 ], [ // Keep the currently top level namespaces: Shop/Ticketing (offset:1) // Concatenate the subnamespaces with the classnames ( Shop\Cart\Entry -> Shop\CartEntry ) "type" => Transformer::TYPE_PREPEND_NAMESPACE, "offset" => 1 ] ] ] ] ] ] ] ];
运行实体转换器
vendor/bin/entity-transpiler --config=LOCATION_OF_YOUR_CONFIG_FILE
结果应如下所示
export class User { id: number; name: string; }
示例项目
更多高级示例,请参阅 示例项目
待办事项
项目中仍缺少一些功能,但我计划在未来实施它们
- 可配置的继承映射
- 接口
- 枚举作为对象键
- 在 Php 7.4 版本中使用协变返回和逆变参数(PathResolver / Transformer 等...)