diff --git a/src/Console/Commands/CrudBackpackCommand.php b/src/Console/Commands/CrudBackpackCommand.php index f3beee3..52a1fb1 100644 --- a/src/Console/Commands/CrudBackpackCommand.php +++ b/src/Console/Commands/CrudBackpackCommand.php @@ -12,7 +12,7 @@ class CrudBackpackCommand extends Command * * @var string */ - protected $signature = 'backpack:crud {name}'; + protected $signature = 'backpack:crud {name} {folder?} {--rf}'; /** * The console command description. @@ -32,23 +32,51 @@ public function handle() $lowerName = strtolower($this->argument('name')); $pluralName = Str::plural($name); - // Create the CRUD Controller and show output - $this->call('backpack:crud-controller', ['name' => $name]); + $argumentsArray = ['name' => $name]; + $crudControllerName = "{$name}CrudController"; + if (strlen($this->argument('folder')) > 1) { + $folderName = ucfirst($this->argument('folder')); + $folderLowerName = strtolower($this->argument('folder')); + $argumentsArray['folder'] = $folderName; + $crudControllerName = "$folderName\\".$name."CrudController"; + } + + if ($this->option('rf') == true) { + // Create the CRUD Controller and show output + $this->call('backpack:crud-controller', array_merge($argumentsArray, [ + '--rf' => true + ])); + } else { + // Create the CRUD Controller and show output + $this->call('backpack:crud-controller', $argumentsArray); + } // Create the CRUD Model and show output - $this->call('backpack:crud-model', ['name' => $name]); + $this->call('backpack:crud-model', $argumentsArray); // Create the CRUD Request and show output - $this->call('backpack:crud-request', ['name' => $name]); + $this->call('backpack:crud-request', $argumentsArray); // Create the CRUD route - $this->call('backpack:add-custom-route', [ - 'code' => "Route::crud('$lowerName', '{$name}CrudController');", - ]); - - // Create the sidebar item - $this->call('backpack:add-sidebar-content', [ - 'code' => "", - ]); + if (strlen($this->argument('folder')) > 1 && $this->option('rf') == true) { + $this->call('backpack:add-custom-route', [ + 'code' => "Route::crud('$folderLowerName/$lowerName', '$crudControllerName');", + ]); + + // Create the sidebar item + $this->call('backpack:add-sidebar-content', [ + 'code' => "", + ]); + } else { + $this->call('backpack:add-custom-route', [ + 'code' => "Route::crud('$lowerName', '$crudControllerName');", + ]); + + // Create the sidebar item + $this->call('backpack:add-sidebar-content', [ + 'code' => "", + ]); + } + } } diff --git a/src/Console/Commands/CrudControllerBackpackCommand.php b/src/Console/Commands/CrudControllerBackpackCommand.php index 3151462..351ddc4 100644 --- a/src/Console/Commands/CrudControllerBackpackCommand.php +++ b/src/Console/Commands/CrudControllerBackpackCommand.php @@ -20,7 +20,7 @@ class CrudControllerBackpackCommand extends GeneratorCommand * * @var string */ - protected $signature = 'backpack:crud-controller {name}'; + protected $signature = 'backpack:crud-controller {name} {folder?} {--rf}'; /** * The console command description. @@ -46,7 +46,6 @@ class CrudControllerBackpackCommand extends GeneratorCommand protected function getPath($name) { $name = str_replace($this->laravel->getNamespace(), '', $name); - return $this->laravel['path'].'/'.str_replace('\\', '/', $name).'CrudController.php'; } @@ -69,7 +68,13 @@ protected function getStub() */ protected function getDefaultNamespace($rootNamespace) { - return $rootNamespace.'\Http\Controllers\Admin'; + $currentNamespace = $rootNamespace.'\Http\Controllers\Admin'; + if (strlen($this->argument('folder')) > 1) { + $folderName = ucfirst($this->argument('folder')); + $currentNamespace = $rootNamespace.'\Http\Controllers\Admin\\'.$folderName; + + } + return $currentNamespace; } /** @@ -85,6 +90,12 @@ protected function replaceNameStrings(&$stub, $name) $table = Str::plural(ltrim(strtolower(preg_replace('/[A-Z]/', '_$0', str_replace($this->getNamespace($name).'\\', '', $name))), '_')); $stub = str_replace('DummyTable', $table, $stub); + if (strlen($this->argument('folder')) > 1) { + $folderLowerName = strtolower($this->argument('folder')); + if ($this->option('rf') == true) { + $stub = str_replace('/dummy_class', '/'.$folderLowerName.'/'.strtolower(str_replace($this->getNamespace($name).'\\', '', $name)), $stub); + } + } $stub = str_replace('dummy_class', strtolower(str_replace($this->getNamespace($name).'\\', '', $name)), $stub); return $this; @@ -118,6 +129,10 @@ protected function replaceSetFromDb(&$stub, $name) { $class = Str::afterLast($name, '\\'); $model = "App\\Models\\$class"; + if (strlen($this->argument('folder')) > 1) { + $folderName = ucfirst($this->argument('folder')); + $model = "App\\Models\\".$folderName."\\$class"; + } if (! class_exists($model)) { return $this; @@ -159,7 +174,22 @@ protected function replaceSetFromDb(&$stub, $name) protected function replaceModel(&$stub, $name) { $class = str_replace($this->getNamespace($name).'\\', '', $name); - $stub = str_replace(['DummyClass', '{{ class }}', '{{class}}'], $class, $stub); + if (strlen($this->argument('folder')) > 1) { + $folderName = ucfirst($this->argument('folder')); + + // replace crudcontroller + $stub = str_replace('DummyClassCrudController', $class.'CrudController', $stub); + + // replace CrudRequest + $stub = str_replace('\DummyClassRequest', "\\".$folderName.'\\'.$class.'Request', $stub); + $stub = str_replace('DummyClassRequest', $class.'Request', $stub); + + // replace CrudModel + $stub = str_replace('DummyClass::class', $folderName.'\\'.$class.'::class', $stub); + + } else { + $stub = str_replace(['DummyClass', '{{ class }}', '{{class}}'], $class, $stub); + } return $this; } @@ -176,9 +206,9 @@ protected function buildClass($name) $stub = $this->files->get($this->getStub()); $this->replaceNamespace($stub, $name) - ->replaceNameStrings($stub, $name) - ->replaceModel($stub, $name) - ->replaceSetFromDb($stub, $name); + ->replaceNameStrings($stub, $name) + ->replaceModel($stub, $name) + ->replaceSetFromDb($stub, $name); return $stub; } diff --git a/src/Console/Commands/CrudModelBackpackCommand.php b/src/Console/Commands/CrudModelBackpackCommand.php index cbb81a6..b1593d2 100644 --- a/src/Console/Commands/CrudModelBackpackCommand.php +++ b/src/Console/Commands/CrudModelBackpackCommand.php @@ -19,7 +19,7 @@ class CrudModelBackpackCommand extends GeneratorCommand * * @var string */ - protected $signature = 'backpack:crud-model {name}'; + protected $signature = 'backpack:crud-model {name} {folder?}'; /** * The console command description. @@ -54,7 +54,10 @@ public function handle() $name = $this->getNameInput(); $namespaceApp = $this->qualifyClass($this->getNameInput()); $namespaceModels = $this->qualifyClass('/Models/'.$this->getNameInput()); - + if (strlen($this->argument('folder')) > 1) { + $folderName = ucfirst($this->argument('folder')); + $namespaceModels = $this->qualifyClass('/Models/'.$folderName.'/'.$this->getNameInput()); + } // Check if exists on app or models $existsOnApp = $this->alreadyExists($namespaceApp); $existsOnModels = $this->alreadyExists($namespaceModels); @@ -63,7 +66,11 @@ public function handle() // should be written. Then, we will build the class and make the proper replacements on // the stub files so that it gets the correctly formatted namespace and class name. if (! $existsOnApp && ! $existsOnModels) { - $this->makeDirectory($namespaceModels); + if (strlen($this->argument('folder')) > 1) { + mkdir(str_replace('/'.$this->getNameInput().'.php', '', $this->getPath($namespaceModels))); + } else { + $this->makeDirectory($namespaceModels); + } $this->files->put($this->getPath($namespaceModels), $this->sortImports($this->buildClass($namespaceModels))); @@ -173,7 +180,6 @@ protected function replaceTable(&$stub, $name) protected function buildClass($name) { $stub = $this->files->get($this->getStub()); - return $this->replaceNamespace($stub, $name)->replaceTable($stub, $name)->replaceClass($stub, $name); } diff --git a/src/Console/Commands/CrudRequestBackpackCommand.php b/src/Console/Commands/CrudRequestBackpackCommand.php index 4f345e6..7016af7 100644 --- a/src/Console/Commands/CrudRequestBackpackCommand.php +++ b/src/Console/Commands/CrudRequestBackpackCommand.php @@ -18,7 +18,7 @@ class CrudRequestBackpackCommand extends GeneratorCommand * * @var string */ - protected $signature = 'backpack:crud-request {name}'; + protected $signature = 'backpack:crud-request {name} {folder?}'; /** * The console command description. @@ -44,7 +44,6 @@ class CrudRequestBackpackCommand extends GeneratorCommand protected function getPath($name) { $name = str_replace($this->laravel->getNamespace(), '', $name); - return $this->laravel['path'].'/'.str_replace('\\', '/', $name).'Request.php'; } @@ -67,7 +66,13 @@ protected function getStub() */ protected function getDefaultNamespace($rootNamespace) { - return $rootNamespace.'\Http\Requests'; + $currentNamespace = $rootNamespace.'\Http\Requests'; + if (strlen($this->argument('folder')) > 1) { + $folderName = ucfirst($this->argument('folder')); + $currentNamespace =$rootNamespace.'\Http\Requests\\'.$folderName; + + } + return $currentNamespace; } /**