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