Code Monkey home page Code Monkey logo

typescriptgeneratorbundle's Introduction

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:

$ 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.

typescriptgeneratorbundle's People

Contributors

janit avatar alsbury avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.