|
1
|
|
|
<?php |
|
2
|
|
|
namespace keeko\tools\command; |
|
3
|
|
|
|
|
4
|
|
|
use gossi\codegen\model\PhpClass; |
|
5
|
|
|
use keeko\tools\generator\BlankHtmlResponseGenerator; |
|
6
|
|
|
use keeko\tools\generator\BlankJsonResponseGenerator; |
|
7
|
|
|
use keeko\tools\generator\GeneratorFactory; |
|
8
|
|
|
use keeko\tools\generator\ModelResponseTraitGenerator; |
|
9
|
|
|
use keeko\tools\generator\TwigHtmlResponseGenerator; |
|
10
|
|
|
use keeko\tools\helpers\QuestionHelperTrait; |
|
11
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
|
12
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
13
|
|
|
use Symfony\Component\Console\Input\InputOption; |
|
14
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
15
|
|
|
use Symfony\Component\Console\Question\ConfirmationQuestion; |
|
16
|
|
|
use Symfony\Component\Console\Question\Question; |
|
17
|
|
|
|
|
18
|
|
|
class GenerateResponseCommand extends AbstractGenerateCommand { |
|
19
|
|
|
|
|
20
|
|
|
use QuestionHelperTrait; |
|
21
|
|
|
|
|
22
|
|
|
protected $traits = []; |
|
23
|
|
|
|
|
24
|
20 |
|
protected function configure() { |
|
25
|
20 |
|
$this |
|
26
|
20 |
|
->setName('generate:response') |
|
27
|
20 |
|
->setDescription('Generates code for a response') |
|
28
|
20 |
|
->addArgument( |
|
29
|
20 |
|
'name', |
|
30
|
20 |
|
InputArgument::OPTIONAL, |
|
31
|
|
|
'The name of the action, which should be generated. Typically in the form %nomen%-%verb% (e.g. user-create)' |
|
32
|
20 |
|
) |
|
33
|
20 |
|
->addOption( |
|
34
|
20 |
|
'format', |
|
35
|
20 |
|
'', |
|
36
|
20 |
|
InputOption::VALUE_OPTIONAL, |
|
37
|
20 |
|
'The response format to create', |
|
38
|
|
|
'json' |
|
39
|
20 |
|
) |
|
40
|
20 |
|
->addOption( |
|
41
|
20 |
|
'template', |
|
42
|
20 |
|
'', |
|
43
|
20 |
|
InputOption::VALUE_OPTIONAL, |
|
44
|
20 |
|
'The template for the body method (blank or twig)', |
|
45
|
1 |
|
'blank' |
|
46
|
20 |
|
) |
|
47
|
|
|
; |
|
48
|
|
|
|
|
49
|
20 |
|
$this->configureGenerateOptions(); |
|
50
|
|
|
|
|
51
|
20 |
|
parent::configure(); |
|
52
|
20 |
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Checks whether actions can be generated at all by reading composer.json and verify |
|
56
|
|
|
* all required information are available |
|
57
|
|
|
*/ |
|
58
|
7 |
|
private function preCheck() { |
|
59
|
7 |
|
$module = $this->packageService->getModule(); |
|
60
|
7 |
|
if ($module === null || count($module->getActionNames()) == 0) { |
|
61
|
1 |
|
throw new \DomainException('No action definition found in composer.json - please run `keeko generate:action`.'); |
|
62
|
|
|
} |
|
63
|
6 |
|
} |
|
64
|
|
|
|
|
65
|
|
|
protected function interact(InputInterface $input, OutputInterface $output) { |
|
66
|
|
|
$this->preCheck(); |
|
67
|
|
|
|
|
68
|
|
|
// check if the dialog can be skipped |
|
69
|
|
|
$name = $input->getArgument('name'); |
|
|
|
|
|
|
70
|
|
|
$specificAction = false; |
|
71
|
|
|
|
|
72
|
|
|
if ($name === null) { |
|
73
|
|
|
$formatQuestion = new ConfirmationQuestion('Do you want to generate a response for a specific action?'); |
|
74
|
|
|
$specificAction = $this->askConfirmation($formatQuestion); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
// ask which action |
|
78
|
|
|
if ($specificAction) { |
|
79
|
|
|
$names = []; |
|
|
|
|
|
|
80
|
|
|
$actions = $this->getKeekoActions(); |
|
|
|
|
|
|
81
|
|
|
foreach (array_keys($actions) as $name) { |
|
82
|
|
|
$names[] = $name; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
$formatQuestion = new Question('Which action'); |
|
86
|
|
|
$formatQuestion->setAutocompleterValues($names); |
|
87
|
|
|
$name = $this->askQuestion($formatQuestion); |
|
88
|
|
|
$input->setArgument('name', $name); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
|
|
92
|
|
|
// ask which format |
|
93
|
|
|
$formatQuestion = new Question('Which format', 'json'); |
|
94
|
|
|
$formatQuestion->setAutocompleterValues(['json', 'html']); |
|
95
|
|
|
$format = $this->askQuestion($formatQuestion); |
|
96
|
|
|
$input->setOption('format', $format); |
|
97
|
|
|
|
|
98
|
|
|
// ask which template |
|
99
|
|
|
$templateQuestion = new Question('Which template', 'blank'); |
|
100
|
|
|
$templateQuestion->setAutocompleterValues(['blank', 'twig']); |
|
101
|
|
|
$template = $this->askQuestion($templateQuestion); |
|
102
|
|
|
$input->setOption('template', $template); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
7 |
|
protected function execute(InputInterface $input, OutputInterface $output) { |
|
106
|
7 |
|
$this->preCheck(); |
|
107
|
|
|
|
|
108
|
6 |
|
$name = $input->getArgument('name'); |
|
109
|
|
|
|
|
110
|
|
|
// only a specific action |
|
111
|
6 |
|
if ($name) { |
|
112
|
5 |
|
$this->generateResponse($name); |
|
113
|
4 |
|
} |
|
114
|
|
|
|
|
115
|
|
|
// anyway all actions |
|
116
|
|
|
else { |
|
117
|
1 |
|
$actions = $this->packageService->getModule()->getActionNames(); |
|
118
|
|
|
|
|
119
|
1 |
|
foreach ($actions as $name) { |
|
120
|
1 |
|
$this->generateResponse($name); |
|
121
|
1 |
|
} |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
5 |
|
$this->packageService->savePackage(); |
|
125
|
5 |
|
} |
|
126
|
|
|
|
|
127
|
6 |
|
private function generateResponse($actionName) { |
|
128
|
6 |
|
$module = $this->packageService->getModule(); |
|
129
|
|
|
|
|
130
|
6 |
|
if (!$module->hasAction($actionName)) { |
|
131
|
1 |
|
throw new \RuntimeException(sprintf('action (%s) not found', $actionName)); |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
5 |
|
$input = $this->io->getInput(); |
|
|
|
|
|
|
135
|
5 |
|
$action = $module->getAction($actionName); |
|
|
|
|
|
|
136
|
5 |
|
$modelName = $this->modelService->getModelNameByAction($action); |
|
137
|
5 |
|
$format = $input->getOption('format'); |
|
|
|
|
|
|
138
|
5 |
|
$template = $input->getOption('template'); |
|
|
|
|
|
|
139
|
|
|
|
|
140
|
5 |
|
if (!$action->hasResponse($format)) { |
|
141
|
5 |
|
$action->setResponse($format, str_replace(['Action', 'action'], [ucwords($format) . 'Response', 'response'], $action->getClass())); |
|
142
|
5 |
|
} |
|
143
|
|
|
|
|
144
|
|
|
// find generator |
|
145
|
5 |
|
$generator = null; |
|
146
|
5 |
|
$type = $this->packageService->getActionType($actionName, $modelName); |
|
|
|
|
|
|
147
|
5 |
|
$isModel = $type && $this->modelService->isModelAction($action); |
|
|
|
|
|
|
148
|
|
|
|
|
149
|
|
|
// model given and format is json |
|
150
|
5 |
|
if ($isModel && $format === 'json') { |
|
151
|
2 |
|
$generator = GeneratorFactory::createJsonResponseGenerator($type, $this->service); |
|
152
|
2 |
|
} |
|
153
|
|
|
|
|
154
|
|
|
// blank json |
|
155
|
4 |
|
else if ($format === 'json') { |
|
156
|
2 |
|
$generator = new BlankJsonResponseGenerator($this->service); |
|
157
|
2 |
|
} |
|
158
|
|
|
|
|
159
|
|
|
// html + twig |
|
160
|
2 |
|
else if ($format === 'html' && $template == 'twig') { |
|
161
|
1 |
|
$generator = new TwigHtmlResponseGenerator($this->service); |
|
162
|
1 |
|
} |
|
163
|
|
|
|
|
164
|
|
|
// blank html as default |
|
165
|
1 |
|
else if ($format == 'html') { |
|
166
|
1 |
|
$generator = new BlankHtmlResponseGenerator($this->service); |
|
167
|
1 |
|
} |
|
168
|
|
|
|
|
169
|
|
|
// run generation, if generator was chosen |
|
170
|
5 |
|
if ($generator !== null) { |
|
171
|
|
|
/* @var $class PhpClass */ |
|
172
|
5 |
|
$class = $generator->generate($action); |
|
173
|
|
|
|
|
174
|
|
|
// generate json trait |
|
175
|
5 |
|
if ($isModel && $format === 'json') { |
|
176
|
2 |
|
$generator = new ModelResponseTraitGenerator($this->service); |
|
177
|
2 |
|
$trait = $generator->generate($action); |
|
|
|
|
|
|
178
|
2 |
|
$class->addTrait($trait); |
|
|
|
|
|
|
179
|
|
|
|
|
180
|
2 |
|
if (!in_array($trait->getName(), $this->traits)) { |
|
181
|
2 |
|
$this->codegenService->dumpStruct($trait, true); |
|
182
|
2 |
|
$this->traits[] = $trait->getName(); |
|
183
|
2 |
|
} |
|
184
|
2 |
|
} |
|
185
|
|
|
|
|
186
|
|
|
// write to file |
|
187
|
5 |
|
$this->codegenService->dumpStruct($class, $input->getOption('force')); |
|
188
|
5 |
|
} |
|
189
|
5 |
|
} |
|
190
|
|
|
|
|
191
|
|
|
} |
|
192
|
|
|
|
This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.
To visualize
will produce issues in the first and second line, while this second example
will produce no issues.