Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add --force argument to php artisan backpack:crud #187

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/Console/Commands/CrudBackpackCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class CrudBackpackCommand extends BackpackCommand
* @var string
*/
protected $signature = 'backpack:crud {name}
{--validation= : Validation type, must be request, array or field}';
{--validation= : Validation type, must be request, array or field} {--force}';

/**
* The console command description.
Expand Down Expand Up @@ -52,14 +52,14 @@ public function handle()
}

// Create the CRUD Model and show output
$this->call('backpack:crud-model', ['name' => $name]);
$this->call('backpack:crud-model', ['name' => $nameTitle, '--force' => $this->option('force')]);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can probably store the --force option up close where we init other command properties like $name, $nameTitle etc. We then re-use the variable across the command 👍 This is just personal preference, it's not that is bad implemented.

Suggested change
$this->call('backpack:crud-model', ['name' => $nameTitle, '--force' => $this->option('force')]);
$this->call('backpack:crud-model', ['name' => $nameTitle, '--force' => $force]);

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please also take a look at this @karandatwani92 let me know if you think we shouldn't do this, otherwise please do it 👍


// Create the CRUD Controller and show output
$this->call('backpack:crud-controller', ['name' => $name, '--validation' => $validation]);
$this->call('backpack:crud-controller', ['name' => $nameTitle, '--validation' => $validation, '--force' => $this->option('force')]);

// Create the CRUD Request and show output
if ($validation === 'request') {
$this->call('backpack:crud-request', ['name' => $name]);
$this->call('backpack:crud-request', ['name' => $nameTitle, '--force' => $this->option('force')]);
}

// Create the CRUD route
Expand Down
2 changes: 1 addition & 1 deletion src/Console/Commands/CrudControllerBackpackCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class CrudControllerBackpackCommand extends BackpackCommand
* @var string
*/
protected $signature = 'backpack:crud-controller {name}
{--validation=request : Validation type, must be request, array or field}';
{--validation=request : Validation type, must be request, array or field} {--force}';

/**
* The console command description.
Expand Down
4 changes: 2 additions & 2 deletions src/Console/Commands/CrudModelBackpackCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class CrudModelBackpackCommand extends BackpackCommand
*
* @var string
*/
protected $signature = 'backpack:crud-model {name}';
protected $signature = 'backpack:crud-model {name} {--force}';

/**
* The console command description.
Expand Down Expand Up @@ -69,7 +69,7 @@ public function handle()
// If no model was found, we will generate the path to the location where this class file
// 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) {
if ($this->option('force') || (! $existsOnApp && ! $existsOnModels)) {
$this->makeDirectory($this->getPath($namespaceModels));

$this->files->put($this->getPath($namespaceModels), $this->sortImports($this->buildClass($nameTitle)));
Expand Down
2 changes: 1 addition & 1 deletion src/Console/Commands/CrudRequestBackpackCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class CrudRequestBackpackCommand extends BackpackCommand
*
* @var string
*/
protected $signature = 'backpack:crud-request {name}';
protected $signature = 'backpack:crud-request {name} {--force}';

/**
* The console command description.
Expand Down
68 changes: 7 additions & 61 deletions src/Console/Commands/ModelBackpackCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@

namespace Backpack\Generators\Console\Commands;

use Illuminate\Console\GeneratorCommand;
use Illuminate\Support\Str;
use Illuminate\Console\Command;

class ModelBackpackCommand extends GeneratorCommand
class ModelBackpackCommand extends Command
{
/**
* The console command name.
Expand All @@ -19,7 +18,7 @@ class ModelBackpackCommand extends GeneratorCommand
*
* @var string
*/
protected $signature = 'backpack:model {name} {--softdelete}';
protected $signature = 'backpack:model {name} {--force}';

/**
* The console command description.
Expand All @@ -29,65 +28,12 @@ class ModelBackpackCommand extends GeneratorCommand
protected $description = 'Generate a backpack templated model';

/**
* The type of class being generated.
* Execute the console command.
*
* @var string
*/
protected $type = 'Model';

/**
* Get the stub file for the generator.
*
* @return string
*/
protected function getStub()
{
if ($this->option('softdelete')) {
return __DIR__.'/../stubs/model-softdelete.stub';
}

return __DIR__.'/../stubs/model.stub';
}

/**
* Get the default namespace for the class.
*
* @param string $rootNamespace
* @return string
*/
protected function getDefaultNamespace($rootNamespace)
{
return $rootNamespace.'\Models';
}

/**
* Replace the table name for the given stub.
*
* @param string $stub
* @param string $name
* @return string
*/
protected function replaceTable(&$stub, $name)
{
$name = ltrim(strtolower(preg_replace('/[A-Z]/', '_$0', str_replace($this->getNamespace($name).'\\', '', $name))), '_');

$table = Str::snake(Str::plural($name));

$stub = str_replace('DummyTable', $table, $stub);

return $this;
}

/**
* Build the class with the given name.
*
* @param string $name
* @return string
* @return bool|null
*/
protected function buildClass($name)
public function handle()
{
$stub = $this->files->get($this->getStub());

return $this->replaceNamespace($stub, $name)->replaceTable($stub, $name)->replaceClass($stub, $name);
$this->call('backpack:crud-model', ['name' => $this->argument('name'), '--force' => $this->option('force')]);
}
}
32 changes: 7 additions & 25 deletions src/Console/Commands/RequestBackpackCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

namespace Backpack\Generators\Console\Commands;

use Illuminate\Console\GeneratorCommand;
use Illuminate\Console\Command;

class RequestBackpackCommand extends GeneratorCommand
class RequestBackpackCommand extends Command
{
/**
* The console command name.
Expand All @@ -18,7 +18,7 @@ class RequestBackpackCommand extends GeneratorCommand
*
* @var string
*/
protected $signature = 'backpack:request {name}';
protected $signature = 'backpack:request {name} {--force}';

/**
* The console command description.
Expand All @@ -28,30 +28,12 @@ class RequestBackpackCommand extends GeneratorCommand
protected $description = 'Generate a backpack templated request';

/**
* The type of class being generated.
* Execute the console command.
*
* @var string
*/
protected $type = 'Request';

/**
* Get the stub file for the generator.
*
* @return string
*/
protected function getStub()
{
return __DIR__.'/../stubs/request.stub';
}

/**
* Get the default namespace for the class.
*
* @param string $rootNamespace
* @return string
* @return bool|null
*/
protected function getDefaultNamespace($rootNamespace)
public function handle()
{
return $rootNamespace.'\Http\Requests';
$this->call('backpack:crud-request', ['name' => $this->argument('name'), '--force' => $this->option('force')]);
}
}