janit/typescript-generator-bundle
Composer 安装命令:
composer require janit/typescript-generator-bundle
包简介
A Symfony Bundle that provides a command to generate TypeScript interfaces from PHP classes
README 文档
README
A Symfony bundle that adds a command to extract TypeScript interface from PHP classes. Based on the example from Martin Vseticka this bundle uses the PHP-Parser library and annotations. This is currently very basic feature wise, but it does work.
TypeScript is a superscript of JavaScript that adds strong typing and other features on top of JS. Automatically generated classes can be useful, for example when using a simple JSON API to communicate to a JavaScript client. This way you can get typing for your API responses in an easy way.
This is currently tightly coupled to the Symfony Framework, but could be extracted. Feel free to build on this or use as inspiration to build something completely different.
Installation
As a Symfony bundle you'll need to start by add the package to your project with composer:
$ composer req janit/typescript-generator-bundle
After this you'll need to activate the bundle in your app/AppKernel.php file:
new Janit\TypeScriptGeneratorBundle\TypeScriptGeneratorBundle()
Once this is done you should have the added command in place in your console and you can run:
$ php bin/console typescript:generate-interfaces
This will yield an error because the command expects fromDir as a parameter on where to scan for PHP classes.
NOTE: These instructions are for Symfony Standard Edition (3.3), but the bundle should work with Symfony Flex as well.
Usage
The command scans directories recursively for all .php files. It will only generate Type Definitions (interfaces) for files with appropriate annotations.
To generate interfaces, create a new class in src/AppBundle/Entity/Person.php and enter the following:
<?php namespace AppBundle\Entity; /** * @TypeScriptMe */ class Person { /** * @var string */ public $firstName; /** * @var string */ public $lastName; /** * @var int */ public $age; }
Once this is in place you can run the command with the argument src/:
$ php bin/console typescript:generate-interfaces src/
This will generate the following file typescript/Person.d.ts with the following content:
interface Person {
firstName: string,
lastName: string,
age: number
}
declare var Person: Person;
If you provide another argument (toDir) you can change the target directory to something else.
janit/typescript-generator-bundle 适用场景与选型建议
janit/typescript-generator-bundle 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 461 次下载、GitHub Stars 达 16, 最近一次更新时间为 2017 年 11 月 04 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 janit/typescript-generator-bundle 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 janit/typescript-generator-bundle 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 461
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 16
- 点击次数: 7
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2017-11-04