Skip to content

Commit 4599134

Browse files
authored
Merge pull request #136 from Laravel-Backpack/prettier-backpack-crud-command
Improved UI for backpack:crud command
2 parents a472003 + 3e97955 commit 4599134

File tree

6 files changed

+124
-17
lines changed

6 files changed

+124
-17
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
}
2626
],
2727
"require": {
28-
"backpack/crud": "^4.1|^5.0"
28+
"backpack/crud": "^5.3.3"
2929
},
3030
"require-dev": {
3131
"phpunit/phpunit" : "^9.0||^7.0",

src/Console/Commands/BuildBackpackCommand.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
class BuildBackpackCommand extends Command
1010
{
11+
use \Backpack\CRUD\app\Console\Commands\Traits\PrettyCommandOutput;
12+
1113
/**
1214
* The name and signature of the console command.
1315
*
@@ -30,22 +32,20 @@ class BuildBackpackCommand extends Command
3032
public function handle()
3133
{
3234
// make a list of all models
33-
$models = $this->getModels(base_path().'/app');
35+
$models = $this->getModels(base_path('app'));
3436

3537
if (! count($models)) {
36-
$this->error('No models found.');
38+
$this->errorBlock('No models found.');
3739

38-
return false;
40+
return;
3941
}
4042

4143
foreach ($models as $key => $model) {
42-
$this->info("--- $model ---");
43-
// Create the CrudController & Request
44-
// Attach CrudTrait to Model
45-
// Add sidebar item
46-
// Add routes
4744
$this->call('backpack:crud', ['name' => $model]);
45+
$this->line(' <fg=gray>----------</>');
4846
}
47+
48+
$this->deleteLines();
4949
}
5050

5151
private function getModels($path)

src/Console/Commands/CrudBackpackCommand.php

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@
22

33
namespace Backpack\Generators\Console\Commands;
44

5-
use Illuminate\Console\Command;
5+
use Illuminate\Console\GeneratorCommand;
66
use Illuminate\Support\Str;
77

8-
class CrudBackpackCommand extends Command
8+
class CrudBackpackCommand extends GeneratorCommand
99
{
10+
use \Backpack\CRUD\app\Console\Commands\Traits\PrettyCommandOutput;
11+
1012
/**
1113
* The name and signature of the console command.
1214
*
@@ -28,11 +30,20 @@ class CrudBackpackCommand extends Command
2830
*/
2931
public function handle()
3032
{
31-
$name = (string) $this->argument('name');
33+
$name = $this->getNameInput();
3234
$nameTitle = ucfirst(Str::camel($name));
3335
$nameKebab = Str::kebab($nameTitle);
3436
$namePlural = ucfirst(str_replace('-', ' ', Str::plural($nameKebab)));
3537

38+
// Validate if the name is reserved
39+
if ($this->isReservedName($nameTitle)) {
40+
$this->errorBlock("The name '$nameTitle' is reserved by PHP.");
41+
42+
return false;
43+
}
44+
45+
$this->infoBlock("Creating CRUD for the <fg=blue>$nameTitle</> model:");
46+
3647
// Create the CRUD Model and show output
3748
$this->call('backpack:crud-model', ['name' => $nameTitle]);
3849

@@ -57,5 +68,21 @@ public function handle()
5768
if (app()->routesAreCached()) {
5869
$this->call('route:cache');
5970
}
71+
72+
$url = Str::of(config('app.url'))->finish('/')->append('admin/')->append($nameKebab);
73+
74+
$this->newLine();
75+
$this->line(" Done! Go to <fg=blue>$url</> to see the CRUD in action.");
76+
$this->newLine();
77+
}
78+
79+
/**
80+
* Get the stub file for the generator.
81+
*
82+
* @return string
83+
*/
84+
protected function getStub()
85+
{
86+
return false;
6087
}
6188
}

src/Console/Commands/CrudControllerBackpackCommand.php

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
class CrudControllerBackpackCommand extends GeneratorCommand
1010
{
11+
use \Backpack\CRUD\app\Console\Commands\Traits\PrettyCommandOutput;
12+
1113
/**
1214
* The console command name.
1315
*
@@ -36,6 +38,39 @@ class CrudControllerBackpackCommand extends GeneratorCommand
3638
*/
3739
protected $type = 'Controller';
3840

41+
/**
42+
* Execute the console command.
43+
*
44+
* @return bool|null
45+
*
46+
* @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
47+
*/
48+
public function handle()
49+
{
50+
$name = $this->qualifyClass($this->getNameInput());
51+
$path = $this->getPath($name);
52+
53+
$this->progressBlock("Creating ${name}CrudController");
54+
55+
// Next, We will check to see if the class already exists. If it does, we don't want
56+
// to create the class and overwrite the user's code. So, we will bail out so the
57+
// code is untouched. Otherwise, we will continue generating this class' files.
58+
if ((! $this->hasOption('force') || ! $this->option('force')) && $this->alreadyExists($this->getNameInput())) {
59+
$this->closeProgressBlock('Already existed', 'yellow');
60+
61+
return false;
62+
}
63+
64+
// Next, we will generate the path to the location where this class' file should get
65+
// written. Then, we will build the class and make the proper replacements on the
66+
// stub files so that it gets the correctly formatted namespace and class name.
67+
$this->makeDirectory($path);
68+
69+
$this->files->put($path, $this->sortImports($this->buildClass($name)));
70+
71+
$this->closeProgressBlock();
72+
}
73+
3974
/**
4075
* Get the destination class path.
4176
*
@@ -95,7 +130,7 @@ protected function replaceNameStrings(&$stub, $name)
95130
protected function getAttributes($model)
96131
{
97132
$attributes = [];
98-
$model = new $model;
133+
$model = new $model();
99134

100135
// if fillable was defined, use that as the attributes
101136
if (count($model->getFillable())) {

src/Console/Commands/CrudModelBackpackCommand.php

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
class CrudModelBackpackCommand extends GeneratorCommand
99
{
10+
use \Backpack\CRUD\app\Console\Commands\Traits\PrettyCommandOutput;
11+
1012
/**
1113
* The console command name.
1214
*
@@ -55,6 +57,8 @@ public function handle()
5557
$namespaceApp = $this->qualifyClass($this->getNameInput());
5658
$namespaceModels = $this->qualifyClass('/Models/'.$this->getNameInput());
5759

60+
$this->progressBlock("Creating $namespaceModels");
61+
5862
// Check if exists on app or models
5963
$existsOnApp = $this->alreadyExists($namespaceApp);
6064
$existsOnModels = $this->alreadyExists($namespaceModels);
@@ -67,11 +71,14 @@ public function handle()
6771

6872
$this->files->put($this->getPath($namespaceModels), $this->sortImports($this->buildClass($namespaceModels)));
6973

70-
$this->info($this->type.' created successfully.');
74+
$this->closeProgressBlock();
7175

7276
return;
7377
}
7478

79+
// Model exists
80+
$this->closeProgressBlock('Already existed', 'yellow');
81+
7582
// If it was found on both namespaces, we'll ask user to pick one of them
7683
if ($existsOnApp && $existsOnModels) {
7784
$result = $this->choice('Multiple models with this name were found, which one do you want to use?', [
@@ -90,13 +97,15 @@ public function handle()
9097
// As the class already exists, we don't want to create the class and overwrite the
9198
// user's code. We just make sure it uses CrudTrait. We add that one line.
9299
if (! $this->hasOption('force') || ! $this->option('force')) {
100+
$this->progressBlock('Adding CrudTrait to model');
101+
93102
$file = $this->files->get($path);
94103
$lines = preg_split('/(\r\n)|\r|\n/', $file);
95104

96105
// check if it already uses CrudTrait
97106
// if it does, do nothing
98107
if (Str::contains($file, $this->crudTrait)) {
99-
$this->comment('Model already used CrudTrait.');
108+
$this->closeProgressBlock('Already existed', 'yellow');
100109

101110
return;
102111
}
@@ -123,14 +132,15 @@ public function handle()
123132
$this->files->put($path, implode(PHP_EOL, $lines));
124133

125134
// let the user know what we've done
126-
$this->info('Model already existed. Added CrudTrait to it.');
135+
$this->closeProgressBlock();
127136

128137
return;
129138
}
130139
}
131140

132141
// In case we couldn't add the CrudTrait
133-
$this->error("Model already existed on '$name' and we couldn't add CrudTrait. Please add it manually.");
142+
$this->errorProgressBlock();
143+
$this->note("Model already existed on '$name' and we couldn't add CrudTrait. Please add it manually.", 'red');
134144
}
135145
}
136146

src/Console/Commands/CrudRequestBackpackCommand.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
class CrudRequestBackpackCommand extends GeneratorCommand
88
{
9+
use \Backpack\CRUD\app\Console\Commands\Traits\PrettyCommandOutput;
10+
911
/**
1012
* The console command name.
1113
*
@@ -34,6 +36,39 @@ class CrudRequestBackpackCommand extends GeneratorCommand
3436
*/
3537
protected $type = 'Request';
3638

39+
/**
40+
* Execute the console command.
41+
*
42+
* @return bool|null
43+
*
44+
* @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
45+
*/
46+
public function handle()
47+
{
48+
$name = $this->qualifyClass($this->getNameInput());
49+
$path = $this->getPath($name);
50+
51+
$this->progressBlock("Creating ${name}Request");
52+
53+
// Next, We will check to see if the class already exists. If it does, we don't want
54+
// to create the class and overwrite the user's code. So, we will bail out so the
55+
// code is untouched. Otherwise, we will continue generating this class' files.
56+
if ((! $this->hasOption('force') || ! $this->option('force')) && $this->alreadyExists($this->getNameInput())) {
57+
$this->closeProgressBlock('Already existed', 'yellow');
58+
59+
return false;
60+
}
61+
62+
// Next, we will generate the path to the location where this class' file should get
63+
// written. Then, we will build the class and make the proper replacements on the
64+
// stub files so that it gets the correctly formatted namespace and class name.
65+
$this->makeDirectory($path);
66+
67+
$this->files->put($path, $this->sortImports($this->buildClass($name)));
68+
69+
$this->closeProgressBlock();
70+
}
71+
3772
/**
3873
* Get the destination class path.
3974
*

0 commit comments

Comments
 (0)