1 | <?php |
||
2 | |||
3 | namespace LaravelModulize\Console\Commands; |
||
4 | |||
5 | use Illuminate\Support\Str; |
||
6 | use LaravelModulize\Support\Entity; |
||
7 | |||
8 | class GenerateControllerCommand extends BaseGeneratorCommand |
||
9 | { |
||
10 | /** |
||
11 | * The name and signature of the console command. |
||
12 | * |
||
13 | * @var string |
||
14 | */ |
||
15 | protected $signature = 'modulize:make:controller {module} {name} {--m|model=}'; |
||
16 | |||
17 | /** |
||
18 | * The console command description. |
||
19 | * |
||
20 | * @var string |
||
21 | */ |
||
22 | protected $description = 'Generate a controller for the module'; |
||
23 | |||
24 | /** |
||
25 | * Execute the console command. |
||
26 | * |
||
27 | * @return mixed |
||
28 | */ |
||
29 | public function handle() |
||
30 | { |
||
31 | parent::handle(); |
||
32 | } |
||
33 | |||
34 | /** |
||
35 | * Get the stub file for the generator. |
||
36 | * |
||
37 | * @return string |
||
38 | */ |
||
39 | protected function getStub(): string |
||
40 | { |
||
41 | if ($this->option('model')) { |
||
42 | return __DIR__ . '/stubs/ModelController.stub'; |
||
43 | } |
||
44 | |||
45 | return __DIR__ . '/stubs/Controller.stub'; |
||
46 | } |
||
47 | |||
48 | /** |
||
49 | * Get the destination class path. |
||
50 | * |
||
51 | * @param string $name |
||
52 | * @return string |
||
53 | */ |
||
54 | protected function getPath($name): string |
||
55 | { |
||
56 | return $this->repository->controllerPath($this->module) . '/' . $this->getNameInput() . '.php'; |
||
57 | } |
||
58 | |||
59 | /** |
||
60 | * Build the class with the given name. |
||
61 | * |
||
62 | * @param string $name |
||
63 | * @return string |
||
64 | */ |
||
65 | protected function buildClass($name) |
||
66 | { |
||
67 | $dummies = [ |
||
68 | 'DummyClass', |
||
69 | 'DummyModel', |
||
70 | 'DummyLowercasedModel', |
||
71 | 'DummyControllerNamespace', |
||
72 | 'DummyModuleNamespace', |
||
73 | 'ModelName', |
||
74 | ]; |
||
75 | |||
76 | $modelName = Entity::getClassName($this->option('model')); |
||
0 ignored issues
–
show
Unused Code
introduced
by
![]() |
|||
77 | |||
78 | $replacements = [ |
||
79 | Entity::getClassName($this->getNameInput()), |
||
80 | str_replace('/', '\\', $this->option('model')), |
||
81 | Str::lower($this->option('model')), |
||
82 | Entity::getClassNamespace($this->getNameInput()), |
||
83 | $this->repository->getModuleNamespace($this->module), |
||
84 | ]; |
||
85 | |||
86 | return str_replace($dummies, $replacements, parent::buildClass($name)); |
||
87 | } |
||
88 | |||
89 | /** |
||
90 | * Get the default namespace for the class. |
||
91 | * |
||
92 | * @param string $rootNamespace |
||
93 | * @return string |
||
94 | */ |
||
95 | protected function getDefaultNamespace($rootNamespace) |
||
96 | { |
||
97 | return $this->repository->getModuleNamespace($this->module); |
||
98 | } |
||
99 | } |
||
100 |