|
1
|
|
|
<?php namespace Limoncello\Application\Commands; |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Copyright 2015-2017 [email protected] |
|
5
|
|
|
* |
|
6
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
7
|
|
|
* you may not use this file except in compliance with the License. |
|
8
|
|
|
* You may obtain a copy of the License at |
|
9
|
|
|
* |
|
10
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
|
11
|
|
|
* |
|
12
|
|
|
* Unless required by applicable law or agreed to in writing, software |
|
13
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
|
14
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
15
|
|
|
* See the License for the specific language governing permissions and |
|
16
|
|
|
* limitations under the License. |
|
17
|
|
|
*/ |
|
18
|
|
|
|
|
19
|
|
|
use Limoncello\Application\Exceptions\InvalidArgumentException; |
|
20
|
|
|
use Limoncello\Contracts\Commands\CommandInterface; |
|
21
|
|
|
use Limoncello\Contracts\Commands\IoInterface; |
|
22
|
|
|
use Limoncello\Contracts\FileSystem\FileSystemInterface; |
|
23
|
|
|
use Limoncello\Contracts\Settings\Packages\AuthorizationSettingsInterface; |
|
24
|
|
|
use Limoncello\Contracts\Settings\Packages\DataSettingsInterface; |
|
25
|
|
|
use Limoncello\Contracts\Settings\Packages\FluteSettingsInterface; |
|
26
|
|
|
use Limoncello\Contracts\Settings\SettingsProviderInterface; |
|
27
|
|
|
use Psr\Container\ContainerExceptionInterface; |
|
28
|
|
|
use Psr\Container\ContainerInterface; |
|
29
|
|
|
use Psr\Container\NotFoundExceptionInterface; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @package Limoncello\Application |
|
33
|
|
|
*/ |
|
34
|
|
|
class MakeCommand implements CommandInterface |
|
35
|
|
|
{ |
|
36
|
|
|
/** |
|
37
|
|
|
* Command name. |
|
38
|
|
|
*/ |
|
39
|
|
|
const NAME = 'l:make'; |
|
40
|
|
|
|
|
41
|
|
|
/** Argument name */ |
|
42
|
|
|
const ARG_ITEM = 'item'; |
|
43
|
|
|
|
|
44
|
|
|
/** Argument name */ |
|
45
|
|
|
const ARG_SINGULAR = 'singular'; |
|
46
|
|
|
|
|
47
|
|
|
/** Argument name */ |
|
48
|
|
|
const ARG_PLURAL = 'plural'; |
|
49
|
|
|
|
|
50
|
|
|
/** Command action */ |
|
51
|
|
|
const ITEM_MIGRATE = 'migrate'; |
|
52
|
|
|
|
|
53
|
|
|
/** Command action */ |
|
54
|
|
|
const ITEM_SEED = 'seed'; |
|
55
|
|
|
|
|
56
|
|
|
/** Command action */ |
|
57
|
|
|
const ITEM_CONTROLLER = 'controller'; |
|
58
|
|
|
|
|
59
|
|
|
/** Command action */ |
|
60
|
|
|
const ITEM_JSONAPI = 'jsonapi'; |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Taken from http://php.net/manual/en/language.oop5.basic.php |
|
64
|
|
|
*/ |
|
65
|
|
|
protected const VALID_CLASS_NAME_REGEX = '/^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*$/'; |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @inheritdoc |
|
69
|
|
|
*/ |
|
70
|
1 |
|
public static function getName(): string |
|
71
|
|
|
{ |
|
72
|
1 |
|
return static::NAME; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @inheritdoc |
|
77
|
|
|
*/ |
|
78
|
1 |
|
public static function getDescription(): string |
|
79
|
|
|
{ |
|
80
|
1 |
|
return 'Creates necessary classes for models, migrations and data seeds.'; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* @inheritdoc |
|
85
|
|
|
*/ |
|
86
|
1 |
|
public static function getHelp(): string |
|
87
|
|
|
{ |
|
88
|
1 |
|
return 'This command creates necessary classes for models, migrations and data seeds.'; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* @inheritdoc |
|
93
|
|
|
*/ |
|
94
|
1 |
|
public static function getArguments(): array |
|
95
|
|
|
{ |
|
96
|
1 |
|
$migrate = static::ITEM_MIGRATE; |
|
97
|
1 |
|
$seed = static::ITEM_SEED; |
|
98
|
1 |
|
$jsonapi = static::ITEM_JSONAPI; |
|
99
|
|
|
|
|
100
|
|
|
return [ |
|
101
|
|
|
[ |
|
102
|
1 |
|
static::ARGUMENT_NAME => static::ARG_ITEM, |
|
103
|
1 |
|
static::ARGUMENT_DESCRIPTION => "Action such as `$migrate`, `$seed` or `$jsonapi`.", |
|
104
|
1 |
|
static::ARGUMENT_MODE => static::ARGUMENT_MODE__REQUIRED, |
|
105
|
|
|
], |
|
106
|
|
|
[ |
|
107
|
1 |
|
static::ARGUMENT_NAME => static::ARG_SINGULAR, |
|
108
|
1 |
|
static::ARGUMENT_DESCRIPTION => 'Singular name in camel case (e.g. `Post`).', |
|
109
|
1 |
|
static::ARGUMENT_MODE => static::ARGUMENT_MODE__REQUIRED, |
|
110
|
|
|
], |
|
111
|
|
|
[ |
|
112
|
1 |
|
static::ARGUMENT_NAME => static::ARG_PLURAL, |
|
113
|
1 |
|
static::ARGUMENT_DESCRIPTION => 'Plural name in camel case (e.g. `Posts`).', |
|
114
|
1 |
|
static::ARGUMENT_MODE => static::ARGUMENT_MODE__REQUIRED, |
|
115
|
|
|
], |
|
116
|
|
|
]; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* @inheritdoc |
|
121
|
|
|
*/ |
|
122
|
1 |
|
public static function getOptions(): array |
|
123
|
|
|
{ |
|
124
|
1 |
|
return []; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* @inheritdoc |
|
129
|
|
|
*/ |
|
130
|
7 |
|
public static function execute(ContainerInterface $container, IoInterface $inOut): void |
|
131
|
|
|
{ |
|
132
|
7 |
|
(new static())->run($container, $inOut); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* @param ContainerInterface $container |
|
137
|
|
|
* @param IoInterface $inOut |
|
138
|
|
|
* |
|
139
|
|
|
* @return void |
|
140
|
|
|
* |
|
141
|
|
|
* @throws ContainerExceptionInterface |
|
142
|
|
|
* @throws NotFoundExceptionInterface |
|
143
|
|
|
*/ |
|
144
|
7 |
|
protected function run(ContainerInterface $container, IoInterface $inOut): void |
|
145
|
|
|
{ |
|
146
|
7 |
|
$item = $inOut->getArguments()[static::ARG_ITEM]; |
|
147
|
7 |
|
$singular = $inOut->getArguments()[static::ARG_SINGULAR]; |
|
148
|
7 |
|
$plural = $inOut->getArguments()[static::ARG_PLURAL]; |
|
149
|
|
|
|
|
150
|
7 |
|
if ($this->isValidShortClassName($singular) === false) { |
|
151
|
1 |
|
throw new InvalidArgumentException("`$singular` is not a valid class name."); |
|
152
|
|
|
} |
|
153
|
6 |
|
if ($this->isValidShortClassName($plural) === false) { |
|
154
|
1 |
|
throw new InvalidArgumentException("`$plural` is not a valid class name."); |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
switch ($item) { |
|
158
|
5 |
|
case static::ITEM_MIGRATE: |
|
159
|
1 |
|
$this->createTemplates($this->getFileSystem($container), [ |
|
160
|
1 |
|
$this->composeMigrationParameters($container, $singular, $plural), |
|
161
|
|
|
]); |
|
162
|
1 |
|
break; |
|
163
|
4 |
|
case static::ITEM_SEED: |
|
164
|
1 |
|
$this->createTemplates($this->getFileSystem($container), [ |
|
165
|
1 |
|
$this->composeSeedParameters($container, $singular, $plural), |
|
166
|
|
|
]); |
|
167
|
1 |
|
break; |
|
168
|
3 |
|
case static::ITEM_CONTROLLER: |
|
169
|
1 |
|
$this->createTemplates($this->getFileSystem($container), [ |
|
170
|
1 |
|
$this->composeWebControllerParameters($container, $singular, $plural), |
|
171
|
1 |
|
$this->composeWebRouteParameters($container, $singular, $plural), |
|
172
|
|
|
]); |
|
173
|
1 |
|
break; |
|
174
|
2 |
|
case static::ITEM_JSONAPI: |
|
175
|
1 |
|
$this->createTemplates($this->getFileSystem($container), [ |
|
176
|
1 |
|
$this->composeMigrationParameters($container, $singular, $plural), |
|
177
|
1 |
|
$this->composeSeedParameters($container, $singular, $plural), |
|
178
|
1 |
|
$this->composeModelParameters($container, $singular, $plural), |
|
179
|
1 |
|
$this->composeSchemaParameters($container, $singular, $plural), |
|
180
|
1 |
|
$this->composeApiParameters($container, $singular, $plural), |
|
181
|
1 |
|
$this->composeAuthorizationParameters($container, $singular, $plural), |
|
182
|
1 |
|
$this->composeValidationRulesParameters($container, $singular, $plural), |
|
183
|
1 |
|
$this->composeValidationOnCreateRuleSetsParameters($container, $singular, $plural), |
|
184
|
1 |
|
$this->composeValidationOnUpdateRuleSetsParameters($container, $singular, $plural), |
|
185
|
1 |
|
$this->composeJsonControllerParameters($container, $singular, $plural), |
|
186
|
1 |
|
$this->composeJsonRouteParameters($container, $singular, $plural), |
|
187
|
|
|
]); |
|
188
|
1 |
|
break; |
|
189
|
|
|
default: |
|
190
|
1 |
|
$inOut->writeError("Unsupported item type `$item`." . PHP_EOL); |
|
191
|
1 |
|
break; |
|
192
|
|
|
} |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
/** |
|
196
|
|
|
* @param ContainerInterface $container |
|
197
|
|
|
* @param string $singular |
|
198
|
|
|
* @param string $plural |
|
199
|
|
|
* |
|
200
|
|
|
* @return array |
|
201
|
|
|
* @throws ContainerExceptionInterface |
|
202
|
|
|
* @throws NotFoundExceptionInterface |
|
203
|
|
|
*/ |
|
204
|
2 |
|
private function composeMigrationParameters(ContainerInterface $container, string $singular, string $plural): array |
|
205
|
|
|
{ |
|
206
|
2 |
|
$outputPath = $this->getDataSettings($container)[DataSettingsInterface::KEY_MIGRATIONS_FOLDER] |
|
207
|
2 |
|
. DIRECTORY_SEPARATOR . $plural . 'Migration.php'; |
|
208
|
|
|
$parameters = [ |
|
209
|
2 |
|
'{%SINGULAR_CC%}' => $singular, |
|
210
|
2 |
|
'{%PLURAL_CC%}' => $plural, |
|
211
|
|
|
]; |
|
212
|
|
|
|
|
213
|
2 |
|
return [$outputPath, $this->getTemplatePath('Migration.txt'), $parameters]; |
|
214
|
|
|
} |
|
215
|
|
|
|
|
216
|
|
|
/** |
|
217
|
|
|
* @param ContainerInterface $container |
|
218
|
|
|
* @param string $singular |
|
219
|
|
|
* @param string $plural |
|
220
|
|
|
* |
|
221
|
|
|
* @return array |
|
222
|
|
|
* @throws ContainerExceptionInterface |
|
223
|
|
|
* @throws NotFoundExceptionInterface |
|
224
|
|
|
*/ |
|
225
|
2 |
|
private function composeSeedParameters(ContainerInterface $container, string $singular, string $plural): array |
|
226
|
|
|
{ |
|
227
|
2 |
|
$outputPath = $this->getDataSettings($container)[DataSettingsInterface::KEY_SEEDS_FOLDER] |
|
228
|
2 |
|
. DIRECTORY_SEPARATOR . $plural . 'Seed.php'; |
|
229
|
|
|
$parameters = [ |
|
230
|
2 |
|
'{%SINGULAR_CC%}' => $singular, |
|
231
|
2 |
|
'{%PLURAL_CC%}' => $plural, |
|
232
|
|
|
]; |
|
233
|
|
|
|
|
234
|
2 |
|
return [$outputPath, $this->getTemplatePath('Seed.txt'), $parameters]; |
|
235
|
|
|
} |
|
236
|
|
|
|
|
237
|
|
|
/** |
|
238
|
|
|
* @param ContainerInterface $container |
|
239
|
|
|
* @param string $singular |
|
240
|
|
|
* @param string $plural |
|
241
|
|
|
* |
|
242
|
|
|
* @return array |
|
243
|
|
|
* @throws ContainerExceptionInterface |
|
244
|
|
|
* @throws NotFoundExceptionInterface |
|
245
|
|
|
*/ |
|
246
|
1 |
|
private function composeModelParameters(ContainerInterface $container, string $singular, string $plural): array |
|
247
|
|
|
{ |
|
248
|
1 |
|
$outputPath = $this->getDataSettings($container)[DataSettingsInterface::KEY_MODELS_FOLDER] |
|
249
|
1 |
|
. DIRECTORY_SEPARATOR . $singular . '.php'; |
|
250
|
|
|
$parameters = [ |
|
251
|
1 |
|
'{%SINGULAR_CC%}' => $singular, |
|
252
|
1 |
|
'{%SINGULAR_LC%}' => strtolower($singular), |
|
253
|
1 |
|
'{%SINGULAR_UC%}' => strtoupper($singular), |
|
254
|
1 |
|
'{%PLURAL_LC%}' => strtolower($plural), |
|
255
|
1 |
|
'{%PLURAL_UC%}' => strtoupper($plural), |
|
256
|
|
|
]; |
|
257
|
|
|
|
|
258
|
1 |
|
return [$outputPath, $this->getTemplatePath('Model.txt'), $parameters]; |
|
259
|
|
|
} |
|
260
|
|
|
|
|
261
|
|
|
/** |
|
262
|
|
|
* @param ContainerInterface $container |
|
263
|
|
|
* @param string $singular |
|
264
|
|
|
* @param string $plural |
|
265
|
|
|
* |
|
266
|
|
|
* @return array |
|
267
|
|
|
* @throws ContainerExceptionInterface |
|
268
|
|
|
* @throws NotFoundExceptionInterface |
|
269
|
|
|
*/ |
|
270
|
1 |
|
private function composeSchemaParameters(ContainerInterface $container, string $singular, string $plural): array |
|
271
|
|
|
{ |
|
272
|
1 |
|
$outputPath = $this->getFluteSettings($container)[FluteSettingsInterface::KEY_SCHEMAS_FOLDER] |
|
273
|
1 |
|
. DIRECTORY_SEPARATOR . $singular . 'Schema.php'; |
|
274
|
|
|
$parameters = [ |
|
275
|
1 |
|
'{%SINGULAR_CC%}' => $singular, |
|
276
|
1 |
|
'{%PLURAL_LC%}' => strtolower($plural), |
|
277
|
|
|
]; |
|
278
|
|
|
|
|
279
|
1 |
|
return [$outputPath, $this->getTemplatePath('Schema.txt'), $parameters]; |
|
280
|
|
|
} |
|
281
|
|
|
|
|
282
|
|
|
/** |
|
283
|
|
|
* @param ContainerInterface $container |
|
284
|
|
|
* @param string $singular |
|
285
|
|
|
* @param string $plural |
|
286
|
|
|
* |
|
287
|
|
|
* @return array |
|
288
|
|
|
* @throws ContainerExceptionInterface |
|
289
|
|
|
* @throws NotFoundExceptionInterface |
|
290
|
|
|
*/ |
|
291
|
1 |
|
private function composeApiParameters(ContainerInterface $container, string $singular, string $plural): array |
|
292
|
|
|
{ |
|
293
|
1 |
|
$outputPath = $this->getFluteSettings($container)[FluteSettingsInterface::KEY_API_FOLDER] |
|
294
|
1 |
|
. DIRECTORY_SEPARATOR . $plural . 'Api.php'; |
|
295
|
|
|
$parameters = [ |
|
296
|
1 |
|
'{%SINGULAR_CC%}' => $singular, |
|
297
|
1 |
|
'{%PLURAL_CC%}' => $plural, |
|
298
|
1 |
|
'{%SINGULAR_UC%}' => strtoupper($singular), |
|
299
|
1 |
|
'{%PLURAL_UC%}' => strtoupper($plural), |
|
300
|
|
|
]; |
|
301
|
|
|
|
|
302
|
1 |
|
return [$outputPath, $this->getTemplatePath('Api.txt'), $parameters]; |
|
303
|
|
|
} |
|
304
|
|
|
|
|
305
|
|
|
/** |
|
306
|
|
|
* @param ContainerInterface $container |
|
307
|
|
|
* @param string $singular |
|
308
|
|
|
* @param string $plural |
|
309
|
|
|
* |
|
310
|
|
|
* @return array |
|
311
|
|
|
* @throws ContainerExceptionInterface |
|
312
|
|
|
* @throws NotFoundExceptionInterface |
|
313
|
|
|
*/ |
|
314
|
1 |
|
private function composeAuthorizationParameters( |
|
315
|
|
|
ContainerInterface $container, |
|
316
|
|
|
string $singular, |
|
317
|
|
|
string $plural |
|
318
|
|
|
): array { |
|
319
|
1 |
|
$outputPath = $this->getAuthorizationSettings($container)[AuthorizationSettingsInterface::KEY_POLICIES_FOLDER] |
|
320
|
1 |
|
. DIRECTORY_SEPARATOR . $singular . 'Rules.php'; |
|
321
|
|
|
$parameters = [ |
|
322
|
1 |
|
'{%SINGULAR_CC%}' => $singular, |
|
323
|
1 |
|
'{%PLURAL_CC%}' => $plural, |
|
324
|
1 |
|
'{%SINGULAR_LC%}' => strtolower($singular), |
|
325
|
1 |
|
'{%PLURAL_UC%}' => strtoupper($plural), |
|
326
|
1 |
|
'{%SINGULAR_UC%}' => strtoupper($singular), |
|
327
|
|
|
]; |
|
328
|
|
|
|
|
329
|
1 |
|
return [$outputPath, $this->getTemplatePath('ApiAuthorization.txt'), $parameters]; |
|
330
|
|
|
} |
|
331
|
|
|
|
|
332
|
|
|
/** |
|
333
|
|
|
* @param ContainerInterface $container |
|
334
|
|
|
* @param string $singular |
|
335
|
|
|
* @param string $plural |
|
336
|
|
|
* |
|
337
|
|
|
* @return array |
|
338
|
|
|
* @throws ContainerExceptionInterface |
|
339
|
|
|
* @throws NotFoundExceptionInterface |
|
340
|
|
|
*/ |
|
341
|
1 |
|
private function composeValidationRulesParameters( |
|
342
|
|
|
ContainerInterface $container, |
|
343
|
|
|
string $singular, |
|
344
|
|
|
string $plural |
|
345
|
|
|
): array { |
|
346
|
1 |
|
$outputPath = $this->getFluteSettings($container)[FluteSettingsInterface::KEY_JSON_VALIDATION_RULES_FOLDER] |
|
347
|
1 |
|
. DIRECTORY_SEPARATOR . $singular . 'Rules.php'; |
|
348
|
|
|
$parameters = [ |
|
349
|
1 |
|
'{%SINGULAR_CC%}' => $singular, |
|
350
|
1 |
|
'{%SINGULAR_LC%}' => strtolower($singular), |
|
351
|
1 |
|
'{%PLURAL_LC%}' => strtolower($plural), |
|
352
|
|
|
]; |
|
353
|
|
|
|
|
354
|
1 |
|
return [$outputPath, $this->getTemplatePath('ValidationRules.txt'), $parameters]; |
|
355
|
|
|
} |
|
356
|
|
|
|
|
357
|
|
|
/** |
|
358
|
|
|
* @param ContainerInterface $container |
|
359
|
|
|
* @param string $singular |
|
360
|
|
|
* @param string $plural |
|
361
|
|
|
* |
|
362
|
|
|
* @return array |
|
363
|
|
|
* @throws ContainerExceptionInterface |
|
364
|
|
|
* @throws NotFoundExceptionInterface |
|
365
|
|
|
*/ |
|
366
|
1 |
|
private function composeValidationOnCreateRuleSetsParameters( |
|
367
|
|
|
ContainerInterface $container, |
|
368
|
|
|
string $singular, |
|
369
|
|
|
string $plural |
|
|
|
|
|
|
370
|
|
|
): array { |
|
371
|
1 |
|
$folder = $this->filterOutFolderMask( |
|
372
|
1 |
|
$this->getFluteSettings($container)[FluteSettingsInterface::KEY_JSON_VALIDATORS_FOLDER] |
|
373
|
|
|
); |
|
374
|
1 |
|
$outputPath = $folder . DIRECTORY_SEPARATOR . $singular . 'Create.php'; |
|
375
|
|
|
$parameters = [ |
|
376
|
1 |
|
'{%SINGULAR_CC%}' => $singular, |
|
377
|
1 |
|
'{%SINGULAR_LC%}' => strtolower($singular), |
|
378
|
|
|
]; |
|
379
|
|
|
|
|
380
|
1 |
|
return [$outputPath, $this->getTemplatePath('JsonRuleSetOnCreate.txt'), $parameters]; |
|
381
|
|
|
} |
|
382
|
|
|
|
|
383
|
|
|
/** |
|
384
|
|
|
* @param ContainerInterface $container |
|
385
|
|
|
* @param string $singular |
|
386
|
|
|
* @param string $plural |
|
387
|
|
|
* |
|
388
|
|
|
* @return array |
|
389
|
|
|
* @throws ContainerExceptionInterface |
|
390
|
|
|
* @throws NotFoundExceptionInterface |
|
391
|
|
|
*/ |
|
392
|
1 |
|
private function composeValidationOnUpdateRuleSetsParameters( |
|
393
|
|
|
ContainerInterface $container, |
|
394
|
|
|
string $singular, |
|
395
|
|
|
string $plural |
|
|
|
|
|
|
396
|
|
|
): array { |
|
397
|
1 |
|
$folder = $this->filterOutFolderMask( |
|
398
|
1 |
|
$this->getFluteSettings($container)[FluteSettingsInterface::KEY_JSON_VALIDATORS_FOLDER] |
|
399
|
|
|
); |
|
400
|
1 |
|
$outputPath = $folder . DIRECTORY_SEPARATOR . $singular . 'Update.php'; |
|
401
|
|
|
$parameters = [ |
|
402
|
1 |
|
'{%SINGULAR_CC%}' => $singular, |
|
403
|
1 |
|
'{%SINGULAR_LC%}' => strtolower($singular), |
|
404
|
|
|
]; |
|
405
|
|
|
|
|
406
|
1 |
|
return [$outputPath, $this->getTemplatePath('JsonRuleSetOnUpdate.txt'), $parameters]; |
|
407
|
|
|
} |
|
408
|
|
|
|
|
409
|
|
|
/** |
|
410
|
|
|
* @param ContainerInterface $container |
|
411
|
|
|
* @param string $singular |
|
412
|
|
|
* @param string $plural |
|
413
|
|
|
* |
|
414
|
|
|
* @return array |
|
415
|
|
|
* @throws ContainerExceptionInterface |
|
416
|
|
|
* @throws NotFoundExceptionInterface |
|
417
|
|
|
*/ |
|
418
|
1 |
|
private function composeJsonControllerParameters( |
|
419
|
|
|
ContainerInterface $container, |
|
420
|
|
|
string $singular, |
|
421
|
|
|
string $plural |
|
422
|
|
|
): array { |
|
423
|
1 |
|
$folder = $this->filterOutFolderMask( |
|
424
|
1 |
|
$this->getFluteSettings($container)[FluteSettingsInterface::KEY_JSON_CONTROLLERS_FOLDER] |
|
425
|
|
|
); |
|
426
|
1 |
|
$outputPath = $folder . DIRECTORY_SEPARATOR . $plural . 'Controller.php'; |
|
427
|
|
|
$parameters = [ |
|
428
|
1 |
|
'{%SINGULAR_CC%}' => $singular, |
|
429
|
1 |
|
'{%PLURAL_CC%}' => $plural, |
|
430
|
|
|
]; |
|
431
|
|
|
|
|
432
|
1 |
|
return [$outputPath, $this->getTemplatePath('JsonController.txt'), $parameters]; |
|
433
|
|
|
} |
|
434
|
|
|
|
|
435
|
|
|
/** |
|
436
|
|
|
* @param ContainerInterface $container |
|
437
|
|
|
* @param string $singular |
|
438
|
|
|
* @param string $plural |
|
439
|
|
|
* |
|
440
|
|
|
* @return array |
|
441
|
|
|
* @throws ContainerExceptionInterface |
|
442
|
|
|
* @throws NotFoundExceptionInterface |
|
443
|
|
|
*/ |
|
444
|
1 |
|
private function composeJsonRouteParameters( |
|
445
|
|
|
ContainerInterface $container, |
|
446
|
|
|
string $singular, |
|
447
|
|
|
string $plural |
|
448
|
|
|
): array { |
|
449
|
1 |
|
$folder = $this->filterOutFolderMask( |
|
450
|
1 |
|
$this->getFluteSettings($container)[FluteSettingsInterface::KEY_ROUTES_FOLDER] |
|
451
|
|
|
); |
|
452
|
1 |
|
$outputPath = $folder . DIRECTORY_SEPARATOR . $singular . 'ApiRoutes.php'; |
|
453
|
|
|
$parameters = [ |
|
454
|
1 |
|
'{%SINGULAR_CC%}' => $singular, |
|
455
|
1 |
|
'{%PLURAL_CC%}' => $plural, |
|
456
|
|
|
]; |
|
457
|
|
|
|
|
458
|
1 |
|
return [$outputPath, $this->getTemplatePath('JsonRoutes.txt'), $parameters]; |
|
459
|
|
|
} |
|
460
|
|
|
|
|
461
|
|
|
/** |
|
462
|
|
|
* @param ContainerInterface $container |
|
463
|
|
|
* @param string $singular |
|
464
|
|
|
* @param string $plural |
|
465
|
|
|
* |
|
466
|
|
|
* @return array |
|
467
|
|
|
* @throws ContainerExceptionInterface |
|
468
|
|
|
* @throws NotFoundExceptionInterface |
|
469
|
|
|
*/ |
|
470
|
1 |
|
private function composeWebControllerParameters( |
|
471
|
|
|
ContainerInterface $container, |
|
472
|
|
|
string $singular, |
|
473
|
|
|
string $plural |
|
474
|
|
|
): array { |
|
475
|
1 |
|
$folder = $this->filterOutFolderMask( |
|
476
|
1 |
|
$this->getFluteSettings($container)[FluteSettingsInterface::KEY_WEB_CONTROLLERS_FOLDER] |
|
477
|
|
|
); |
|
478
|
1 |
|
$outputPath = $folder . DIRECTORY_SEPARATOR . $plural . 'Controller.php'; |
|
479
|
|
|
$parameters = [ |
|
480
|
1 |
|
'{%SINGULAR_CC%}' => $singular, |
|
481
|
1 |
|
'{%PLURAL_CC%}' => $plural, |
|
482
|
1 |
|
'{%PLURAL_LC%}' => strtolower($plural), |
|
483
|
|
|
]; |
|
484
|
|
|
|
|
485
|
1 |
|
return [$outputPath, $this->getTemplatePath('WebController.txt'), $parameters]; |
|
486
|
|
|
} |
|
487
|
|
|
|
|
488
|
|
|
/** |
|
489
|
|
|
* @param ContainerInterface $container |
|
490
|
|
|
* @param string $singular |
|
491
|
|
|
* @param string $plural |
|
492
|
|
|
* |
|
493
|
|
|
* @return array |
|
494
|
|
|
* @throws ContainerExceptionInterface |
|
495
|
|
|
* @throws NotFoundExceptionInterface |
|
496
|
|
|
*/ |
|
497
|
1 |
|
private function composeWebRouteParameters( |
|
498
|
|
|
ContainerInterface $container, |
|
499
|
|
|
string $singular, |
|
500
|
|
|
string $plural |
|
501
|
|
|
): array { |
|
502
|
1 |
|
$folder = $this->filterOutFolderMask( |
|
503
|
1 |
|
$this->getFluteSettings($container)[FluteSettingsInterface::KEY_ROUTES_FOLDER] |
|
504
|
|
|
); |
|
505
|
1 |
|
$outputPath = $folder . DIRECTORY_SEPARATOR . $singular . 'WebRoutes.php'; |
|
506
|
|
|
$parameters = [ |
|
507
|
1 |
|
'{%SINGULAR_CC%}' => $singular, |
|
508
|
1 |
|
'{%PLURAL_CC%}' => $plural, |
|
509
|
|
|
]; |
|
510
|
|
|
|
|
511
|
1 |
|
return [$outputPath, $this->getTemplatePath('WebRoutes.txt'), $parameters]; |
|
512
|
|
|
} |
|
513
|
|
|
|
|
514
|
|
|
/** |
|
515
|
|
|
* @param FileSystemInterface $fileSystem |
|
516
|
|
|
* @param array $pathsAndParams |
|
517
|
|
|
* |
|
518
|
|
|
* @return void |
|
519
|
|
|
*/ |
|
520
|
4 |
|
private function createTemplates(FileSystemInterface $fileSystem, array $pathsAndParams): void |
|
521
|
|
|
{ |
|
522
|
4 |
|
foreach ($pathsAndParams as list($outputPath)) { |
|
523
|
4 |
|
if ($fileSystem->exists($outputPath) === true) { |
|
524
|
4 |
|
throw new InvalidArgumentException("File `$outputPath` already exists."); |
|
525
|
|
|
} |
|
526
|
|
|
} |
|
527
|
|
|
|
|
528
|
4 |
|
foreach ($pathsAndParams as list($outputPath, $templatePath, $parameters)) { |
|
529
|
4 |
|
$this->writeByTemplate($fileSystem, $outputPath, $templatePath, $parameters); |
|
530
|
|
|
} |
|
531
|
|
|
} |
|
532
|
|
|
|
|
533
|
|
|
/** |
|
534
|
|
|
* @param FileSystemInterface $fileSystem |
|
535
|
|
|
* @param string $outputPath |
|
536
|
|
|
* @param string $templatePath |
|
537
|
|
|
* @param iterable $parameters |
|
538
|
|
|
* |
|
539
|
|
|
* @return void |
|
540
|
|
|
*/ |
|
541
|
4 |
|
private function writeByTemplate( |
|
542
|
|
|
FileSystemInterface $fileSystem, |
|
543
|
|
|
string $outputPath, |
|
544
|
|
|
string $templatePath, |
|
545
|
|
|
iterable $parameters |
|
546
|
|
|
): void { |
|
547
|
4 |
|
$templateContent = $fileSystem->read($templatePath); |
|
548
|
4 |
|
$outputContent = $this->replaceInTemplate($templateContent, $parameters); |
|
549
|
4 |
|
$fileSystem->write($outputPath, $outputContent); |
|
550
|
|
|
} |
|
551
|
|
|
|
|
552
|
|
|
/** |
|
553
|
|
|
* @param string $template |
|
554
|
|
|
* @param iterable $parameters |
|
555
|
|
|
* |
|
556
|
|
|
* @return string |
|
557
|
|
|
*/ |
|
558
|
4 |
|
private function replaceInTemplate(string $template, iterable $parameters): string |
|
559
|
|
|
{ |
|
560
|
4 |
|
$result = $template; |
|
561
|
4 |
|
foreach ($parameters as $key => $value) { |
|
562
|
4 |
|
$result = str_replace($key, $value, $result); |
|
563
|
|
|
} |
|
564
|
|
|
|
|
565
|
4 |
|
return $result; |
|
566
|
|
|
} |
|
567
|
|
|
|
|
568
|
|
|
/** |
|
569
|
|
|
* @param ContainerInterface $container |
|
570
|
|
|
* |
|
571
|
|
|
* @return FileSystemInterface |
|
572
|
|
|
* |
|
573
|
|
|
* @throws ContainerExceptionInterface |
|
574
|
|
|
* @throws NotFoundExceptionInterface |
|
575
|
|
|
*/ |
|
576
|
4 |
|
private function getFileSystem(ContainerInterface $container): FileSystemInterface |
|
577
|
|
|
{ |
|
578
|
4 |
|
assert($container->has(FileSystemInterface::class)); |
|
579
|
|
|
|
|
580
|
|
|
/** @var FileSystemInterface $fileSystem */ |
|
581
|
4 |
|
$fileSystem = $container->get(FileSystemInterface::class); |
|
582
|
|
|
|
|
583
|
4 |
|
return $fileSystem; |
|
584
|
|
|
} |
|
585
|
|
|
|
|
586
|
|
|
/** |
|
587
|
|
|
* @param ContainerInterface $container |
|
588
|
|
|
* |
|
589
|
|
|
* @return SettingsProviderInterface |
|
590
|
|
|
* @throws ContainerExceptionInterface |
|
591
|
|
|
* @throws NotFoundExceptionInterface |
|
592
|
|
|
*/ |
|
593
|
4 |
|
private function getSettingsProvider(ContainerInterface $container): SettingsProviderInterface |
|
594
|
|
|
{ |
|
595
|
4 |
|
assert($container->has(SettingsProviderInterface::class)); |
|
596
|
|
|
|
|
597
|
|
|
/** @var SettingsProviderInterface $provider */ |
|
598
|
4 |
|
$provider = $container->get(SettingsProviderInterface::class); |
|
599
|
|
|
|
|
600
|
4 |
|
return $provider; |
|
601
|
|
|
} |
|
602
|
|
|
|
|
603
|
|
|
/** |
|
604
|
|
|
* @param string $name |
|
605
|
|
|
* |
|
606
|
|
|
* @return bool |
|
607
|
|
|
*/ |
|
608
|
7 |
|
private function isValidShortClassName(string $name): bool |
|
609
|
|
|
{ |
|
610
|
7 |
|
return empty($name) === false && preg_match(static::VALID_CLASS_NAME_REGEX, $name) === 1; |
|
611
|
|
|
} |
|
612
|
|
|
|
|
613
|
|
|
/** |
|
614
|
|
|
* @param string $fileName |
|
615
|
|
|
* |
|
616
|
|
|
* @return string |
|
617
|
|
|
*/ |
|
618
|
4 |
|
private function getTemplatePath(string $fileName): string |
|
619
|
|
|
{ |
|
620
|
4 |
|
return implode(DIRECTORY_SEPARATOR, [__DIR__, '..', '..', 'res', 'CodeTemplates', $fileName]); |
|
621
|
|
|
} |
|
622
|
|
|
|
|
623
|
|
|
/** |
|
624
|
|
|
* @param ContainerInterface $container |
|
625
|
|
|
* |
|
626
|
|
|
* @return array |
|
627
|
|
|
* |
|
628
|
|
|
* @throws ContainerExceptionInterface |
|
629
|
|
|
* @throws NotFoundExceptionInterface |
|
630
|
|
|
*/ |
|
631
|
3 |
|
private function getDataSettings(ContainerInterface $container): array |
|
632
|
|
|
{ |
|
633
|
3 |
|
$dataSettings = $this->getSettingsProvider($container)->get(DataSettingsInterface::class); |
|
634
|
|
|
|
|
635
|
3 |
|
return $dataSettings; |
|
636
|
|
|
} |
|
637
|
|
|
|
|
638
|
|
|
/** |
|
639
|
|
|
* @param ContainerInterface $container |
|
640
|
|
|
* |
|
641
|
|
|
* @return array |
|
642
|
|
|
* |
|
643
|
|
|
* @throws ContainerExceptionInterface |
|
644
|
|
|
* @throws NotFoundExceptionInterface |
|
645
|
|
|
*/ |
|
646
|
2 |
|
private function getFluteSettings(ContainerInterface $container): array |
|
647
|
|
|
{ |
|
648
|
2 |
|
$dataSettings = $this->getSettingsProvider($container)->get(FluteSettingsInterface::class); |
|
649
|
|
|
|
|
650
|
2 |
|
return $dataSettings; |
|
651
|
|
|
} |
|
652
|
|
|
|
|
653
|
|
|
/** |
|
654
|
|
|
* @param ContainerInterface $container |
|
655
|
|
|
* |
|
656
|
|
|
* @return array |
|
657
|
|
|
* |
|
658
|
|
|
* @throws ContainerExceptionInterface |
|
659
|
|
|
* @throws NotFoundExceptionInterface |
|
660
|
|
|
*/ |
|
661
|
1 |
|
private function getAuthorizationSettings(ContainerInterface $container): array |
|
662
|
|
|
{ |
|
663
|
1 |
|
$dataSettings = $this->getSettingsProvider($container)->get(AuthorizationSettingsInterface::class); |
|
664
|
|
|
|
|
665
|
1 |
|
return $dataSettings; |
|
666
|
|
|
} |
|
667
|
|
|
|
|
668
|
|
|
/** |
|
669
|
|
|
* Folder paths might include masks such as `**`. This function tries to filter them out. |
|
670
|
|
|
* |
|
671
|
|
|
* @param string $folder |
|
672
|
|
|
* |
|
673
|
|
|
* @return string |
|
674
|
|
|
*/ |
|
675
|
2 |
|
private function filterOutFolderMask(string $folder): string |
|
676
|
|
|
{ |
|
677
|
2 |
|
$mask = '**'; |
|
678
|
|
|
|
|
679
|
2 |
|
$folder = str_replace($mask . DIRECTORY_SEPARATOR, '', $folder); |
|
680
|
2 |
|
$folder = str_replace($mask, '', $folder); |
|
681
|
|
|
|
|
682
|
2 |
|
return $folder; |
|
683
|
|
|
} |
|
684
|
|
|
} |
|
685
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.