Skip to content

Commit c52decf

Browse files
committed
Merge branch 'validate-models-namespace'
2 parents de9f46a + 1ef1f88 commit c52decf

File tree

6 files changed

+65
-71
lines changed

6 files changed

+65
-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/ConfigBackpackCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public function fire()
6767
$path = $this->getPath($name);
6868

6969
if ($this->alreadyExists($this->getNameInput())) {
70-
$this->error($this->type.' already exists!');
70+
$this->error($this->type.' already existed!');
7171

7272
return false;
7373
}

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 & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -51,37 +51,63 @@ 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+
72+
return;
73+
}
74+
75+
// If it was found on both namespaces, we'll ask user to pick one of them
76+
if ($existsOnApp && $existsOnModels) {
77+
$result = $this->choice('Multiple models with this name were found, which one do you want to use?', [
78+
1 => "Use $namespaceApp",
79+
2 => "Use $namespaceModels",
80+
]);
81+
82+
// Disable the namespace not selected
83+
$existsOnApp = $result === 1;
84+
$existsOnModels = $result === 2;
85+
}
86+
87+
$name = $existsOnApp ? $namespaceApp : $namespaceModels;
5688
$path = $this->getPath($name);
5789

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())) {
90+
// As the class already exists, we don't want to create the class and overwrite the
91+
// user's code. We just make sure it uses CrudTrait. We add that one line.
92+
if (! $this->hasOption('force') || ! $this->option('force')) {
6493
$file = $this->files->get($path);
65-
$file_array = preg_split('/(\r\n)|\r|\n/', $file);
94+
$lines = preg_split('/(\r\n)|\r|\n/', $file);
6695

6796
// check if it already uses CrudTrait
6897
// if it does, do nothing
69-
if (Str::contains($file, [$this->crudTrait])) {
70-
$this->info('Model already exists and uses CrudTrait.');
98+
if (Str::contains($file, $this->crudTrait)) {
99+
$this->comment('Model already used CrudTrait.');
71100

72-
return false;
101+
return;
73102
}
74103

75104
// 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)) {
105+
foreach ($lines as $key => $line) {
106+
if (Str::contains($line, "class {$this->getNameInput()} extends")) {
81107
if (Str::endsWith($line, '{')) {
82108
// add the trait on the next
83109
$position = $key + 1;
84-
} elseif ($file_array[$key + 1] == '{') {
110+
} elseif ($lines[$key + 1] == '{') {
85111
// add the trait on the next next line
86112
$position = $key + 2;
87113
}
@@ -91,31 +117,21 @@ public function handle()
91117
// IDEs start counting from 1
92118

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

96122
// save the file
97-
$this->files->put($path, implode(PHP_EOL, $file_array));
123+
$this->files->put($path, implode(PHP_EOL, $lines));
98124

99125
// let the user know what we've done
100-
$this->info('Model already exists! We just added CrudTrait on it.');
126+
$this->info('Model already existed. Added CrudTrait to it.');
101127

102-
return false;
128+
return;
103129
}
104130
}
105131

106-
$this->error('Model already exists! Could not add CrudTrait - please add manually.');
107-
108-
return false;
132+
// 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.");
109134
}
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.');
119135
}
120136

121137
/**
@@ -128,18 +144,6 @@ protected function getStub()
128144
return __DIR__.'/../stubs/crud-model.stub';
129145
}
130146

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-
143147
/**
144148
* Replace the table name for the given stub.
145149
*

src/Console/Commands/ViewBackpackCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public function fire()
7171
$path = $this->getPath($name);
7272

7373
if ($this->alreadyExists($this->getNameInput())) {
74-
$this->error($this->type.' already exists!');
74+
$this->error($this->type.' already existed!');
7575

7676
return false;
7777
}

0 commit comments

Comments
 (0)