Skip to content
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
54 changes: 41 additions & 13 deletions src/Console/Commands/CrudBackpackCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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' => "<li class='nav-item'><a class='nav-link' href='{{ backpack_url('$lowerName') }}'><i class='nav-icon la la-question'></i> $pluralName</a></li>",
]);
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' => "<li class='nav-item'><a class='nav-link' href='{{ backpack_url('$folderLowerName/$lowerName') }}'><i class='nav-icon la la-question'></i> $pluralName</a></li>",
]);
} else {
$this->call('backpack:add-custom-route', [
'code' => "Route::crud('$lowerName', '$crudControllerName');",
]);

// Create the sidebar item
$this->call('backpack:add-sidebar-content', [
'code' => "<li class='nav-item'><a class='nav-link' href='{{ backpack_url('$lowerName') }}'><i class='nav-icon la la-question'></i> $pluralName</a></li>",
]);
}

}
}
44 changes: 37 additions & 7 deletions src/Console/Commands/CrudControllerBackpackCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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';
}

Expand All @@ -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;
}

/**
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand All @@ -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;
}
Expand Down
14 changes: 10 additions & 4 deletions src/Console/Commands/CrudModelBackpackCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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);
Expand All @@ -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)));

Expand Down Expand Up @@ -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);
}

Expand Down
11 changes: 8 additions & 3 deletions src/Console/Commands/CrudRequestBackpackCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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';
}

Expand All @@ -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;
}

/**
Expand Down