From 0cd4845d0cb5eae8f5f35306903bd327f72cc1b7 Mon Sep 17 00:00:00 2001 From: Alex Clark Date: Thu, 23 Feb 2023 11:16:42 +1300 Subject: [PATCH] Remove namespaces and change interfaces to types --- src/Generators/AbstractGenerator.php | 9 ++++----- src/Generators/ModelGenerator.php | 10 +++++----- src/TypeScriptGenerator.php | 14 +++++++++++--- 3 files changed, 20 insertions(+), 13 deletions(-) diff --git a/src/Generators/AbstractGenerator.php b/src/Generators/AbstractGenerator.php index 8d84649..6598244 100644 --- a/src/Generators/AbstractGenerator.php +++ b/src/Generators/AbstractGenerator.php @@ -15,14 +15,13 @@ public function generate(ReflectionClass $reflection): ?string $this->boot(); if (empty(trim($definition = $this->getDefinition()))) { - return " export interface {$this->tsClassName()} {}" . PHP_EOL; + return " {$this->tsClassName()}: {};" . PHP_EOL; } return <<tsClassName()} { - $definition - } - + {$this->tsClassName()}: { + $definition + }; TS; } diff --git a/src/Generators/ModelGenerator.php b/src/Generators/ModelGenerator.php index f34f7a9..56bc588 100644 --- a/src/Generators/ModelGenerator.php +++ b/src/Generators/ModelGenerator.php @@ -45,7 +45,7 @@ public function getDefinition(): ?string $this->getAccessors(), ]) ->filter(fn (string $part) => !empty($part)) - ->join(PHP_EOL . ' '); + ->join(PHP_EOL . ' '); } /** @@ -72,7 +72,7 @@ protected function getProperties(): string nullable: !$column->getNotnull() ); }) - ->join(PHP_EOL . ' '); + ->join(PHP_EOL . ' '); } protected function getAccessors(): string @@ -98,7 +98,7 @@ protected function getAccessors(): string readonly: true ); }) - ->join(PHP_EOL . ' '); + ->join(PHP_EOL . ' '); } protected function getRelations(): string @@ -112,7 +112,7 @@ protected function getRelations(): string nullable: true ); }) - ->join(PHP_EOL . ' '); + ->join(PHP_EOL . ' '); } protected function getManyRelations(): string @@ -127,7 +127,7 @@ protected function getManyRelations(): string nullable: true ); }) - ->join(PHP_EOL . ' '); + ->join(PHP_EOL . ' '); } protected function getRelationMethods(): Collection diff --git a/src/TypeScriptGenerator.php b/src/TypeScriptGenerator.php index a3f327c..b433f17 100644 --- a/src/TypeScriptGenerator.php +++ b/src/TypeScriptGenerator.php @@ -42,17 +42,25 @@ public function execute() protected function makeNamespace(string $namespace, Collection $reflections): string { - return $reflections->map(fn (ReflectionClass $reflection) => $this->makeInterface($reflection)) + return $reflections->map(fn (ReflectionClass $reflection) => $this->makeType($reflection)) ->whereNotNull() ->whenNotEmpty(function (Collection $definitions) use ($namespace) { $tsNamespace = str_replace('\\', '.', $namespace); + $parts = explode('.', $tsNamespace); + $rootType = array_shift($parts); - return $definitions->prepend("declare namespace {$tsNamespace} {")->push('}' . PHP_EOL); + foreach ($parts as $type) { + $definitions->prepend(" {$type}: {")->push('};' . PHP_EOL); + } + + $definitions->prepend("export type {$rootType} = {")->push('};' . PHP_EOL); + + return $definitions; }) ->join(PHP_EOL); } - protected function makeInterface(ReflectionClass $reflection): ?string + protected function makeType(ReflectionClass $reflection): ?string { $generator = collect($this->generators) ->filter(fn (string $generator, string $baseClass) => $reflection->isSubclassOf($baseClass))