generated from spatie/package-skeleton-laravel
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from savannabits/0.x-dev
New Feature: Controller Make Command
- Loading branch information
Showing
4 changed files
with
150 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
<?php | ||
|
||
namespace Savannabits\Modular\Commands; | ||
|
||
use Savannabits\Modular\Support\Concerns\GeneratesModularFiles; | ||
use function Laravel\Prompts\confirm; | ||
|
||
class ControllerMakeCommand extends \Illuminate\Routing\Console\ControllerMakeCommand | ||
{ | ||
use GeneratesModularFiles; | ||
|
||
protected $name = 'modular:make-controller'; | ||
protected $description = 'Create a new controller class in a modular package'; | ||
protected function getRelativeNamespace(): string | ||
{ | ||
return '\\Http\\Controllers'; | ||
} | ||
|
||
protected function buildParentReplacements(): array | ||
{ | ||
$parentModelClass = $this->parseModel($this->option('parent')); | ||
|
||
if (! class_exists($parentModelClass, false) && | ||
confirm("A {$parentModelClass} model does not exist. Do you want to generate it?", default: true)) { | ||
$this->call('modular:make-model', ['name' => $parentModelClass, 'module' => $this->getModule()->name()]); | ||
} | ||
|
||
return [ | ||
'ParentDummyFullModelClass' => $parentModelClass, | ||
'{{ namespacedParentModel }}' => $parentModelClass, | ||
'{{namespacedParentModel}}' => $parentModelClass, | ||
'ParentDummyModelClass' => class_basename($parentModelClass), | ||
'{{ parentModel }}' => class_basename($parentModelClass), | ||
'{{parentModel}}' => class_basename($parentModelClass), | ||
'ParentDummyModelVariable' => lcfirst(class_basename($parentModelClass)), | ||
'{{ parentModelVariable }}' => lcfirst(class_basename($parentModelClass)), | ||
'{{parentModelVariable}}' => lcfirst(class_basename($parentModelClass)), | ||
]; | ||
} | ||
|
||
/** | ||
* Build the model replacement values. | ||
* | ||
* @param array $replace | ||
* @return array | ||
*/ | ||
protected function buildModelReplacements(array $replace): array | ||
{ | ||
$modelClass = $this->parseModel($this->option('model')); | ||
|
||
if (! class_exists($modelClass, false) && confirm("A {$modelClass} model does not exist. Do you want to generate it?", default: true)) { | ||
$this->call('modular:make-model', ['name' => $modelClass,'module' => $this->getModule()->name()]); | ||
} | ||
|
||
$replace = $this->buildFormRequestReplacements($replace, $modelClass); | ||
|
||
return array_merge($replace, [ | ||
'DummyFullModelClass' => $modelClass, | ||
'{{ namespacedModel }}' => $modelClass, | ||
'{{namespacedModel}}' => $modelClass, | ||
'DummyModelClass' => class_basename($modelClass), | ||
'{{ model }}' => class_basename($modelClass), | ||
'{{model}}' => class_basename($modelClass), | ||
'DummyModelVariable' => lcfirst(class_basename($modelClass)), | ||
'{{ modelVariable }}' => lcfirst(class_basename($modelClass)), | ||
'{{modelVariable}}' => lcfirst(class_basename($modelClass)), | ||
]); | ||
} | ||
|
||
protected function buildFormRequestReplacements(array $replace, $modelClass): array | ||
{ | ||
[$namespace, $storeRequestClass, $updateRequestClass] = [ | ||
'Illuminate\\Http', 'Request', 'Request', | ||
]; | ||
|
||
if ($this->option('requests')) { | ||
$namespace = $this->getModule()->makeNamespace('\\Http\\Requests'); | ||
|
||
[$storeRequestClass, $updateRequestClass] = $this->generateFormRequests( | ||
$modelClass, $storeRequestClass, $updateRequestClass | ||
); | ||
} | ||
|
||
$namespacedRequests = $namespace.'\\'.$storeRequestClass.';'; | ||
|
||
if ($storeRequestClass !== $updateRequestClass) { | ||
$namespacedRequests .= PHP_EOL.'use '.$namespace.'\\'.$updateRequestClass.';'; | ||
} | ||
|
||
return array_merge($replace, [ | ||
'{{ storeRequest }}' => $storeRequestClass, | ||
'{{storeRequest}}' => $storeRequestClass, | ||
'{{ updateRequest }}' => $updateRequestClass, | ||
'{{updateRequest}}' => $updateRequestClass, | ||
'{{ namespacedStoreRequest }}' => $namespace.'\\'.$storeRequestClass, | ||
'{{namespacedStoreRequest}}' => $namespace.'\\'.$storeRequestClass, | ||
'{{ namespacedUpdateRequest }}' => $namespace.'\\'.$updateRequestClass, | ||
'{{namespacedUpdateRequest}}' => $namespace.'\\'.$updateRequestClass, | ||
'{{ namespacedRequests }}' => $namespacedRequests, | ||
'{{namespacedRequests}}' => $namespacedRequests, | ||
]); | ||
} | ||
|
||
/** | ||
* @param $modelClass | ||
* @param $storeRequestClass | ||
* @param $updateRequestClass | ||
* @return string[] | ||
* TODO: Replace this with a call to the modular:make-request command | ||
*/ | ||
protected function generateFormRequests($modelClass, $storeRequestClass, $updateRequestClass): array | ||
{ | ||
$storeRequestClass = 'Store'.class_basename($modelClass).'Request'; | ||
|
||
$this->call('make:request', [ | ||
'name' => $storeRequestClass, | ||
]); | ||
|
||
$updateRequestClass = 'Update'.class_basename($modelClass).'Request'; | ||
|
||
$this->call('make:request', [ | ||
'name' => $updateRequestClass, | ||
]); | ||
|
||
return [$storeRequestClass, $updateRequestClass]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters