diff --git a/src/Commands/LaravelGiiCommand.php b/src/Commands/LaravelGiiCommand.php deleted file mode 100644 index 4d14227..0000000 --- a/src/Commands/LaravelGiiCommand.php +++ /dev/null @@ -1,19 +0,0 @@ -comment('All done'); - - return self::SUCCESS; - } -} diff --git a/src/Commands/OrionTypescriptCommand.php b/src/Commands/OrionTypescriptCommand.php new file mode 100644 index 0000000..d9478ba --- /dev/null +++ b/src/Commands/OrionTypescriptCommand.php @@ -0,0 +1,126 @@ +config = $config; + } + + /** + * Execute the console command. + */ + public function handle() + { + $controller = $this->argument('controller'); + $modelClass = $this->getModelClassFromController($controller); + + if (!class_exists($modelClass)) { + $this->error("Model for controller $controller does not exist."); + return; + } + + $modelInstance = new $modelClass; + $connection = $this->getConnection($modelInstance); + $schema = $this->getSchema($connection); + $table = $modelInstance->getTable(); + + $this->generateOrionModel($controller, $table); + $this->info("Orion SDK TypeScript model generated for $controller."); + } + + /** + * Get the model class name from the controller name. + * + * @param string $controller + * @return string + */ + protected function getModelClassFromController(string $controller): string + { + $modelName = Str::replaceLast('Controller', '', $controller); + return "App\\Models\\$modelName"; + } + + /** + * Get the database connection for the model. + * + * @param $modelInstance + * @return string + */ + protected function getConnection($modelInstance) + { + return $this->option('connection') ?: $modelInstance->getConnectionName() ?: $this->config->get('database.default'); + } + + /** + * Get the schema name for the given connection. + * + * @param string $connection + * @return string + */ + protected function getSchema(string $connection): string + { + return $this->config->get("database.connections.$connection.database"); + } + + /** + * Generate the Orion TypeScript model. + * + * @param string $controller + * @param string $table + */ + protected function generateOrionModel(string $controller, string $table) + { + $modelName = Str::replaceLast('Controller', '', $controller); + $content = "import {Model} from \"@tailflow/laravel-orion/lib/model\";\n\n"; + $content .= "export class $modelName extends Model<{\n"; + + $columns = Schema::getColumnListing($table); + foreach ($columns as $column) { + $type = 'string'; // You could enhance this to detect the proper TypeScript type based on the column type. + $content .= " $column: $type,\n"; + } + + $content .= "}>\n{ + +}"; + + $outputPath = base_path("resources/js/models/$modelName.ts"); + file_put_contents($outputPath, $content); + } +} diff --git a/src/LaravelGiiServiceProvider.php b/src/LaravelGiiServiceProvider.php index 37cd39d..558b460 100644 --- a/src/LaravelGiiServiceProvider.php +++ b/src/LaravelGiiServiceProvider.php @@ -2,7 +2,7 @@ namespace Inquid\LaravelGii; -use Inquid\LaravelGii\Commands\LaravelGiiCommand; +use Inquid\LaravelGii\Commands\OrionTypescriptCommand; use Spatie\LaravelPackageTools\Package; use Spatie\LaravelPackageTools\PackageServiceProvider; @@ -20,6 +20,6 @@ public function configurePackage(Package $package): void ->hasConfigFile() ->hasViews() ->hasMigration('create_laravel_gii_table') - ->hasCommand(LaravelGiiCommand::class); + ->hasCommand(OrionTypescriptCommand::class); } }