Skip to content

Commit

Permalink
Update LynxGenerateController.php
Browse files Browse the repository at this point in the history
  • Loading branch information
arabnewscms authored Aug 19, 2024
1 parent df75301 commit f082b0b
Showing 1 changed file with 46 additions and 8 deletions.
54 changes: 46 additions & 8 deletions src/Commands/LynxGenerateController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Lynx\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\Artisan;

class LynxGenerateController extends Command
{
Expand Down Expand Up @@ -36,7 +37,7 @@ public function handle()
$model = $this->option('model');
$module = $this->option('module');

$stubPath = __DIR__.'/stubs';
$stubPath = __DIR__ . '/stubs';
if ($controller) {
$this->generateFile('api_controller', $controller, $module, $stubPath);
}
Expand All @@ -61,30 +62,31 @@ protected function generateFile($type, $name, $module, $stubPath)
$namespace = $module ? 'Modules\\' . $module . '\App\Http\Controllers\Api' : 'App\Http\Controllers\Api';
$stub = file_get_contents(str_replace('\\', '/', $stubPath) . '/' . $type . '.stub');
$stub = str_replace('{{namespace}}', $namespace, $stub);
$stub = str_replace('{{class}}', $name.'Controller', $stub);
$stub = str_replace('{{class}}', $name . 'Controller', $stub);

// Add Model Namespace
if ($this->option('module')) {
$entity = '\\Modules\\' . $this->option('module') . '\\App\\Models\\' . ($this->option('model')??$name);
$entity = '\\Modules\\' . $this->option('module') . '\\App\\Models\\' . ($this->option('model') ?? $name);
} else {
$entity = 'App\\Models\\' . ($this->option('model')??$name);
$entity = 'App\\Models\\' . ($this->option('model') ?? $name);
}

$stub = str_replace('{{entity}}', $entity, $stub);

// Add Policy Namespace
if ($this->option('module')) {
$policy = '\\Modules\\' . $this->option('module') . '\\App\\Policies\\' . ($this->option('policy')??$name);
$policy = '\\Modules\\' . $this->option('module') . '\\App\\Policies\\' . ($this->option('policy') ?? $name);
} else {
$policy = 'App\\Policies\\' . ($this->option('policy')??$name);
$policy = 'App\\Policies\\' . ($this->option('policy') ?? $name);
}

$stub = str_replace('{{policy}}', $policy, $stub);

// resource
if ($this->option('module')) {
$resource = '\\Modules\\' . $this->option('module') . "\App\\resources\\" . ($this->option('resource')??$name);
$resource = '\\Modules\\' . $this->option('module') . "\App\\resources\\" . ($this->option('resource') ?? $name);
} else {
$resource = 'App\\resources\\' . ($this->option('resource')??$name);
$resource = 'App\\resources\\' . ($this->option('resource') ?? $name);
}
$stub = str_replace('{{resourcesJson}}', $resource . 'Resource', $stub);

Expand All @@ -94,6 +96,42 @@ protected function generateFile($type, $name, $module, $stubPath)
$this->directory($namespace);
file_put_contents(str_replace('\\', '/', $path), $stub);
}

// Create Model if not Exists
if (!class_exists($entity)) {
if ($this->option('module')) {
Artisan::call('module:make-model ' . $name . ' ' . $module);
} else {
Artisan::call('make:model ' . $name);
}
$this->info('lynx sys: i created model ' . $entity . ' ;(');
} else {
$this->warn('lynx sys: your model ' . $entity . ' already exists');
}

// Create Policy if not Exists

if ($this->option('module') && !class_exists('Modules\\' . $module . '\\App\\Policies\\' . $name . 'Policy')) {
Artisan::call('module:make-policy ' . $name . 'Policy ' . $module);
$this->info('lynx sys: i created ' . $name . 'Policy For You ;(');
} elseif (!class_exists('App\\Policies\\' . $name . 'Policy')) {
Artisan::call('make:policy ' . $name . 'Policy --model=' . $name);
$this->info('lynx sys: i created ' . $name . 'Policy For You ;(');
} else {
$this->warn('lynx sys: your ' . $name . 'Policy already exists');
}

// Create Resource if not Exists

if ($this->option('module') && !class_exists('Modules\\' . $module . "\App\\resources\\" . $name . 'Resource')) {
Artisan::call('module:make-resource ' . $name . 'Resource ' . $module);
$this->info('lynx sys: i created ' . $name . 'Resource For You ;(');
} elseif (!class_exists('App\\Http\\Resources\\' . $name . 'Resource') && empty($this->option('module'))) {
Artisan::call('make:policy ' . $name . 'Resource ');
$this->info('lynx sys: i created ' . $name . 'Resource For You ;(');
} else {
$this->warn('lynx sys: your ' . $name . 'Resource already exists');
}
}

public function directory($path)
Expand Down

0 comments on commit f082b0b

Please sign in to comment.