|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\Console\Commands; |
| 4 | + |
| 5 | +use App\Services\ModulesService; |
| 6 | +use Illuminate\Console\Command; |
| 7 | +use Illuminate\Support\Facades\Storage; |
| 8 | +use Illuminate\Support\Str; |
| 9 | + |
| 10 | +class ModuleCommandGenerator extends Command |
| 11 | +{ |
| 12 | + /** |
| 13 | + * The name and signature of the console command. |
| 14 | + * |
| 15 | + * @var string |
| 16 | + */ |
| 17 | + protected $signature = 'modules:command {namespace} {argument} {--force}'; |
| 18 | + |
| 19 | + /** |
| 20 | + * The console command description. |
| 21 | + * |
| 22 | + * @var string |
| 23 | + */ |
| 24 | + protected $description = 'Generate a command for a module.'; |
| 25 | + |
| 26 | + /** |
| 27 | + * Execute the console command. |
| 28 | + * |
| 29 | + * @return int |
| 30 | + */ |
| 31 | + public function handle() |
| 32 | + { |
| 33 | + $modules = app()->make( ModulesService::class ); |
| 34 | + |
| 35 | + /** |
| 36 | + * Check if module is defined |
| 37 | + */ |
| 38 | + if ( $module = $modules->get( $this->argument( 'namespace' ) ) ) { |
| 39 | + /** |
| 40 | + * Define the file name |
| 41 | + */ |
| 42 | + $commandsPath = $module[ 'namespace' ] . DIRECTORY_SEPARATOR . 'Console' . DIRECTORY_SEPARATOR . 'Commands' . DIRECTORY_SEPARATOR; |
| 43 | + $name = ucwords( Str::camel( $this->argument( 'argument' ) ) ); |
| 44 | + $fileName = $commandsPath . $name; |
| 45 | + $namespace = $this->argument( 'namespace' ); |
| 46 | + $fileExists = Storage::disk( 'ns-modules' )->exists( |
| 47 | + $fileName . '.php' |
| 48 | + ); |
| 49 | + |
| 50 | + if ( ! $fileExists || ( $fileExists && $this->option( 'force' ) ) ) { |
| 51 | + Storage::disk( 'ns-modules' )->put( |
| 52 | + $fileName . '.php', view( 'generate.modules.command', compact( |
| 53 | + 'modules', 'module', 'name', 'namespace' |
| 54 | + ) ) ); |
| 55 | + |
| 56 | + return $this->info( sprintf( |
| 57 | + __( 'The command has been created for the module "%s"!' ), |
| 58 | + $module[ 'name' ] |
| 59 | + ) ); |
| 60 | + } |
| 61 | + |
| 62 | + return $this->error( sprintf( 'A similar file is already found at the location: %s.', $fileName . '.php' ) ); |
| 63 | + } |
| 64 | + |
| 65 | + return $this->error( 'Unable to located the module !' ); |
| 66 | + } |
| 67 | +} |
0 commit comments