2017 © Pedro Peláez
 

symfony-bundle typescript-generator-bundle

A Symfony Bundle that provides a command to generate TypeScript interfaces from PHP classes

image

janit/typescript-generator-bundle

A Symfony Bundle that provides a command to generate TypeScript interfaces from PHP classes

  • Saturday, November 4, 2017
  • by janit
  • Repository
  • 1 Watchers
  • 0 Stars
  • 27 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 1 Open issues
  • 11 Versions
  • 23 % Grown

The README.md

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., (*1)

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., (*2)

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., (*3)

Installation

As a Symfony bundle you'll need to start by add the package to your project with composer:, (*4)

$ composer req janit/typescript-generator-bundle

After this you'll need to activate the bundle in your app/AppKernel.php file:, (*5)

new Janit\TypeScriptGeneratorBundle\TypeScriptGeneratorBundle()

Once this is done you should have the added command in place in your console and you can run:, (*6)

$ 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., (*7)

NOTE: These instructions are for Symfony Standard Edition (3.3), but the bundle should work with Symfony Flex as well., (*8)

Usage

The command scans directories recursively for all .php files. It will only generate Type Definitions (interfaces) for files with appropriate annotations., (*9)

To generate interfaces, create a new class in src/AppBundle/Entity/Person.php and enter the following:, (*10)

<?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/:, (*11)

$ php bin/console typescript:generate-interfaces src/

This will generate the following file typescript/Person.d.ts with the following content:, (*12)

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., (*13)

The Versions

04/11 2017

dev-master

9999999-dev

A Symfony Bundle that provides a command to generate TypeScript interfaces from PHP classes

  Sources   Download

MIT

The Requires

 

by Jani Tarvainen

04/11 2017

v0.7

0.7.0.0

A Symfony Bundle that provides a command to generate TypeScript interfaces from PHP classes

  Sources   Download

MIT

The Requires

 

by Jani Tarvainen

04/11 2017

v1.0

1.0.0.0

A Symfony Bundle that provides a command to generate TypeScript interfaces from PHP classes

  Sources   Download

MIT

The Requires

 

by Jani Tarvainen

04/11 2017

v0.6

0.6.0.0

A Symfony Bundle that provides a command to generate TypeScript interfaces from PHP classes

  Sources   Download

MIT

The Requires

 

by Jani Tarvainen

04/11 2017

v0.5.2

0.5.2.0

A Symfony Bundle that provides a command to generate TypeScript interfaces from PHP classes

  Sources   Download

MIT

The Requires

 

by Jani Tarvainen

04/11 2017

v0.5.1

0.5.1.0

A Symfony Bundle that provides a command to generate TypeScript interfaces from PHP classes

  Sources   Download

MIT

The Requires

 

by Jani Tarvainen

04/11 2017

v0.5

0.5.0.0

A Symfony Bundle that provides a command to generate TypeScript interfaces from PHP classes

  Sources   Download

MIT

The Requires

 

by Jani Tarvainen

04/11 2017

v0.4

0.4.0.0

A Symfony Bundle that provides a command to generate TypeScript interfaces from PHP classes

  Sources   Download

MIT

The Requires

 

by Jani Tarvainen

04/11 2017

v0.3

0.3.0.0

A Symfony Bundle that provides a command to generate TypeScript interfaces from PHP classes

  Sources   Download

MIT

The Development Requires

by Jani Tarvainen

04/11 2017

v0.1

0.1.0.0

A Symfony Bundle that provides a command to generate TypeScript interfaces from PHP classes

  Sources   Download

MIT

The Development Requires

by Jani Tarvainen

04/11 2017

v0.2

0.2.0.0

A Symfony Bundle that provides a command to generate TypeScript interfaces from PHP classes

  Sources   Download

MIT

The Development Requires

by Jani Tarvainen