Skip to content

Commit 7d9926f

Browse files
authored
Merge pull request #1 from Paneon/develop
Release
2 parents d7f6c89 + 4e9cfed commit 7d9926f

20 files changed

+3404
-1
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,6 @@ composer.phar
44
# Commit your application's lock file https://getcomposer.org/doc/01-basic-usage.md#commit-your-composer-lock-file-to-version-control
55
# You may choose to ignore a library lock file http://getcomposer.org/doc/02-libraries.md#lock-file
66
# composer.lock
7+
var/
8+
9+
.phpunit.result.cache

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
Changelog
2+
======
3+
4+
# 1.0.0
5+
6+
- Initial Release of the Parser

README.md

Lines changed: 99 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,99 @@
1-
# php-to-typescript
1+
PHP To TypeScript Parser
2+
======
3+
4+
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.
5+
6+
TypeScript is a superscript of JavaScript that adds strong typing and other features on top of JS.
7+
Automatically generated classes can be useful, for example when using a simple JSON API to communicate to a JavaScript client.
8+
This way you can get typing for your API responses in an easy way.
9+
10+
Feel free to build on this or use as inspiration to build something completely different.
11+
12+
## Installation
13+
14+
```bash
15+
$ composer require paneon/php-to-typescript
16+
```
17+
18+
## Features
19+
20+
WIP
21+
22+
#### Example source file:
23+
```php
24+
<?php
25+
26+
/**
27+
* @TypeScriptInterface
28+
*/
29+
class Example
30+
{
31+
/**
32+
* @var string
33+
*/
34+
public $firstName;
35+
36+
/**
37+
* @var string|null
38+
*/
39+
public $middleName;
40+
41+
/**
42+
* @var string
43+
*/
44+
public $lastName;
45+
46+
/**
47+
* @var int|null
48+
*/
49+
public $age;
50+
51+
/** @var Contact[] */
52+
public $contacts;
53+
}
54+
```
55+
56+
#### Default output file:
57+
58+
```typescript
59+
interface Example {
60+
firstName: string;
61+
middleName: string;
62+
lastName: string;
63+
age: number;
64+
contacts: Contact[];
65+
}
66+
```
67+
68+
## Null-aware Types
69+
Since [TypeScript 2.0](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-0.html#null--and-undefined-aware-types)
70+
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
71+
```bash
72+
$ php bin/console typescript:generate --nullable
73+
```
74+
75+
76+
#### Output file with null types:
77+
78+
```typescript
79+
interface Example {
80+
firstName: string;
81+
middleName: string|null;
82+
lastName: string;
83+
age: number;
84+
contacts: Contact[];
85+
}
86+
```
87+
88+
89+
## Usage of the Command 'typescript:generate-single'
90+
91+
The purpose of the generate Command is to create TypeScript definitions for Classes from external packages where you
92+
can't add the TypeScriptInterface-Annotation but their classes are for example used in your classes.
93+
It will only affect a single file and needs a specific target location if you don't want it directly inside assets/js/interfaces.
94+
95+
```bash
96+
$ php bin/console typescript:generate-single vendor/shopping/s2-communication-bundle/src/CommunicationBundle/DTO/ProductTeaser.php assets/js/interfaces/s2-communication-bundle/DTO/
97+
```
98+
99+
It's recommended to trigger the generation of theses interfaces after `composer update/install`.

composer.json

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
{
2+
"name": "paneon/php-to-typescript",
3+
"description": "Generate TypeScript classes and interfaces based on PHP classes.",
4+
"type": "library",
5+
"license": "MIT",
6+
"autoload": {
7+
"psr-4": {
8+
"Paneon\\PhpToTypeScript\\": "src/"
9+
}
10+
},
11+
"autoload-dev": {
12+
"psr-4": {
13+
"Paneon\\PhpToTypeScript\\": "src/",
14+
"Paneon\\PhpToTypeScript\\Tests\\": "tests/"
15+
}
16+
},
17+
"authors": [
18+
{
19+
"name": "Alexander Pape",
20+
"email": "[email protected]"
21+
}
22+
],
23+
"require": {
24+
"doctrine/annotations": "^1.6",
25+
"nikic/php-parser": "^3.1.0|^4.0.0",
26+
"monolog/monolog": "^1.24|^2"
27+
},
28+
"require-dev": {
29+
"phpstan/phpstan": "^1.2",
30+
"phpunit/phpunit": "^8"
31+
},
32+
"scripts": {
33+
"build": [
34+
"@lint",
35+
"@test"
36+
],
37+
"lint": [
38+
"@php vendor/bin/phpstan analyze src --level=5"
39+
],
40+
"test": [
41+
"vendor/bin/phpunit"
42+
]
43+
}
44+
}

0 commit comments

Comments
 (0)