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