1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace Modelarium\Frontend; |
4
|
|
|
|
5
|
|
|
use Formularium\Datatype; |
6
|
|
|
use Formularium\Element; |
7
|
|
|
use Formularium\Field; |
8
|
|
|
use Formularium\Model; |
9
|
|
|
use Formularium\FrameworkComposer; |
10
|
|
|
use Formularium\Frontend\Blade\Framework as FrameworkBlade; |
11
|
|
|
use Formularium\Frontend\Vue\Framework as FrameworkVue; |
12
|
|
|
use Modelarium\GeneratedCollection; |
13
|
|
|
use Modelarium\GeneratedItem; |
14
|
|
|
use Modelarium\GeneratorInterface; |
15
|
|
|
use Modelarium\GeneratorNameTrait; |
16
|
|
|
|
17
|
|
|
use function Safe\file_get_contents; |
18
|
|
|
|
19
|
|
|
class FrontendGenerator implements GeneratorInterface |
20
|
|
|
{ |
21
|
|
|
use GeneratorNameTrait; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var FrameworkComposer |
25
|
|
|
*/ |
26
|
|
|
protected $composer = null; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var Model |
30
|
|
|
*/ |
31
|
|
|
protected $model = null; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var GeneratedCollection |
35
|
|
|
*/ |
36
|
|
|
protected $collection; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* |
40
|
|
|
* @var string |
41
|
|
|
*/ |
42
|
|
|
protected $stubDir = __DIR__ . '/stubs'; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* String substitution |
46
|
|
|
* |
47
|
|
|
* @var string[] |
48
|
|
|
*/ |
49
|
|
|
protected $templateParameters = []; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Fields |
53
|
|
|
* |
54
|
|
|
* @var Field[] |
55
|
|
|
*/ |
56
|
|
|
protected $cardFields = []; |
57
|
|
|
|
58
|
|
|
public function __construct(FrameworkComposer $composer, Model $model) |
59
|
|
|
{ |
60
|
|
|
$this->composer = $composer; |
61
|
|
|
$this->model = $model; |
62
|
|
|
$this->setName($model->getName()); |
63
|
|
|
$this->buildTemplateParameters(); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function generate(): GeneratedCollection |
67
|
|
|
{ |
68
|
|
|
$this->collection = new GeneratedCollection(); |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @var FrameworkVue $vue |
72
|
|
|
*/ |
73
|
|
|
$vue = $this->composer->getByName('Vue'); |
74
|
|
|
// $blade = FrameworkComposer::getByName('Blade'); |
75
|
|
|
|
76
|
|
|
if ($vue !== null) { |
77
|
|
|
$this->makeVue($vue, 'Card', 'viewable'); |
78
|
|
|
$this->makeVue($vue, 'List', 'viewable'); |
79
|
|
|
$this->makeVue($vue, 'Show', 'viewable'); |
80
|
|
|
$this->makeVue($vue, 'Form', 'editable'); |
81
|
|
|
$this->makeVueRoutes(); |
82
|
|
|
$this->makeVueIndex(); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
$this->makeGraphql(); |
86
|
|
|
|
87
|
|
|
return $this->collection; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
protected function buildTemplateParameters(): void |
91
|
|
|
{ |
92
|
|
|
$this->cardFields = $this->model->filterField( |
93
|
|
|
function (Field $field) { |
94
|
|
|
return $field->getRenderable('card', false); |
95
|
|
|
} |
96
|
|
|
); |
97
|
|
|
|
98
|
|
|
$this->templateParameters = [ |
99
|
|
|
'submitButton' => $this->composer->element('Button', [Element::LABEL => 'Submit']) |
100
|
|
|
]; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
public function templateCallback(string $stub, FrameworkVue $vue, array $data, Model $m): string |
|
|
|
|
104
|
|
|
{ |
105
|
|
|
$x = $this->templateFile( |
106
|
|
|
$stub, |
107
|
|
|
array_merge( |
108
|
|
|
$this->templateParameters, |
109
|
|
|
$data |
110
|
|
|
) |
111
|
|
|
); |
112
|
|
|
return $x; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
protected function makeVue(FrameworkVue $vue, string $component, string $mode): void |
116
|
|
|
{ |
117
|
|
|
$path = $this->model->getName() . '/' . |
118
|
|
|
$this->model->getName() . $component . '.vue'; |
119
|
|
|
|
120
|
|
|
$stub = $this->stubDir . "/Vue{$component}.mustache.vue"; |
121
|
|
|
|
122
|
|
|
if ($mode == 'editable') { |
123
|
|
|
$vue->setEditableTemplate( |
124
|
|
|
function (FrameworkVue $vue, array $data, Model $m) use ($stub) { |
125
|
|
|
return $this->templateCallback($stub, $vue, $data, $m); |
126
|
|
|
} |
127
|
|
|
); |
128
|
|
|
|
129
|
|
|
$this->collection->push( |
130
|
|
|
new GeneratedItem( |
131
|
|
|
GeneratedItem::TYPE_FRONTEND, |
132
|
|
|
$this->model->editable($this->composer), |
133
|
|
|
$path |
134
|
|
|
) |
135
|
|
|
); |
136
|
|
|
} else { |
137
|
|
|
$vue->setViewableTemplate( |
138
|
|
|
function (FrameworkVue $vue, array $data, Model $m) use ($stub) { |
139
|
|
|
return $this->templateCallback($stub, $vue, $data, $m); |
140
|
|
|
} |
141
|
|
|
); |
142
|
|
|
|
143
|
|
|
$this->collection->push( |
144
|
|
|
new GeneratedItem( |
145
|
|
|
GeneratedItem::TYPE_FRONTEND, |
146
|
|
|
$this->model->viewable($this->composer, []), |
147
|
|
|
$path |
148
|
|
|
) |
149
|
|
|
); |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
protected function makeGraphql(): void |
154
|
|
|
{ |
155
|
|
|
$cardFieldNames = array_map( |
156
|
|
|
function (Field $field) { |
157
|
|
|
return $field->getName(); |
158
|
|
|
}, |
159
|
|
|
$this->cardFields |
160
|
|
|
); |
161
|
|
|
$cardFieldParameters = implode("\n", $cardFieldNames); |
162
|
|
|
|
163
|
|
|
$listQuery = <<<EOF |
164
|
|
|
query (\$page: Int!) { |
165
|
|
|
{$this->lowerNamePlural}(page: \$page) { |
166
|
|
|
data { |
167
|
|
|
id |
168
|
|
|
$cardFieldParameters |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
paginatorInfo { |
172
|
|
|
currentPage |
173
|
|
|
perPage |
174
|
|
|
total |
175
|
|
|
lastPage |
176
|
|
|
} |
177
|
|
|
} |
178
|
|
|
} |
179
|
|
|
EOF; |
180
|
|
|
|
181
|
|
|
$this->collection->push( |
182
|
|
|
new GeneratedItem( |
183
|
|
|
GeneratedItem::TYPE_FRONTEND, |
184
|
|
|
$listQuery, |
185
|
|
|
$this->model->getName() . '/queryList.graphql' |
186
|
|
|
) |
187
|
|
|
); |
188
|
|
|
|
189
|
|
|
$itemQuery = <<<EOF |
190
|
|
|
query (\$id: ID!) { |
191
|
|
|
{$this->lowerName}(id: \$id) { |
192
|
|
|
id |
193
|
|
|
TODO |
194
|
|
|
} |
195
|
|
|
} |
196
|
|
|
EOF; |
197
|
|
|
|
198
|
|
|
$this->collection->push( |
199
|
|
|
new GeneratedItem( |
200
|
|
|
GeneratedItem::TYPE_FRONTEND, |
201
|
|
|
$itemQuery, |
202
|
|
|
$this->model->getName() . '/queryItem.graphql' |
203
|
|
|
) |
204
|
|
|
); |
205
|
|
|
|
206
|
|
|
// TODO: variables |
207
|
|
|
$createMutation = <<<EOF |
208
|
|
|
mutation create(\$name: String!) { |
209
|
|
|
{$this->lowerName}Create(name: \$name) { |
210
|
|
|
TODO |
211
|
|
|
} |
212
|
|
|
} |
213
|
|
|
EOF; |
214
|
|
|
$this->collection->push( |
215
|
|
|
new GeneratedItem( |
216
|
|
|
GeneratedItem::TYPE_FRONTEND, |
217
|
|
|
$createMutation, |
218
|
|
|
$this->model->getName() . '/mutationCreate.graphql' |
219
|
|
|
) |
220
|
|
|
); |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
protected function makeVueIndex(): void |
224
|
|
|
{ |
225
|
|
|
$path = $this->model->getName() . '/index.js'; |
226
|
|
|
$name = $this->studlyName; |
227
|
|
|
|
228
|
|
|
$items = [ |
229
|
|
|
'Card', |
230
|
|
|
'Form', |
231
|
|
|
'List', |
232
|
|
|
'Show', |
233
|
|
|
]; |
234
|
|
|
|
235
|
|
|
$import = array_map( |
236
|
|
|
function ($i) use ($name) { |
237
|
|
|
return "import {$name}$i from './{$name}$i.vue';"; |
238
|
|
|
}, |
239
|
|
|
$items |
240
|
|
|
); |
241
|
|
|
|
242
|
|
|
$export = array_map( |
243
|
|
|
function ($i) use ($name) { |
244
|
|
|
return " {$name}$i,\n"; |
245
|
|
|
}, |
246
|
|
|
$items |
247
|
|
|
); |
248
|
|
|
|
249
|
|
|
$this->collection->push( |
250
|
|
|
new GeneratedItem( |
251
|
|
|
GeneratedItem::TYPE_FRONTEND, |
252
|
|
|
implode("\n", $import) . "\n" . |
253
|
|
|
"export {\n" . |
254
|
|
|
implode("\n", $export) . "\n};\n", |
255
|
|
|
$path |
256
|
|
|
) |
257
|
|
|
); |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
protected function makeVueRoutes(): void |
261
|
|
|
{ |
262
|
|
|
$path = $this->model->getName() . '/routes.js'; |
263
|
|
|
|
264
|
|
|
$this->collection->push( |
265
|
|
|
new GeneratedItem( |
266
|
|
|
GeneratedItem::TYPE_FRONTEND, |
267
|
|
|
$this->templateFile($this->stubDir . "/routes.mustache.js"), |
268
|
|
|
$path |
269
|
|
|
) |
270
|
|
|
); |
271
|
|
|
} |
272
|
|
|
} |
273
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.