Skip to content

Commit a596017

Browse files
committed
Proper validation for models on app or models namespace
Fixes #97
1 parent de9f46a commit a596017

File tree

4 files changed

+63
-71
lines changed

4 files changed

+63
-71
lines changed

src/Console/Commands/BuildBackpackCommand.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace Backpack\Generators\Console\Commands;
44

5-
use Artisan;
65
use Illuminate\Console\Command;
76
use Illuminate\Support\Arr;
87
use Illuminate\Support\Str;
@@ -34,19 +33,18 @@ public function handle()
3433
$models = $this->getModels(base_path().'/app');
3534

3635
if (! count($models)) {
37-
$this->info('No models found.');
36+
$this->error('No models found.');
3837

3938
return false;
4039
}
4140

4241
foreach ($models as $key => $model) {
43-
$this->info('--- '.$model.' ---');
42+
$this->info("--- $model ---");
4443
// Create the CrudController & Request
4544
// Attach CrudTrait to Model
4645
// Add sidebar item
4746
// Add routes
48-
Artisan::call('backpack:crud', ['name' => $model]);
49-
echo Artisan::output();
47+
$this->call('backpack:crud', ['name' => $model]);
5048
}
5149
}
5250

src/Console/Commands/ChartBackpackCommand.php

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace Backpack\Generators\Console\Commands;
44

5-
use Artisan;
65
use Illuminate\Console\Command;
76
use Illuminate\Support\Str;
87

@@ -33,13 +32,11 @@ public function handle()
3332
$kebabName = Str::kebab($this->argument('name'));
3433

3534
// Create the ChartController and show output
36-
Artisan::call('backpack:chart-controller', ['name' => $studlyName]);
37-
echo Artisan::output();
35+
$this->call('backpack:chart-controller', ['name' => $studlyName]);
3836

3937
// Create the chart route
40-
Artisan::call('backpack:add-custom-route', [
41-
'code' => "Route::get('charts/".$kebabName."', 'Charts\\".$studlyName."ChartController@response')->name('charts.$kebabName.index');",
38+
$this->call('backpack:add-custom-route', [
39+
'code' => "Route::get('charts/{$kebabName}', 'Charts\\{$studlyName}ChartController@response')->name('charts.{$kebabName}.index');",
4240
]);
43-
echo Artisan::output();
4441
}
4542
}

src/Console/Commands/CrudBackpackCommand.php

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace Backpack\Generators\Console\Commands;
44

5-
use Artisan;
65
use Illuminate\Console\Command;
76
use Illuminate\Support\Str;
87

