Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,6 @@ composer.phar
# Commit your application's lock file https://getcomposer.org/doc/01-basic-usage.md#commit-your-composer-lock-file-to-version-control
# You may choose to ignore a library lock file http://getcomposer.org/doc/02-libraries.md#lock-file
# composer.lock
var/

.phpunit.result.cache
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Changelog
======

# 1.0.0

- Initial Release of the Parser
100 changes: 99 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,99 @@
# php-to-typescript
PHP To TypeScript Parser
======

A library which can be used to create TypeScript classes/interfaces based on PHP classes. Main use case is in a scenario where a PHP backend is consumed by a JavaScript/TypeScript frontend and serialized DTOs are consumed.

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.

Feel free to build on this or use as inspiration to build something completely different.

## Installation

```bash
$ composer require paneon/php-to-typescript
```

## Features

WIP

#### Example source file:
```php
<?php

/**
* @TypeScriptInterface
*/
class Example
{
/**
* @var string
*/
public $firstName;

/**
* @var string|null
*/
public $middleName;

/**
* @var string
*/
public $lastName;

/**
* @var int|null
*/
public $age;

/** @var Contact[] */
public $contacts;
}
```

#### Default output file:

```typescript
interface Example {
firstName: string;
middleName: string;
lastName: string;
age: number;
contacts: Contact[];
}
```

## Null-aware Types
Since [TypeScript 2.0](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-0.html#null--and-undefined-aware-types)
Null and optional/undefined types are supported. In the generator bundle, this is an optional feature and null types will be removed by default. To include nullable types use
```bash
$ php bin/console typescript:generate --nullable
```


#### Output file with null types:

```typescript
interface Example {
firstName: string;
middleName: string|null;
lastName: string;
age: number;
contacts: Contact[];
}
```


## Usage of the Command 'typescript:generate-single'

The purpose of the generate Command is to create TypeScript definitions for Classes from external packages where you
can't add the TypeScriptInterface-Annotation but their classes are for example used in your classes.
It will only affect a single file and needs a specific target location if you don't want it directly inside assets/js/interfaces.

```bash
$ php bin/console typescript:generate-single vendor/shopping/s2-communication-bundle/src/CommunicationBundle/DTO/ProductTeaser.php assets/js/interfaces/s2-communication-bundle/DTO/
```

It's recommended to trigger the generation of theses interfaces after `composer update/install`.
44 changes: 44 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
{
"name": "paneon/php-to-typescript",
"description": "Generate TypeScript classes and interfaces based on PHP classes.",
"type": "library",
"license": "MIT",
"autoload": {
"psr-4": {
"Paneon\\PhpToTypeScript\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"Paneon\\PhpToTypeScript\\": "src/",
"Paneon\\PhpToTypeScript\\Tests\\": "tests/"
}
},
"authors": [
{
"name": "Alexander Pape",
"email": "a.pape@paneon.de"
}
],
"require": {
"doctrine/annotations": "^1.6",
"nikic/php-parser": "^3.1.0|^4.0.0",
"monolog/monolog": "^1.24|^2"
},
"require-dev": {
"phpstan/phpstan": "^1.2",
"phpunit/phpunit": "^8"
},
"scripts": {
"build": [
"@lint",
"@test"
],
"lint": [
"@php vendor/bin/phpstan analyze src --level=5"
],
"test": [
"vendor/bin/phpunit"
]
}
}
Loading