Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New Feature: Controller Make Command #9

Merged
merged 1 commit into from
Apr 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
127 changes: 127 additions & 0 deletions src/Commands/ControllerMakeCommand.php
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];
}
}
6 changes: 4 additions & 2 deletions src/Commands/ModelMakeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,9 @@ protected function createSeeder(): void
{
$seeder = Str::studly(class_basename($this->argument('name')));

$this->call('make:seeder', [
$this->call('modular:make-seeder', [
'name' => "{$seeder}Seeder",
'module' => $this->getModule()->name(),
]);
}

Expand All @@ -67,7 +68,8 @@ protected function createController(): void

$modelName = $this->qualifyClass($this->getNameInput());

$this->call('make:controller', array_filter([
$this->call('modular:make-controller', array_filter([
'module' => $this->getModule()->name(),
'name' => "{$controller}Controller",
'--model' => $this->option('resource') || $this->option('api') ? $modelName : null,
'--api' => $this->option('api'),
Expand Down
2 changes: 1 addition & 1 deletion src/Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public function __construct(string $name)
$this->title = Str::of($name)->kebab()->title()->replace('-', ' ')->toString();
// If the module does not exist, throw an error
if (! is_dir(app()->basePath(config('modular.path').DIRECTORY_SEPARATOR.$this->name))) {
abort(404, "Module $name does not exist");
abort(404, "Module $this->title does not exist");
}
$this->studlyName = Str::of($name)->studly()->toString();
$this->namespace = config('modular.namespace').'\\'.$this->studlyName;
Expand Down
19 changes: 18 additions & 1 deletion src/Support/Concerns/GeneratesModularFiles.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Savannabits\Modular\Facades\Modular;
use Savannabits\Modular\Module;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Finder\Finder;

trait GeneratesModularFiles
{
Expand All @@ -24,7 +25,12 @@ protected function resolveStubPath($stub): string
}
public function getModule(): Module
{
return Modular::module($this->argument('module'));
try {
return Modular::module($this->argument('module'));
} catch (\Throwable $e) {
$this->error($e->getMessage());
exit(1);
}
}

protected function getDefaultNamespace($rootNamespace): string
Expand All @@ -48,4 +54,15 @@ protected function getPath($name): string

return $this->getModule()->srcPath(str_replace('\\', '/', $name).'.php');
}

protected function possibleModels()
{
$modelPath = $this->getModule()->srcPath('Models');

return collect(Finder::create()->files()->depth(0)->in($modelPath))
->map(fn ($file) => $file->getBasename('.php'))
->sort()
->values()
->all();
}
}
Loading