Download the PHP package janit/typescript-generator-bundle without Composer
On this page you can find all versions of the php package janit/typescript-generator-bundle. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download janit/typescript-generator-bundle
More information about janit/typescript-generator-bundle
Files in janit/typescript-generator-bundle
Package typescript-generator-bundle
Short Description A Symfony Bundle that provides a command to generate TypeScript interfaces from PHP classes
License MIT
Informations about the package typescript-generator-bundle
PHP Classes to TypeScript Interfaces Generator Bundle
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:
After this you'll need to activate the bundle in your app/AppKernel.php
file:
Once this is done you should have the added command in place in your console and you can run:
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:
Once this is in place you can run the command with the argument src/
:
This will generate the following file typescript/Person.d.ts
with the following content:
If you provide another argument (toDir
) you can change the target directory to something else.