Skip to content

Commit

Permalink
chore: package publishing
Browse files Browse the repository at this point in the history
- Source: https://github.com/lepikhinb/laravel-typescript (Commit: 06f21ffa490657034c88da54f8b7bd76749a3d4f)

BREAK CHANGES
- PHP 8.2, 8.3 support
- Laravel 11 support
- Dropped Laravel 10.x support (the original package is also perfect for this)
- Dropped "Doctrine DBAL"; Package will use Laravel Schema instead of

Feature
- Added custom settings handling to Generator(s)
- Added DB Type to TS file as comment like: /*timestamp*/ | string
    - Added shouldStrictEnum setting to ModelGenerator (default: true)
- DB Enum handling (strict or permissive): "OPTION 1" | "OPTION 2" or string
   - Added shouldDBType setting to ModelGenerator (default: true)
- Sort generated classes by name

Change
- Use Rose special directory structure
- Use Rose special ServiceProvider structure
- New namespaces
- Use PestPHP for tests
  • Loading branch information
rozsazoltan committed Oct 17, 2024
1 parent 32d80cc commit 17a5eab
Show file tree
Hide file tree
Showing 27 changed files with 1,376 additions and 0 deletions.
20 changes: 20 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Docs
.github/ export-ignore
CHANGELOG.md export-ignore
CODE_OF_CONDUCT.md export-ignore
SECURITY.md export-ignore
.editorconfig export-ignore

# Laravel
composer.lock export-ignore
storage/ export-ignore

# Laravel Test
tests/ export-ignore
phpunit.xml export-ignore

# Source Code (Dev)
artisan export-ignore
bootstrap/ export-ignore
config/app.php export-ignore
src/Illuminate/Foundation/Console/* export-ignore
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/vendor/
node_modules/

composer.lock
19 changes: 19 additions & 0 deletions artisan
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/usr/bin/env php
<?php

define('LARAVEL_START', microtime(true));

require __DIR__.'/vendor/autoload.php';

$app = require_once __DIR__.'/bootstrap/app.php';

$kernel = $app->make(Illuminate\Contracts\Console\Kernel::class);

$status = $kernel->handle(
$input = new Symfony\Component\Console\Input\ArgvInput,
new Symfony\Component\Console\Output\ConsoleOutput
);

$kernel->terminate($input, $status);

exit($status);
55 changes: 55 additions & 0 deletions bootstrap/app.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

/*
|--------------------------------------------------------------------------
| Create The Application
|--------------------------------------------------------------------------
|
| The first thing we will do is create a new Laravel application instance
| which serves as the "glue" for all the components of Laravel, and is
| the IoC container for the system binding all of the various parts.
|
*/

$app = new Illuminate\Foundation\Application(
$_ENV['APP_BASE_PATH'] ?? dirname(__DIR__)
);

/*
|--------------------------------------------------------------------------
| Bind Important Interfaces
|--------------------------------------------------------------------------
|
| Next, we need to bind some important interfaces into the container so
| we will be able to resolve them when needed. The kernels serve the
| incoming requests to this application from both the web and CLI.
|
*/

$app->singleton(
Illuminate\Contracts\Http\Kernel::class,
Illuminate\Foundation\Http\Kernel::class
);

$app->singleton(
Illuminate\Contracts\Console\Kernel::class,
Rose\IlluminateTypeScript\Foundation\Console\Kernel::class
);

$app->singleton(
Illuminate\Contracts\Debug\ExceptionHandler::class,
Illuminate\Foundation\Exceptions\Handler::class
);

/*
|--------------------------------------------------------------------------
| Return The Application
|--------------------------------------------------------------------------
|
| This script returns the application instance. The instance is given to
| the calling script so we can separate the building of the instances
| from the actual running of the application and sending responses.
|
*/

return $app;
2 changes: 2 additions & 0 deletions bootstrap/cache/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*
!.gitignore
67 changes: 67 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
{
"name": "rozsazoltan/illuminate-typescript",
"description": "Generates TypeScript interfaces from Laravel-based models",
"keywords": [
"typescript",
"php",
"laravel",
"typescript-interfaces",
"models",
"laravel-models",
"typescript-generator",
"php-to-typescript",
"interfaces",
"code-generation"
],
"homepage": "https://github.com/rozsazoltan/illuminate-typescript",
"license": "MIT",
"authors": [
{
"name": "Zoltán Rózsa",
"homepage": "https://github.com/rozsazoltan",
"role": "Developer"
}
],
"require": {
"php": "^8.2|^8.3",
"illuminate/contracts": "^11.0"
},
"require-dev": {
"orchestra/testbench": "^9.0",
"pestphp/pest": "^3.0"
},
"config": {
"sort-packages": true,
"allow-plugins": {
"pestphp/pest-plugin": true
}
},
"extra": {
"laravel": {
"providers": [
"Rose\\IlluminateTypeScript\\Foundation\\IlluminateTypeScriptServiceProvider"
]
}
},
"autoload": {
"psr-4": {
"Rose\\IlluminateTypeScript\\": "src/Illuminate/"
}
},
"autoload-dev": {
"psr-4": {
"Rose\\IlluminateTypeScript\\Tests\\": "tests/"
}
},
"minimum-stability": "dev",
"prefer-stable": true,
"scripts": {
"post-autoload-dump": [
"Illuminate\\Foundation\\ComposerScripts::postAutoloadDump",
"@php artisan package:discover --ansi"
],
"test": [
"./vendor/bin/pest"
]
}
}
31 changes: 31 additions & 0 deletions config/app.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

