Skip to content

Commit 24cb2ce

Browse files
authored
Merge pull request #158 from Laravel-Backpack/allow-no-name
Get the "name" from "--from" when it's not given
2 parents 1a773bf + 19794c0 commit 24cb2ce

File tree

5 files changed

+79
-21
lines changed

5 files changed

+79
-21
lines changed

src/Console/Commands/ButtonBackpackCommand.php

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class ButtonBackpackCommand extends GeneratorCommand
2222
*
2323
* @var string
2424
*/
25-
protected $signature = 'backpack:button {name} {--from=}';
25+
protected $signature = 'backpack:button {name?} {--from=}';
2626

2727
/**
2828
* The console command description.
@@ -147,4 +147,26 @@ protected function buildClass($name)
147147

148148
return $stub;
149149
}
150+
151+
/**
152+
* Get the desired class name from the input.
153+
*
154+
* @return string
155+
*/
156+
protected function getNameInput()
157+
{
158+
$name = Str::of($this->argument('name'));
159+
$from = Str::of($this->option('from'));
160+
161+
if ($name->isEmpty() && $from->isEmpty()) {
162+
throw new \Exception('Not enough arguments (missing: "name" or "--from").');
163+
}
164+
165+
// Name may come from the --from option
166+
if ($name->isEmpty()) {
167+
$name = $from->afterLast('/')->afterLast('\\');
168+
}
169+
170+
return $name->trim()->snake('_')->value;
171+
}
150172
}

src/Console/Commands/ColumnBackpackCommand.php

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class ColumnBackpackCommand extends GeneratorCommand
2222
*
2323
* @var string
2424
*/
25-
protected $signature = 'backpack:column {name} {--from=}';
25+
protected $signature = 'backpack:column {name?} {--from=}';
2626

2727
/**
2828
* The console command description.
@@ -156,9 +156,18 @@ protected function buildClass($name)
156156
*/
157157
protected function getNameInput()
158158
{
159-
return Str::of($this->argument('name'))
160-
->trim()
161-
->snake('_')
162-
->value;
159+
$name = Str::of($this->argument('name'));
160+
$from = Str::of($this->option('from'));
161+
162+
if ($name->isEmpty() && $from->isEmpty()) {
163+
throw new \Exception('Not enough arguments (missing: "name" or "--from").');
164+
}
165+
166+
// Name may come from the --from option
167+
if ($name->isEmpty()) {
168+
$name = $from->afterLast('/')->afterLast('\\');
169+
}
170+
171+
return $name->trim()->snake('_')->value;
163172
}
164173
}

src/Console/Commands/FieldBackpackCommand.php

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class FieldBackpackCommand extends GeneratorCommand
2222
*
2323
* @var string
2424
*/
25-
protected $signature = 'backpack:field {name} {--from=}';
25+
protected $signature = 'backpack:field {name?} {--from=}';
2626

2727
/**
2828
* The console command description.
@@ -159,9 +159,18 @@ protected function buildClass($name)
159159
*/
160160
protected function getNameInput()
161161
{
162-
return Str::of($this->argument('name'))
163-
->trim()
164-
->snake('_')
165-
->value;
162+
$name = Str::of($this->argument('name'));
163+
$from = Str::of($this->option('from'));
164+
165+
if ($name->isEmpty() && $from->isEmpty()) {
166+
throw new \Exception('Not enough arguments (missing: "name" or "--from").');
167+
}
168+
169+
// Name may come from the --from option
170+
if ($name->isEmpty()) {
171+
$name = $from->afterLast('/')->afterLast('\\');
172+
}
173+
174+
return $name->trim()->snake('_')->value;
166175
}
167176
}

src/Console/Commands/FilterBackpackCommand.php

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class FilterBackpackCommand extends GeneratorCommand
2222
*
2323
* @var string
2424
*/
25-
protected $signature = 'backpack:filter {name} {--from=}';
25+
protected $signature = 'backpack:filter {name?} {--from=}';
2626

2727
/**
2828
* The console command description.
@@ -156,9 +156,18 @@ protected function buildClass($name)
156156
*/
157157
protected function getNameInput()
158158
{
159-
return Str::of($this->argument('name'))
160-
->trim()
161-
->snake('_')
162-
->value;
159+
$name = Str::of($this->argument('name'));
160+
$from = Str::of($this->option('from'));
161+
162+
if ($name->isEmpty() && $from->isEmpty()) {
163+
throw new \Exception('Not enough arguments (missing: "name" or "--from").');
164+
}
165+
166+
// Name may come from the --from option
167+
if ($name->isEmpty()) {
168+
$name = $from->afterLast('/')->afterLast('\\');
169+
}
170+
171+
return $name->trim()->snake('_')->value;
163172
}
164173
}

src/Console/Commands/WidgetBackpackCommand.php

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class WidgetBackpackCommand extends GeneratorCommand
2222
*
2323
* @var string
2424
*/
25-
protected $signature = 'backpack:widget {name} {--from=}';
25+
protected $signature = 'backpack:widget {name?} {--from=}';
2626

2727
/**
2828
* The console command description.
@@ -159,9 +159,18 @@ protected function buildClass($name)
159159
*/
160160
protected function getNameInput()
161161
{
162-
return Str::of($this->argument('name'))
163-
->trim()
164-
->snake('_')
165-
->value;
162+
$name = Str::of($this->argument('name'));
163+
$from = Str::of($this->option('from'));
164+
165+
if ($name->isEmpty() && $from->isEmpty()) {
166+
throw new \Exception('Not enough arguments (missing: "name" or "--from").');
167+
}
168+
169+
// Name may come from the --from option
170+
if ($name->isEmpty()) {
171+
$name = $from->afterLast('/')->afterLast('\\');
172+
}
173+
174+
return $name->trim()->snake('_')->value;
166175
}
167176
}

0 commit comments

Comments
 (0)