|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Rawilk\LaravelModules\Commands\Generators; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Support\Str; |
|
6
|
|
|
use Rawilk\LaravelModules\Support\Config\GenerateConfigReader; |
|
7
|
|
|
use Rawilk\LaravelModules\Support\Stub; |
|
8
|
|
|
use Rawilk\LaravelModules\Traits\ModuleCommands; |
|
9
|
|
|
|
|
10
|
|
|
class ModelMakeCommand extends GeneratorCommand |
|
11
|
|
|
{ |
|
12
|
|
|
use ModuleCommands; |
|
13
|
|
|
|
|
14
|
|
|
/** @var string */ |
|
15
|
|
|
protected $argumentName = 'model'; |
|
16
|
|
|
|
|
17
|
|
|
/** @var string */ |
|
18
|
|
|
protected $signature = 'module:make-model |
|
19
|
|
|
{model : The name of the model} |
|
20
|
|
|
{module? : The name of the module to create the model for} |
|
21
|
|
|
{--fillable= : The fillable attributes} |
|
22
|
|
|
{--base_class= : Override the default base model class (from config)} |
|
23
|
|
|
{--table= : The name of the database table} |
|
24
|
|
|
{--m|migration : Create a migration for the model as well}'; |
|
25
|
|
|
|
|
26
|
|
|
/** @var string */ |
|
27
|
|
|
protected $description = 'Create a new model for the specified module.'; |
|
28
|
|
|
|
|
29
|
|
|
public function handle(): void |
|
30
|
|
|
{ |
|
31
|
|
|
parent::handle(); |
|
32
|
|
|
|
|
33
|
|
|
$this->handleOptionalMigration(); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
protected function getDefaultNamespace(): string |
|
37
|
|
|
{ |
|
38
|
|
|
/** @var \Rawilk\LaravelModules\Contracts\Repository $module */ |
|
39
|
|
|
$module = $this->laravel['modules']; |
|
40
|
|
|
|
|
41
|
|
|
return $module->config('paths.generator.model.namespace') ?: $module->config('paths.generator.model.path', 'Models'); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
protected function getDestinationFilePath(): string |
|
45
|
|
|
{ |
|
46
|
|
|
$path = $this->laravel['modules']->getModulePath($this->getModuleName()); |
|
47
|
|
|
|
|
48
|
|
|
$modelPath = GenerateConfigReader::read('model'); |
|
49
|
|
|
|
|
50
|
|
|
return $path . $modelPath->getPath() . '/' . $this->getModelName() . '.php'; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
protected function getTemplateContents(): string |
|
54
|
|
|
{ |
|
55
|
|
|
/** @var \Rawilk\LaravelModules\Module $module */ |
|
56
|
|
|
$module = $this->laravel['modules']->findOrFail($this->getModuleName()); |
|
57
|
|
|
|
|
58
|
|
|
return (new Stub('/model.stub', [ |
|
59
|
|
|
'FILLABLE' => $this->getFillable(), |
|
60
|
|
|
'NAME' => $this->getModelName(), |
|
61
|
|
|
'NAMESPACE' => $this->getClassNamespace($module), |
|
62
|
|
|
'CLASS' => $this->getClass(), |
|
63
|
|
|
'LOWER_NAME' => $module->getLowerName(), |
|
64
|
|
|
'STUDLY_NAME' => $module->getStudlyName(), |
|
65
|
|
|
'MODULE_NAMESPACE' => $this->laravel['modules']->config('namespace'), |
|
66
|
|
|
'TABLE' => $this->createMigrationName(), |
|
67
|
|
|
'BASE_CLASS' => $this->getBaseClass('model'), |
|
68
|
|
|
'BASE_CLASS_SHORT' => $this->getBaseClass('model', true), |
|
69
|
|
|
]))->render(); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
private function createMigrationName(): string |
|
73
|
|
|
{ |
|
74
|
|
|
if ($table = $this->option('table')) { |
|
75
|
|
|
return $table; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
$pieces = preg_split( |
|
79
|
|
|
'/(?=[A-Z])/', |
|
80
|
|
|
class_basename($this->argument('model')), |
|
|
|
|
|
|
81
|
|
|
-1, |
|
82
|
|
|
PREG_SPLIT_NO_EMPTY |
|
83
|
|
|
); |
|
84
|
|
|
|
|
85
|
|
|
$name = ''; |
|
86
|
|
|
$count = count($pieces); |
|
|
|
|
|
|
87
|
|
|
|
|
88
|
|
|
foreach ($pieces as $index => $piece) { |
|
89
|
|
|
if ($index + 1 < $count) { |
|
90
|
|
|
$name .= strtolower($piece) . '_'; |
|
91
|
|
|
} else { |
|
92
|
|
|
$name .= Str::plural(strtolower($piece)); |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
return $name; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
private function getFillable(): string |
|
100
|
|
|
{ |
|
101
|
|
|
$fillable = $this->option('fillable'); |
|
102
|
|
|
|
|
103
|
|
|
if ($fillable !== null) { |
|
|
|
|
|
|
104
|
|
|
return str_replace(['"', ','], ["'", ', '], json_encode(explode(',', $fillable))); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
return '[]'; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
private function getModelName(): string |
|
111
|
|
|
{ |
|
112
|
|
|
return Str::studly($this->argument($this->argumentName)); |
|
|
|
|
|
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
private function handleOptionalMigration(): void |
|
116
|
|
|
{ |
|
117
|
|
|
if ($this->option('migration')) { |
|
118
|
|
|
$migrationName = 'create_' . $this->createMigrationName() . '_table'; |
|
119
|
|
|
|
|
120
|
|
|
$this->call('module:make-migration', [ |
|
121
|
|
|
'name' => $migrationName, |
|
122
|
|
|
'module' => $this->argument('module') |
|
123
|
|
|
]); |
|
124
|
|
|
} |
|
125
|
|
|
} |
|
126
|
|
|
} |
|
127
|
|
|
|