1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace EasyPanel\Commands\Actions; |
5
|
|
|
|
6
|
|
|
|
7
|
|
|
use InvalidArgumentException; |
8
|
|
|
use Symfony\Component\Console\Exception\CommandNotFoundException; |
9
|
|
|
|
10
|
|
|
trait StubParser |
11
|
|
|
{ |
12
|
|
|
|
13
|
|
|
protected function getDefaultNamespace($rootNamespace) |
14
|
|
|
{ |
15
|
|
|
$name = ucfirst($this->getNameInput()); |
|
|
|
|
16
|
|
|
$this->path = parent::getDefaultNamespace($rootNamespace)."\\Http\\Livewire\\Admin\\$name"; |
|
|
|
|
17
|
|
|
|
18
|
|
|
return $this->path; |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
protected function replaceModel($stub) |
22
|
|
|
{ |
23
|
|
|
$fields = $this->getConfig('fields'); |
24
|
|
|
if(!$fields) { |
25
|
|
|
throw new CommandNotFoundException("There is no `field` in your config file"); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
$modelNamespace = $this->parseModel($this->getConfig('model')); |
29
|
|
|
$modelName = $this->getModelName($modelNamespace); |
30
|
|
|
|
31
|
|
|
$array = [ |
32
|
|
|
'{{ modelName }}' => $modelName, |
33
|
|
|
'{{ modelNamespace }}' => $modelNamespace, |
34
|
|
|
'{{ model }}' => strtolower($modelName), |
35
|
|
|
'{{ properties }}' => $this->parseProperties($fields), |
36
|
|
|
'{{ rules }}' => $this->parseValidationRules(), |
37
|
|
|
'{{ fields }}' => $this->parseFields($fields), |
38
|
|
|
'{{ setProperties }}' => $this->parsePropertiesValue($fields), |
39
|
|
|
]; |
40
|
|
|
|
41
|
|
|
return str_replace(array_keys($array), array_values($array), $stub); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
protected function parseBlade($stub){ |
45
|
|
|
$modelNamespace = $this->parseModel($this->getConfig('model')); |
46
|
|
|
$modelName = $this->getModelName($modelNamespace); |
47
|
|
|
$array = [ |
48
|
|
|
'{{ model }}' => strtolower($modelName), |
49
|
|
|
'{{ modelName }}' => $modelName, |
50
|
|
|
'{{ data }}' => $this->parseDataInBlade($modelName), |
51
|
|
|
'{{ titles }}' => $this->parseTitlesInBlade(), |
52
|
|
|
'{{ inputs }}' => $this->parseInputsInBlade(), |
53
|
|
|
]; |
54
|
|
|
|
55
|
|
|
return str_replace(array_keys($array), array_values($array), $stub); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
protected function parseModel($model) |
59
|
|
|
{ |
60
|
|
|
if (preg_match('([^A-Za-z0-9_/\\\\])', $model)) { |
61
|
|
|
throw new InvalidArgumentException('Model name contains invalid characters.'); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
return $this->qualifyModel($model); |
|
|
|
|
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
protected function getConfig($key){ |
68
|
|
|
$action = $this->getNameInput(); |
69
|
|
|
|
70
|
|
|
if(config('easy_panel.actions.'.$action.'.'.$key)){ |
71
|
|
|
return config('easy_panel.actions.'.$action.'.'.$key); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
throw new CommandNotFoundException("There is no {$key} in {$action} config."); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
protected function getModelName($modelNamespace) |
78
|
|
|
{ |
79
|
|
|
$array = explode('\\', $modelNamespace); |
80
|
|
|
|
81
|
|
|
return end($array); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
protected function parseProperties($fields) |
85
|
|
|
{ |
86
|
|
|
$fields = array_keys($fields); |
87
|
|
|
$str = ''; |
88
|
|
|
foreach ($fields as $field) { |
89
|
|
|
$str .= 'public $'.$field.";".$this->makeTab(1); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
return $str; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
protected function parsePropertiesValue($fields) |
96
|
|
|
{ |
97
|
|
|
$fields = array_keys($fields); |
98
|
|
|
$str = ''; |
99
|
|
|
$action = $this->getNameInput(); |
100
|
|
|
foreach ($fields as $field) { |
101
|
|
|
$str .= '$this->'.$field.' = $this->'.$action.'->'.$field.';'.$this->makeTab(1); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
return $str; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
protected function parseValidationRules() |
108
|
|
|
{ |
109
|
|
|
$rules = $this->getConfig('validation'); |
110
|
|
|
|
111
|
|
|
$str = ''; |
112
|
|
|
foreach ($rules as $key => $rule) { |
113
|
|
|
$str .= $rule != end($rules) ? "'$key' => '$rule',".$this->makeTab(2) : "'$key' => '$rule',"; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
return $str; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
protected function parseFields($fields) |
120
|
|
|
{ |
121
|
|
|
$str = ''; |
122
|
|
|
foreach ($fields as $key => $field) { |
123
|
|
|
$str .= $field != end($fields) ? "'$key' => " . '$this' . "->$key,".$this->makeTab(3) : "'$key' => " . '$this' . "->$key,"; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
return $str; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
protected function parseDataInBlade($modelName) |
130
|
|
|
{ |
131
|
|
|
$fields = $this->getConfig('show'); |
132
|
|
|
$str = ''; |
133
|
|
|
$modelName = strtolower($modelName); |
134
|
|
|
foreach ($fields as $value) { |
135
|
|
|
$str .= '<td> {{ $'.$modelName.'->'.$value." }} </td>".$this->makeTab(1); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
return $str; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
protected function parseTitlesInBlade() |
142
|
|
|
{ |
143
|
|
|
$fields = $this->getConfig('show'); |
144
|
|
|
$str = ''; |
145
|
|
|
foreach ($fields as $field) { |
146
|
|
|
$field = ucfirst($field); |
147
|
|
|
$str .= "<td> $field </td>".$this->makeTab(6); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
return $str; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
protected function parseInputsInBlade() |
154
|
|
|
{ |
155
|
|
|
$fields = $this->getConfig('fields'); |
156
|
|
|
|
157
|
|
|
$str = ''; |
158
|
|
|
foreach ($fields as $key => $type) { |
159
|
|
|
$str .= '<div class="form-group"><label for="input'.$key.'" class="col-sm-2 control-label">'.ucfirst($key).'</label>'.PHP_EOL; |
160
|
|
|
$str = $this->inputsHTML($type, $key, $str).PHP_EOL; |
161
|
|
|
$str .='@error("'.$key.'") <div class="invalid-feedback">{{ $message }}</div> @enderror</div>'.PHP_EOL; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
return $str; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
protected function inputsHTML($type, $key, string $str): string |
168
|
|
|
{ |
169
|
|
|
$array = [ |
170
|
|
|
'text' => '<input type="text" wire:model.lazy="' . $key . '" class="form-control @error(\''.$key.'\') is-invalid @enderror" id="input' . $key . '">'.PHP_EOL, |
171
|
|
|
'email' => '<input type="email" wire:model.lazy="' . $key . '" class="form-control @error(\''.$key.'\') is-invalid @enderror" id="input' . $key . '">'.PHP_EOL, |
172
|
|
|
'number' => '<input type="number" wire:model.lazy="' . $key . '" class="form-control @error(\''.$key.'\') is-invalid @enderror" id="input' . $key . '">'.PHP_EOL, |
173
|
|
|
'file' => '<input type="file" wire:model="' . $key . '" class="form-control-file @error(\''.$key.'\')is-invalid @enderror" id="input' . $key . '"">'.PHP_EOL, |
174
|
|
|
'textarea' => '<textarea wire:model="' . $key . '" class="form-control @error(\''.$key.'\')is-invalid @enderror"></textarea>'.PHP_EOL, |
175
|
|
|
'password' => '<input type="password" wire:model.lazy="' . $key . '" class="form-control @error(\''.$key.'\') is-invalid @enderror" id="input' . $key . '">'.PHP_EOL, |
176
|
|
|
]; |
177
|
|
|
$str .= $array[$type]; |
178
|
|
|
|
179
|
|
|
return $str; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
private function makeTab($count){ |
183
|
|
|
$count = $count * 4; |
184
|
|
|
$tabs = str_repeat(' ', $count); |
185
|
|
|
|
186
|
|
|
return "\n".$tabs; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
} |
190
|
|
|
|