Skip to content

Commit

Permalink
🚀 Adding first Orion generator
Browse files Browse the repository at this point in the history
  • Loading branch information
contactinquid committed Oct 13, 2024
1 parent ba50fa9 commit 94d6cd1
Show file tree
Hide file tree
Showing 3 changed files with 128 additions and 21 deletions.
19 changes: 0 additions & 19 deletions src/Commands/LaravelGiiCommand.php

This file was deleted.

126 changes: 126 additions & 0 deletions src/Commands/OrionTypescriptCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
<?php

namespace Inquid\LaravelGii\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\Schema;
use Illuminate\Contracts\Config\Repository;
use Illuminate\Support\Str;

class OrionTypescriptCommand extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'code:orion-models
{controller : The name of the controller}
{--c|connection= : The name of the connection}';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Generate Orion SDK TypeScript models based on the Laravel model';

/**
* @var Repository
*/
protected $config;

/**
* Create a new command instance.
*
* @param Repository $config
*/
public function __construct(Repository $config)
{
parent::__construct();

$this->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);
}
}
4 changes: 2 additions & 2 deletions src/LaravelGiiServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -20,6 +20,6 @@ public function configurePackage(Package $package): void
->hasConfigFile()
->hasViews()
->hasMigration('create_laravel_gii_table')
->hasCommand(LaravelGiiCommand::class);
->hasCommand(OrionTypescriptCommand::class);
}
}

0 comments on commit 94d6cd1

Please sign in to comment.