1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
require(__DIR__ . '/../vendor/autoload.php'); |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Str; |
6
|
|
|
use LightnCandy\LightnCandy; |
7
|
|
|
|
8
|
|
|
use function Safe\file_put_contents; |
9
|
|
|
|
10
|
|
|
function createDirective(string $name, array $processors) |
11
|
|
|
{ |
12
|
|
|
$parameters = [ |
13
|
|
|
'name' => $name, |
14
|
|
|
'namespace' => 'Modelarium\\Laravel\\Lighthouse\\Directives', |
15
|
|
|
'studlyName' => Str::studly($name), |
16
|
|
|
'event' => in_array('event', $processors), |
17
|
|
|
'factory' => in_array('factory', $processors), |
18
|
|
|
'model' => in_array('model', $processors), |
19
|
|
|
'migration' => in_array('migration', $processors), |
20
|
|
|
'policy' => in_array('policy', $processors), |
21
|
|
|
'seed' => in_array('seed', $processors), |
22
|
|
|
'implements' => implode( |
23
|
|
|
', ', |
24
|
|
|
array_map( |
25
|
|
|
function ($i) { |
26
|
|
|
return Str::studly($i) . 'DirectiveInterface'; |
27
|
|
|
}, |
28
|
|
|
$processors |
29
|
|
|
) |
30
|
|
|
) |
31
|
|
|
]; |
32
|
|
|
$template = \Safe\file_get_contents(__DIR__ . '/directive.lighthouse.mustache'); |
33
|
|
|
$phpStr = LightnCandy::compile( |
34
|
|
|
$template, |
35
|
|
|
[ |
36
|
|
|
'flags' => LightnCandy::FLAG_ERROR_EXCEPTION |
37
|
|
|
] |
38
|
|
|
); |
39
|
|
|
if (!$phpStr) { |
40
|
|
|
throw new Exception('Invalid template'); |
41
|
|
|
} |
42
|
|
|
/** @var callable $renderer */ |
43
|
|
|
$renderer = LightnCandy::prepare($phpStr); |
|
|
|
|
44
|
|
|
file_put_contents(__DIR__ . '/../Modelarium/Laravel/Lighthouse/Directives/' . $parameters['studlyName'] . 'Directive.php', $renderer($parameters)); |
45
|
|
|
|
46
|
|
|
$template = \Safe\file_get_contents(__DIR__ . '/directive.mustache'); |
47
|
|
|
$phpStr = LightnCandy::compile( |
48
|
|
|
$template, |
49
|
|
|
[ |
50
|
|
|
'flags' => LightnCandy::FLAG_ERROR_EXCEPTION |
51
|
|
|
] |
52
|
|
|
); |
53
|
|
|
if (!$phpStr) { |
54
|
|
|
throw new Exception('Invalid template'); |
55
|
|
|
} |
56
|
|
|
/** @var callable $renderer */ |
57
|
|
|
$renderer = LightnCandy::prepare($phpStr); |
|
|
|
|
58
|
|
|
$renderer($parameters); |
59
|
|
|
file_put_contents(__DIR__ . '/../Modelarium/Laravel/Directives/' . $parameters['studlyName'] . 'Directive.php', $renderer($parameters)); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
// Script example.php |
63
|
|
|
$longopts = array( |
64
|
|
|
"name:", |
65
|
|
|
"processors:" |
66
|
|
|
); |
67
|
|
|
$options = getopt('', $longopts); |
68
|
|
|
createDirective($options['name'], explode(',', $options['processors'])); |
69
|
|
|
|