-
Notifications
You must be signed in to change notification settings - Fork 1
/
CreateModuleVuePageIndex.php
179 lines (153 loc) · 5.23 KB
/
CreateModuleVuePageIndex.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
<?php
namespace Vheins\LaravelModuleGenerator\Console;
use Illuminate\Support\Str;
use Nwidart\Modules\Commands\GeneratorCommand;
use Nwidart\Modules\Support\Config\GenerateConfigReader;
use Nwidart\Modules\Support\Stub;
use Nwidart\Modules\Traits\ModuleCommandTrait;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;
final class CreateModuleVuePageIndex extends GeneratorCommand
{
use ModuleCommandTrait;
/**
* The console command name.
*
* @var string
*/
protected $name = 'create:module:vue:page:index';
protected $argumentName = 'name';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Create a new Vue Page Index for the specified module.';
/**
* Get the console command options.
*
* @return array
*/
protected function getOptions()
{
return [
['fillable', null, InputOption::VALUE_OPTIONAL, 'The fillable attributes.', null],
];
}
public function getDefaultNamespace(): string
{
$module = $this->laravel['modules'];
return $module->config('paths.generator.vue-pages.namespace') ?: $module->config('paths.generator.vue-pages.path', 'vue/stores');
}
/**
* Get template contents.
*
* @return string
*/
protected function getTemplateContents()
{
$module = $this->laravel['modules']->findOrFail($this->getModuleName());
$classNames = explode('_', Str::of($this->getClass())->snake());
$splitNames = [];
foreach ($classNames as $className) {
$splitNames[] = Str::of($className)->singular();
}
$unique = array_unique($splitNames);
$unique = implode('_', $unique);
$classNames = Str::of($unique)->studly();
return (new Stub('/vue/page.index.stub', [
'STUDLY_NAME' => $module->getStudlyName(),
'API_ROUTE' => $this->pageUrl($module->getStudlyName()),
'CLASS' => $classNames,
'LOWER_NAME' => $module->getLowerName(),
'MODULE' => $this->getModuleName(),
'SEARCHABLE' => $this->getSearchable(),
'HEADER' => $this->getHeader(),
// 'NAME' => $this->getModelName(),
// 'NAMESPACE' => $this->getClassNamespace($module),
// 'MODULE_NAMESPACE' => $this->laravel['modules']->config('namespace'),
]))->render();
}
/**
* @return string
*/
private function getHeader()
{
$fillable = $this->option('fillable');
if (! is_null($fillable)) {
foreach (explode(',', $fillable) as $var) {
$key = explode(':', $var)[1];
$val = explode(':', $var)[0];
if (in_array($key, [
'foreignId', 'foreignUuid', 'foreignUlid',
])) {
$val = Str::of($val)->replace('_id', '')->toString().'.name';
}
$arrays[] = "{ '".Str::camel($val)."': '".Str::of($val)->replace('.', '_')->headline()."' }";
}
return "[\n\t\t\t\t".implode(", \n\t\t\t\t", $arrays)."\n\t\t\t]";
}
return '[]';
}
/**
* @return string
*/
private function getSearchable()
{
$fillable = $this->option('fillable');
if (! is_null($fillable)) {
foreach (explode(',', $fillable) as $var) {
$key = explode(':', $var)[1];
$val = explode(':', $var)[0];
if (in_array($key, [
'foreignId', 'foreignUuid', 'foreignUlid',
])) {
$val = Str::of($val)->replace('_id', '')->toString().'.name';
}
$arrays[] = "'".Str::camel($val)."'";
}
return '['.implode(', ', $arrays).']';
}
return '[]';
}
/**
* Get the destination file path.
*
* @return string
*/
protected function getDestinationFilePath()
{
$path = $this->laravel['modules']->getModulePath($this->getModuleName());
$module = $this->laravel['modules']->findOrFail($this->getModuleName());
$Path = GenerateConfigReader::read('vue-pages');
return $path.$Path->getPath().'/dashboard/'.$this->pageUrl().'/index.vue';
}
private function pageUrl()
{
if ($this->argument('name') == $this->argument('module')) {
return Str::of($this->argument('module'))->headline()->plural()->slug();
} else {
return Str::of($this->argument('module'))->headline()->plural()->slug().'/'.
Str::of($this->argument('name'))->remove($this->argument('module'), false)->headline()->plural()->slug();
}
}
/**
* @return string
*/
private function getFileName()
{
return Str::studly($this->argument('name'));
}
/**
* Get the console command arguments.
*
* @return array
*/
protected function getArguments()
{
return [
['name', InputArgument::REQUIRED, 'The name of the notification class.'],
['module', InputArgument::OPTIONAL, 'The name of module will be used.'],
];
}
}