Skip to content

Commit e646eaf

Browse files
authored
Merge pull request #123 from ahmed-abdelnaby-fahmy/feat/enhance-model-command
feat: enhance make:model command
2 parents 21afd31 + ab81016 commit e646eaf

File tree

3 files changed

+84
-5
lines changed

3 files changed

+84
-5
lines changed

src/devtool/src/Generator/FactoryCommand.php

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
namespace Hypervel\Devtool\Generator;
66

77
use Hyperf\Devtool\Generator\GeneratorCommand;
8+
use Hypervel\Support\Str;
9+
use Symfony\Component\Console\Input\InputOption;
810

911
class FactoryCommand extends GeneratorCommand
1012
{
@@ -29,17 +31,23 @@ protected function getModelNamespace(string $name): string
2931
{
3032
$namespace = $this->getConfig()['model_namespace'] ?? 'App\Models';
3133

32-
return "{$namespace}\\{{$name}}";
34+
return "{$namespace}\\{$name}";
3335
}
3436

3537
/**
3638
* Replace the class name for the given stub.
3739
*/
3840
protected function replaceClass(string $stub, string $name): string
3941
{
42+
$factory = Str::ucfirst(str_replace('Factory', '', $name));
43+
44+
$model = $this->input->getOption('model')
45+
? $this->input->getOption('model')
46+
: $factory;
47+
4048
$replace = [
41-
'%CLASS%' => $name,
42-
'%MODEL_NAMESPACE%' => $this->getModelNamespace($name),
49+
'%CLASS%' => $model,
50+
'%MODEL_NAMESPACE%' => $this->getModelNamespace($model),
4351
];
4452

4553
return str_replace(
@@ -71,4 +79,13 @@ protected function getDefaultNamespace(): string
7179
{
7280
return '';
7381
}
82+
83+
protected function getOptions(): array
84+
{
85+
return [
86+
['force', 'f', InputOption::VALUE_NONE, 'Whether force to rewrite.'],
87+
['namespace', 'N', InputOption::VALUE_OPTIONAL, 'The namespace for class.', null],
88+
['model', 'm', InputOption::VALUE_OPTIONAL, 'The name of the model'],
89+
];
90+
}
7491
}

src/devtool/src/Generator/ModelCommand.php

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ public function execute(InputInterface $input, OutputInterface $output): int
3636
$this->input->setOption('factory', true);
3737
$this->input->setOption('seed', true);
3838
$this->input->setOption('migration', true);
39+
$this->input->setOption('controller', true);
40+
$this->input->setOption('policy', true);
41+
$this->input->setOption('resource', true);
3942
}
4043

4144
if ($this->input->getOption('factory')) {
@@ -50,6 +53,15 @@ public function execute(InputInterface $input, OutputInterface $output): int
5053
$this->createSeeder();
5154
}
5255

56+
if ($this->input->getOption('controller') || $this->input->getOption('resource') || $this->input->getOption('api')) {
57+
$this->createController();
58+
} elseif ($this->input->getOption('requests')) {
59+
$this->createFormRequests();
60+
}
61+
62+
if ($this->input->getOption('policy')) {
63+
$this->createPolicy();
64+
}
5365
return 0;
5466
}
5567

@@ -84,6 +96,11 @@ protected function getOptions(): array
8496
['force', null, InputOption::VALUE_NONE, 'Create the class even if the model already exists'],
8597
['migration', 'm', InputOption::VALUE_NONE, 'Create a new migration file for the model'],
8698
['seed', 's', InputOption::VALUE_NONE, 'Create a new seeder for the model'],
99+
['controller', 'c', InputOption::VALUE_NONE, 'Create a new controller for the model'],
100+
['policy', null, InputOption::VALUE_NONE, 'Create a new policy for the model'],
101+
['resource', 'r', InputOption::VALUE_NONE, 'Indicates if the generated controller should be a resource controller'],
102+
['api', null, InputOption::VALUE_NONE, 'Indicates if the generated controller should be an API resource controller'],
103+
['requests', 'R', InputOption::VALUE_NONE, 'Create new form request classes and use them in the resource controller'],
87104
];
88105
}
89106

@@ -126,6 +143,52 @@ protected function createSeeder()
126143
]);
127144
}
128145

146+
/**
147+
* Create a controller for the model.
148+
*/
149+
protected function createController()
150+
{
151+
$controller = Str::studly($this->input->getArgument('name'));
152+
153+
$modelName = $this->qualifyClass($this->getNameInput());
154+
155+
$this->call('make:controller', array_filter([
156+
'name' => "{$controller}Controller",
157+
'--model' => $this->input->getOption('resource') || $this->input->getOption('api') ? $modelName : null,
158+
'--api' => $this->input->getOption('api'),
159+
'--requests' => $this->input->getOption('requests') || $this->input->getOption('all'),
160+
]));
161+
}
162+
163+
/**
164+
* Create the form requests for the model.
165+
*/
166+
protected function createFormRequests()
167+
{
168+
$request = Str::studly($this->input->getArgument('name'));
169+
170+
$this->call('make:request', [
171+
'name' => "Store{$request}Request",
172+
]);
173+
174+
$this->call('make:request', [
175+
'name' => "Update{$request}Request",
176+
]);
177+
}
178+
179+
/**
180+
* Create a policy file for the model.
181+
*/
182+
protected function createPolicy()
183+
{
184+
$policy = Str::studly($this->input->getArgument('name'));
185+
186+
$this->call('make:policy', [
187+
'name' => "{$policy}Policy",
188+
'--model' => $policy,
189+
]);
190+
}
191+
129192
protected function call(string $command, array $parameters = []): int
130193
{
131194
return $this->getApplication()->doRun(

src/devtool/src/Generator/stubs/policy.stub

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,8 @@ declare(strict_types=1);
44

55
namespace %NAMESPACE%;
66

7-
use Hypervel\Auth\Access\Response;
8-
use %NAMESPACED_MODEL%;
97
use %NAMESPACED_USER_MODEL%;
8+
use %NAMESPACED_MODEL%;
109

1110
class %CLASS%
1211
{

0 commit comments

Comments
 (0)