From c7419f1105170450578f4328d3599c4d332e5346 Mon Sep 17 00:00:00 2001 From: kevin olson Date: Thu, 21 Oct 2021 15:47:07 -0500 Subject: [PATCH] =?UTF-8?q?=F0=9F=8E=A8=20readme=20update=20for=20global?= =?UTF-8?q?=20namespace?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- readme.md | 20 ++++++++++++++++++++ src/Commands/ModelTyper.php | 2 +- src/ModelInterface.php | 13 +++++++------ 3 files changed, 28 insertions(+), 7 deletions(-) diff --git a/readme.md b/readme.md index 870e105..d4d6dc8 100644 --- a/readme.md +++ b/readme.md @@ -113,3 +113,23 @@ You can also specify an interface is nullable: ], ]; ``` + + +### Declare global +Generate your interfaces in a global namespace named `model` +```bash +artisn model:typer --global +``` + +```ts +export {} +declare global { + export namespace models { + + export interface Provider { + // columns + id: number + user_id: number + avatar?: string +... +``` diff --git a/src/Commands/ModelTyper.php b/src/Commands/ModelTyper.php index 99b3ed8..bfdf662 100644 --- a/src/Commands/ModelTyper.php +++ b/src/Commands/ModelTyper.php @@ -40,7 +40,7 @@ public function __construct() */ public function handle() { - echo (new ModelInterface($this->option('global') !== null))->generate(); + echo (new ModelInterface($this->option('global')))->generate(); return 0; } } diff --git a/src/ModelInterface.php b/src/ModelInterface.php index f7022e8..18e128b 100644 --- a/src/ModelInterface.php +++ b/src/ModelInterface.php @@ -56,13 +56,14 @@ public function __construct( */ public function generate(): string { - $allCode = ''; + $models = $this->getModels(); + $allCode = $this->getImports($models); + if ($this->global) { - $allCode = "export {}\ndeclare global {\n export namespace models {\n\n"; + $allCode .= "export {}\ndeclare global {\n export namespace models {\n\n"; $this->space = ' '; } - $models = $this->getModels(); - $allCode .= $this->getImports($models); + foreach ($models as $model) { $interface = $this->getInterface(new $model()); $allCode .= $this->getCode($interface); @@ -84,14 +85,14 @@ private function getImports(Collection $models): string { foreach ($models as $model) { if ($interfaces = (new $model())->interfaces) { foreach ($interfaces as $interface) { - if (isset($interfaces['import'])) { + if (isset($interface['import'])) { $imports[ $interface[ 'import' ] ][] = $interface[ 'name' ]; } } } } foreach ($imports as $import=>$names) { - $code .= "{$this->space}import { " . join(', ', array_unique($names)) . " } from '$import'\n"; + $code .= "import { " . join(', ', array_unique($names)) . " } from '$import'\n"; } return $code; }