Skip to content

Commit fa2c85f

Browse files
committed
.
1 parent efd0c9e commit fa2c85f

9 files changed

Lines changed: 92 additions & 20 deletions

File tree

resources/js/models/index.d.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
declare namespace App.Interfaces {
2+
export type AnimalInterface = "App\\Models\\Cat" | "App\\Models\\Dog";
3+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
namespace SynergiTech\ExportTypes\Commands;
4+
5+
use Illuminate\Console\Command;
6+
7+
class ExportAllTypesCommand extends Command
8+
{
9+
protected $signature = 'synergi-types:all
10+
{--form-input=app/Http/Requests}
11+
{--form-output=resources/js/form-requests}
12+
{--model-input=app/Models}
13+
{--model-output=resources/js/models}
14+
{--format}
15+
{--prettier=}';
16+
17+
protected $description = 'Export all types.';
18+
19+
public function handle()
20+
{
21+
$this->call(GenerateFormRequestsCommand::class, [
22+
'--input' => $this->option('form-input'),
23+
'--output' => $this->option('form-output'),
24+
'--format' => $this->option('format'),
25+
'--prettier' => $this->option('prettier'),
26+
]);
27+
28+
$this->call(GenerateInterfaceUnionsCommand::class, [
29+
'--input' => $this->option('model-input'),
30+
'--output' => $this->option('model-output'),
31+
'--format' => $this->option('format'),
32+
'--prettier' => $this->option('prettier'),
33+
]);
34+
}
35+
}

src/Commands/GenerateFormRequestsCommand.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@
1010

1111
class GenerateFormRequestsCommand extends BaseCommand
1212
{
13-
protected $signature = 'export-form-requests:generate
13+
protected $signature = 'synergi-types:requests
1414
{--input=app/Http/Requests}
1515
{--output=resources/js/form-requests}
1616
{--format}
1717
{--prettier=}';
1818

19-
protected $description = 'Export models that implement an interface to your frontend.';
19+
protected $description = 'Export Form Requests as TypeScript types.';
2020

2121
public function __construct(
2222
protected Filesystem $files
@@ -184,7 +184,7 @@ protected function parseRules(FormRequest $formRequest) {
184184
protected function readFormRequests(string $path)
185185
{
186186
$classes = collect(iterator_to_array(new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path))))
187-
->reject(fn ($i) => $i->isDir() || str_ends_with($i->getRealPath(), '/..'))
187+
->reject(fn ($i) => !$i->isFile() || !str_ends_with($i->getRealPath(), '.php'))
188188
->map(fn ($item) => $this->fqcnFromPath($item->getRealPath()))
189189
->filter( fn($class) => is_subclass_of($class, FormRequest::class))
190190
->values();

src/Commands/GenerateInterfaceUnionsCommand.php

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@
99

1010
class GenerateInterfaceUnionsCommand extends BaseCommand
1111
{
12-
protected $signature = 'export-interface-unions:generate
12+
protected $signature = 'synergi-types:interface-unions
1313
{--input=app/Models}
1414
{--output=resources/js/models}
1515
{--format}
1616
{--prettier=}';
1717

18-
protected $description = 'Export models that implement an interface to your frontend.';
18+
protected $description = 'Export models that implement an interface as TypeScript union types.';
1919

2020
public function __construct(
2121
protected Filesystem $files
@@ -29,21 +29,19 @@ protected function process(): void
2929

3030
$interfaces = $this->readInterfaces(
3131
path: $this->base(),
32-
interfaces: config('export-types.interfaces', [])
32+
interfaces: config('synergi-types.interfaces', [])
3333
);
3434

35-
$tsContent = collect($interfaces)
36-
->map(function ($classes, $interface) {
37-
$shortInterface = Str::afterLast($interface, '\\');
38-
$classNames = collect($classes)
39-
->map(fn ($class) => '"'. addslashes($class) . '"')
40-
->implode(' | ');
41-
42-
return "export type {$shortInterface} = {$classNames};";
43-
})
44-
->implode("\n\n");
45-
46-
$tsContent .= "\n\nexport {};\n";
35+
// All interface unions should be exported under App.Interfaces
36+
$tsContent = "declare namespace App.Interfaces {\n";
37+
foreach ($interfaces as $interface => $classes) {
38+
$shortInterface = Str::afterLast($interface, '\\');
39+
$classNames = collect($classes)
40+
->map(fn ($class) => '"' . addslashes($class) . '"')
41+
->implode(' | ');
42+
$tsContent .= " export type {$shortInterface} = {$classNames};\n";
43+
}
44+
$tsContent .= "}\n";
4745

4846
$this->files->put($this->tsFilePath($path), $tsContent);
4947
}
@@ -56,11 +54,11 @@ protected function readInterfaces(string $path, array $interfaces)
5654
$paths = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path));
5755

5856
$rootNamespace = $this->determineRootNamespace($interfaces);
59-
57+
6058
return collect($interfaces)
6159
->mapWithKeys(fn ($interface) => [
6260
$this->chopStart($interface, $rootNamespace) => collect($paths)
63-
->reject(fn ($i) => $i->isDir() || str_ends_with($i->getRealPath(), '/..'))
61+
->reject(fn ($i) => !$i->isFile() || !str_ends_with($i->getRealPath(), '.php'))
6462
->map(fn ($item) => $this->fqcnFromPath($item->getRealPath()))
6563
->filter(fn ($i) => is_subclass_of($i, $interface))
6664
->values()

src/ExportTypesServiceProvider.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace SynergiTech\ExportTypes;
44

55
use Illuminate\Support\ServiceProvider;
6+
use SynergiTech\ExportTypes\Commands\ExportAllTypesCommand;
67
use SynergiTech\ExportTypes\Commands\GenerateFormRequestsCommand;
78
use SynergiTech\ExportTypes\Commands\GenerateInterfaceUnionsCommand;
89

@@ -13,6 +14,7 @@ public function boot(): void
1314
if ($this->app->runningInConsole()) {
1415
$this->commands(
1516
[
17+
ExportAllTypesCommand::class,
1618
GenerateInterfaceUnionsCommand::class,
1719
GenerateFormRequestsCommand::class,
1820
]
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
namespace App\Interfaces;
4+
5+
interface AnimalInterface
6+
{
7+
}

workbench/app/Models/Cat.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
namespace App\Models;
4+
5+
use App\Interfaces\AnimalInterface;
6+
7+
class Cat implements AnimalInterface
8+
{
9+
}

workbench/app/Models/Dog.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
namespace App\Models;
4+
5+
use App\Interfaces\AnimalInterface;
6+
7+
class Dog implements AnimalInterface
8+
{
9+
}

workbench/config/synergi-types.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
use App\Interfaces\AnimalInterface;
4+
5+
return [
6+
'interfaces' => [
7+
AnimalInterface::class,
8+
],
9+
];

0 commit comments

Comments
 (0)