return [

/*
|--------------------------------------------------------------------------
| Autoloaded Service Providers
|--------------------------------------------------------------------------
|
| The service providers listed here will be automatically loaded on the
| request to your application. Feel free to add your own services to
| this array to grant expanded functionality to your applications.
|
*/

'providers' => [
/**
** Default Service Providers...
*/
\Illuminate\Cache\CacheServiceProvider::class,
\Illuminate\Database\DatabaseServiceProvider::class,
\Illuminate\Filesystem\FilesystemServiceProvider::class,
\Illuminate\Foundation\Providers\ConsoleSupportServiceProvider::class,
\Illuminate\Queue\QueueServiceProvider::class,

/**
** Package Service Providers...
*/
],

];
29 changes: 29 additions & 0 deletions config/typescript.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

use Rose\IlluminateTypeScript\Generators\ModelGenerator;
use Illuminate\Database\Eloquent\Model;

return [
'generators' => [
Model::class => [
'name' => ModelGenerator::class,
'settings' => [
'shouldDBType' => true,
'shouldStrictEnum' => true,
],
],
],

'paths' => [
//
],

'customRules' => [
// \App\Rules\MyCustomRule::class => 'string',
// \App\Rules\MyOtherCustomRule::class => ['string', 'number'],
],

'output' => resource_path('js/models.d.ts'),

'autoloadDev' => false,
];
15 changes: 15 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.5/phpunit.xsd" bootstrap="vendor/autoload.php" colors="true">
<testsuites>
<testsuite name="Test Suite">
<directory suffix="Test.php">./tests</directory>
</testsuite>
</testsuites>
<coverage/>
<source>
<include>
<directory suffix=".php">./app</directory>
<directory suffix=".php">./src</directory>
</include>
</source>
</phpunit>
27 changes: 27 additions & 0 deletions src/Illuminate/Commands/TypeScriptGenerateCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Rose\IlluminateTypeScript\Commands;

use Rose\IlluminateTypeScript\Foundation\TypeScriptGenerator;
use Illuminate\Console\Command;

class TypeScriptGenerateCommand extends Command
{
public $signature = 'typescript:generate';

public $description = 'Generates TypeScript interfaces from Laravel-based models';

public function handle()
{
$generator = new TypeScriptGenerator(
generators: config('typescript.generators', []),
paths: config('typescript.paths', []),
output: config('typescript.output', resource_path('js/models.d.ts')),
autoloadDev: config('typescript.autoloadDev', false),
);

$generator->execute();

$this->comment('TypeScript definitions generated successfully');
}
}
14 changes: 14 additions & 0 deletions src/Illuminate/Contracts/Generator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace Rose\IlluminateTypeScript\Contracts;

use ReflectionClass;

interface Generator
{
public function __construct(array $userSettings = []);

public function generate(ReflectionClass $reflection): ?string;

public function getDefinition(): ?string;
}
37 changes: 37 additions & 0 deletions src/Illuminate/Definitions/TypeScriptProperty.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace Rose\IlluminateTypeScript\Definitions;

use Illuminate\Support\Collection;

class TypeScriptProperty
{
public function __construct(
public string $name,
public string|array $types,
public bool $optional = false,
public bool $readonly = false,
public bool $nullable = false
)
{
//
}

public function getTypes(): string
{
return collect($this->types)
->when($this->nullable, fn(Collection $types) => $types->push(TypeScriptType::NULL))
->join(' | ', '');
}

public function __toString(): string
{
return collect([$this->name])
->when($this->readonly, fn(Collection $definition) => $definition->prepend('readonly '))
->when($this->optional, fn(Collection $definition) => $definition->push('?'))
->push(': ')
->push($this->getTypes())
->push(';')
->join('');
}
}
49 changes: 49 additions & 0 deletions src/Illuminate/Definitions/TypeScriptType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

namespace Rose\IlluminateTypeScript\Definitions;

use ReflectionMethod;
use ReflectionUnionType;

class TypeScriptType
{
public const STRING = 'string';
public const NUMBER = 'number';
public const BOOLEAN = 'boolean';
public const ANY = 'any';
public const NULL = 'null';

public static function array(string $type = self::ANY): string
{
return "Array<{$type}>";
}

public static function fromMethod(ReflectionMethod $method): array
{
$types = $method->getReturnType() instanceof ReflectionUnionType
? $method->getReturnType()->getTypes()
: (string) $method->getReturnType();

if (is_string($types) && strpos($types, '?') !== false) {
$types = [
str_replace('?', '', $types),
self::NULL
];
}

return collect($types)
->map(function (string $type) {
return match ($type) {
'int' => self::NUMBER,
'float' => self::NUMBER,
'string' => self::STRING,
'array' => self::array(),
'object' => self::ANY,
'null' => self::NULL,
'bool' => self::BOOLEAN,
default => self::ANY,
};
})
->toArray();
}
}
17 changes: 17 additions & 0 deletions src/Illuminate/Foundation/Console/Kernel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace Rose\IlluminateTypeScript\Foundation\Console;

use Illuminate\Foundation\Console\Kernel as ConsoleKernel;

class Kernel extends ConsoleKernel
{
/**
* A list of the commands provided by your application.
*
* @var array
*/
protected $commands = [
//
];
}
Loading

0 comments on commit 17a5eab

Please sign in to comment.