1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace Modelarium\Frontend; |
4
|
|
|
|
5
|
|
|
use Formularium\Model; |
6
|
|
|
use Formularium\FrameworkComposer; |
7
|
|
|
use Formularium\Frontend\Blade\Framework as FrameworkBlade; |
8
|
|
|
use Formularium\Frontend\Vue\Framework as FrameworkVue; |
9
|
|
|
use Modelarium\GeneratedCollection; |
10
|
|
|
use Modelarium\GeneratedItem; |
11
|
|
|
use Modelarium\GeneratorInterface; |
12
|
|
|
use Modelarium\GeneratorNameTrait; |
13
|
|
|
|
14
|
|
|
use function Safe\file_get_contents; |
15
|
|
|
|
16
|
|
|
class FrontendGenerator implements GeneratorInterface |
17
|
|
|
{ |
18
|
|
|
use GeneratorNameTrait; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var FrameworkComposer |
22
|
|
|
*/ |
23
|
|
|
protected $composer = null; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var Model |
27
|
|
|
*/ |
28
|
|
|
protected $model = null; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var GeneratedCollection |
32
|
|
|
*/ |
33
|
|
|
protected $collection; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* |
37
|
|
|
* @var string |
38
|
|
|
*/ |
39
|
|
|
protected $stubDir = __DIR__ . '/stubs'; |
40
|
|
|
|
41
|
|
|
public function __construct(FrameworkComposer $composer, Model $model) |
42
|
|
|
{ |
43
|
|
|
$this->composer = $composer; |
44
|
|
|
$this->model = $model; |
45
|
|
|
$this->setName($model->getName()); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function generate(): GeneratedCollection |
49
|
|
|
{ |
50
|
|
|
$this->collection = new GeneratedCollection(); |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @var FrameworkVue $vue |
54
|
|
|
*/ |
55
|
|
|
$vue = $this->composer->getByName('Vue'); |
56
|
|
|
// $blade = FrameworkComposer::getByName('Blade'); |
57
|
|
|
|
58
|
|
|
if ($vue !== null) { |
59
|
|
|
$this->makeVue($vue, 'Base', 'viewable'); |
60
|
|
|
$this->makeVue($vue, 'Card', 'viewable'); |
61
|
|
|
$this->makeVue($vue, 'List', 'viewable'); |
62
|
|
|
$this->makeVue($vue, 'Show', 'viewable'); |
63
|
|
|
$this->makeVue($vue, 'Form', 'editable'); |
64
|
|
|
$this->makeVueRoutes(); |
65
|
|
|
$this->makeVueIndex(); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
$this->makeGraphql(); |
69
|
|
|
|
70
|
|
|
return $this->collection; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
protected function makeVue(FrameworkVue $vue, string $component, string $mode): void |
74
|
|
|
{ |
75
|
|
|
$path = $this->model->getName() . '/' . |
76
|
|
|
$this->model->getName() . $component . '.vue'; |
77
|
|
|
$stub = file_get_contents($this->stubDir . "/Vue{$component}.stub.vue"); |
78
|
|
|
if ($mode == 'editable') { |
79
|
|
|
$vue->setEditableTemplate($this->template($stub)); |
80
|
|
|
$this->collection->push( |
81
|
|
|
new GeneratedItem( |
82
|
|
|
GeneratedItem::TYPE_FRONTEND, |
83
|
|
|
$this->model->editable($this->composer), |
84
|
|
|
$path |
85
|
|
|
) |
86
|
|
|
); |
87
|
|
|
} else { |
88
|
|
|
$vue->setViewableTemplate($this->template($stub)); |
89
|
|
|
$this->collection->push( |
90
|
|
|
new GeneratedItem( |
91
|
|
|
GeneratedItem::TYPE_FRONTEND, |
92
|
|
|
$this->model->viewable($this->composer, []), |
93
|
|
|
$path |
94
|
|
|
) |
95
|
|
|
); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
protected function makeGraphql(): void |
100
|
|
|
{ |
101
|
|
|
$listQuery = <<<EOF |
102
|
|
|
query (\$page: Int!) { |
103
|
|
|
{$this->lowerNamePlural}(page: \$page) { |
104
|
|
|
data { |
105
|
|
|
id |
106
|
|
|
TODO |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
paginatorInfo { |
110
|
|
|
currentPage |
111
|
|
|
perPage |
112
|
|
|
total |
113
|
|
|
lastPage |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
EOF; |
118
|
|
|
|
119
|
|
|
$this->collection->push( |
120
|
|
|
new GeneratedItem( |
121
|
|
|
GeneratedItem::TYPE_FRONTEND, |
122
|
|
|
$listQuery, |
123
|
|
|
$this->model->getName() . '/queryList.graphql' |
124
|
|
|
) |
125
|
|
|
); |
126
|
|
|
|
127
|
|
|
$itemQuery = <<<EOF |
128
|
|
|
query (\$id: ID!) { |
129
|
|
|
{$this->lowerName}(id: \$id) { |
130
|
|
|
id |
131
|
|
|
TODO |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
EOF; |
135
|
|
|
|
136
|
|
|
$this->collection->push( |
137
|
|
|
new GeneratedItem( |
138
|
|
|
GeneratedItem::TYPE_FRONTEND, |
139
|
|
|
$itemQuery, |
140
|
|
|
$this->model->getName() . '/queryItem.graphql' |
141
|
|
|
) |
142
|
|
|
); |
143
|
|
|
|
144
|
|
|
// TODO: variables |
145
|
|
|
$createMutation = <<<EOF |
146
|
|
|
mutation create(\$name: String!) { |
147
|
|
|
{$this->lowerName}Create(name: \$name) { |
148
|
|
|
TODO |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
EOF; |
152
|
|
|
$this->collection->push( |
153
|
|
|
new GeneratedItem( |
154
|
|
|
GeneratedItem::TYPE_FRONTEND, |
155
|
|
|
$createMutation, |
156
|
|
|
$this->model->getName() . '/mutationCreate.graphql' |
157
|
|
|
) |
158
|
|
|
); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
protected function makeVueIndex(): void |
162
|
|
|
{ |
163
|
|
|
$path = $this->model->getName() . '/index.js'; |
164
|
|
|
$name = $this->studlyName; |
165
|
|
|
|
166
|
|
|
$items = [ |
167
|
|
|
'Card', |
168
|
|
|
'Form', |
169
|
|
|
'List', |
170
|
|
|
'Show', |
171
|
|
|
]; |
172
|
|
|
|
173
|
|
|
$import = array_map( |
174
|
|
|
function ($i) use ($name) { |
175
|
|
|
return "import {$name}$i from './{$name}$i.vue';"; |
176
|
|
|
}, |
177
|
|
|
$items |
178
|
|
|
); |
179
|
|
|
|
180
|
|
|
$export = array_map( |
181
|
|
|
function ($i) use ($name) { |
182
|
|
|
return " {$name}$i,\n"; |
183
|
|
|
}, |
184
|
|
|
$items |
185
|
|
|
); |
186
|
|
|
|
187
|
|
|
$this->collection->push( |
188
|
|
|
new GeneratedItem( |
189
|
|
|
GeneratedItem::TYPE_FRONTEND, |
190
|
|
|
implode("\n", $import) . "\n" . |
191
|
|
|
"export {\n" . |
192
|
|
|
implode("\n", $export) . "\n};\n", |
193
|
|
|
$path |
194
|
|
|
) |
195
|
|
|
); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
protected function makeVueRoutes(): void |
199
|
|
|
{ |
200
|
|
|
$path = $this->model->getName() . '/routes.js'; |
201
|
|
|
$stub = file_get_contents($this->stubDir . "/routes.stub.js"); |
202
|
|
|
|
203
|
|
|
$this->collection->push( |
204
|
|
|
new GeneratedItem( |
205
|
|
|
GeneratedItem::TYPE_FRONTEND, |
206
|
|
|
$this->template($stub), |
207
|
|
|
$path |
208
|
|
|
) |
209
|
|
|
); |
210
|
|
|
} |
211
|
|
|
} |
212
|
|
|
|