@@ -31,29 +30,25 @@ public function handle()
3130
{
3231
$name = ucfirst($this->argument('name'));
3332
$lowerName = strtolower($this->argument('name'));
33+
$pluralName = Str::plural($name);
3434

3535
// Create the CRUD Controller and show output
36-
Artisan::call('backpack:crud-controller', ['name' => $name]);
37-
echo Artisan::output();
36+
$this->call('backpack:crud-controller', ['name' => $name]);
3837

3938
// Create the CRUD Model and show output
40-
Artisan::call('backpack:crud-model', ['name' => $name]);
41-
echo Artisan::output();
39+
$this->call('backpack:crud-model', ['name' => $name]);
4240

4341
// Create the CRUD Request and show output
44-
Artisan::call('backpack:crud-request', ['name' => $name]);
45-
echo Artisan::output();
42+
$this->call('backpack:crud-request', ['name' => $name]);
4643

4744
// Create the CRUD route
48-
Artisan::call('backpack:add-custom-route', [
49-
'code' => "Route::crud('".$lowerName."', '".$name."CrudController');",
45+
$this->call('backpack:add-custom-route', [
46+
'code' => "Route::crud('$lowerName', '{$name}CrudController');",
5047
]);
51-
echo Artisan::output();
5248

5349
// Create the sidebar item
54-
Artisan::call('backpack:add-sidebar-content', [
55-
'code' => "<li class='nav-item'><a class='nav-link' href='{{ backpack_url('".$lowerName."') }}'><i class='nav-icon la la-question'></i> ".Str::plural($name).'</a></li>',
50+
$this->call('backpack:add-sidebar-content', [
51+
'code' => "<li class='nav-item'><a class='nav-link' href='{{ backpack_url('$lowerName') }}'><i class='nav-icon la la-question'></i> $pluralName</a></li>",
5652
]);
57-
echo Artisan::output();
5853
}
5954
}

src/Console/Commands/CrudModelBackpackCommand.php

Lines changed: 49 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -51,37 +51,61 @@ class CrudModelBackpackCommand extends GeneratorCommand
5151
*/
5252
public function handle()
5353
{
54-
$name = $this->qualifyClass($this->getNameInput());
54+
$name = $this->getNameInput();
55+
$namespaceApp = $this->qualifyClass($this->getNameInput());
56+
$namespaceModels = $this->qualifyClass('/Models/' . $this->getNameInput());
5557

58+
// Check if exists on app or models
59+
$existsOnApp = $this->alreadyExists($namespaceApp);
60+
$existsOnModels = $this->alreadyExists($namespaceModels);
61+
62+
// If no model was found, we will generate the path to the location where this class file
63+
// should be written. Then, we will build the class and make the proper replacements on
64+
// the stub files so that it gets the correctly formatted namespace and class name.
65+
if (!$existsOnApp && !$existsOnModels) {
66+
$this->makeDirectory($namespaceModels);
67+
68+
$this->files->put($this->getPath($namespaceModels), $this->sortImports($this->buildClass($namespaceModels)));
69+
70+
$this->info($this->type . ' created successfully.');
71+
return;
72+
}
73+
74+
// If it was found on both namespaces, we'll ask user to pick one of them
75+
if ($existsOnApp && $existsOnModels) {
76+
$result = $this->choice('Multiple models with this name were found, which one do you want to use?', [
77+
1 => "Use $namespaceApp",
78+
2 => "Use $namespaceModels",
79+
]);
80+
81+
// Disable the namespace not selected
82+
$existsOnApp = $result === 1;
83+
$existsOnModels = $result === 2;
84+
}
85+
86+
$name = $existsOnApp ? $namespaceApp : $namespaceModels;
5687
$path = $this->getPath($name);
5788

58-
// First we will check to see if the class already exists. If it does, we don't want
59-
// to create the class and overwrite the user's code. We just make sure it uses CrudTrait
60-
// We add that one line. Otherwise, we will continue generating this class' files.
61-
if ((! $this->hasOption('force') ||
62-
! $this->option('force')) &&
63-
$this->alreadyExists($this->getNameInput())) {
89+
// As the class already exists, we don't want to create the class and overwrite the
90+
// user's code. We just make sure it uses CrudTrait. We add that one line.
91+
if (!$this->hasOption('force') || !$this->option('force')) {
6492
$file = $this->files->get($path);
65-
$file_array = preg_split('/(\r\n)|\r|\n/', $file);
93+
$lines = preg_split('/(\r\n)|\r|\n/', $file);
6694

6795
// check if it already uses CrudTrait
6896
// if it does, do nothing
69-
if (Str::contains($file, [$this->crudTrait])) {
70-
$this->info('Model already exists and uses CrudTrait.');
71-
72-
return false;
97+
if (Str::contains($file, $this->crudTrait)) {
98+
$this->error('Model already uses CrudTrait.');
99+
return;
73100
}
74101

75102
// if it does not have CrudTrait, add the trait on the Model
76-
77-
$classDefinition = 'class '.$this->getNameInput().' extends';
78-
79-
foreach ($file_array as $key => $line) {
80-
if (Str::contains($line, $classDefinition)) {
103+
foreach ($lines as $key => $line) {
104+
if (Str::contains($line, "class {$this->getNameInput()} extends")) {
81105
if (Str::endsWith($line, '{')) {
82106
// add the trait on the next
83107
$position = $key + 1;
84-
} elseif ($file_array[$key + 1] == '{') {
108+
} elseif ($lines[$key + 1] == '{') {
85109
// add the trait on the next next line
86110
$position = $key + 2;
87111
}
@@ -91,31 +115,21 @@ public function handle()
91115
// IDEs start counting from 1
92116

93117
// add CrudTrait
94-
array_splice($file_array, $position, 0, ' use \\'.$this->crudTrait.';');
118+
array_splice($lines, $position, 0, " use \\{$this->crudTrait};");
95119

96120
// save the file
97-
$this->files->put($path, implode(PHP_EOL, $file_array));
121+
$this->files->put($path, implode(PHP_EOL, $lines));
98122

99123
// let the user know what we've done
100-
$this->info('Model already exists! We just added CrudTrait on it.');
101-
102-
return false;
124+
$this->error('Model already exists!');
125+
$this->info('We\'ve added CrudTrait on the Model.');
126+
return;
103127
}
104128
}
105129

106-
$this->error('Model already exists! Could not add CrudTrait - please add manually.');
107-
108-
return false;
130+
// In case we couldn't add the CrudTrait
131+
$this->error("Model already exists on '$name' and we couldn't add CrudTrait. Please add it manually.");
109132
}
110-
111-
// Next, we will generate the path to the location where this class' file should get
112-
// written. Then, we will build the class and make the proper replacements on the
113-
// stub files so that it gets the correctly formatted namespace and class name.
114-
$this->makeDirectory($path);
115-
116-
$this->files->put($path, $this->sortImports($this->buildClass($name)));
117-
118-
$this->info($this->type.' created successfully.');
119133
}
120134

121135
/**
@@ -128,18 +142,6 @@ protected function getStub()
128142
return __DIR__.'/../stubs/crud-model.stub';
129143
}
130144

131-
/**
132-
* Get the default namespace for the class.
133-
*
134-
* @param string $rootNamespace
135-
*
136-
* @return string
137-
*/
138-
protected function getDefaultNamespace($rootNamespace)
139-
{
140-
return $rootNamespace.'\Models';
141-
}
142-
143145
/**
144146
* Replace the table name for the given stub.
145147
*

0 commit comments

Comments
 (0)