1
|
|
|
<?php |
2
|
|
|
namespace keeko\tools\services; |
3
|
|
|
|
4
|
|
|
use gossi\codegen\model\AbstractPhpStruct; |
5
|
|
|
use gossi\docblock\tags\AuthorTag; |
6
|
|
|
use Propel\Generator\Model\Table; |
7
|
|
|
use gossi\codegen\generator\CodeFileGenerator; |
8
|
|
|
use keeko\tools\utils\NamespaceResolver; |
9
|
|
|
use keeko\core\schema\PackageSchema; |
10
|
|
|
use keeko\core\schema\AuthorSchema; |
11
|
|
|
use phootwork\file\File; |
12
|
|
|
use phootwork\file\Path; |
13
|
|
|
use keeko\core\schema\CodegenSchema; |
14
|
|
|
|
15
|
|
|
class CodeGeneratorService extends AbstractService { |
16
|
|
|
|
17
|
|
|
private $codegen; |
18
|
|
|
|
19
|
7 |
|
public function getCodegenFile() { |
20
|
7 |
|
$basepath = dirname($this->project->getComposerFileName()); |
21
|
7 |
|
return $basepath . '/codegen.json'; |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Loads the contents from codegen.json into a collection |
26
|
|
|
* |
27
|
|
|
* @throws JsonEmptyException |
28
|
|
|
* @throws \RuntimeException |
29
|
|
|
* @return CodegenSchema |
30
|
|
|
*/ |
31
|
12 |
|
public function getCodegen() { |
32
|
7 |
|
if ($this->codegen === null) { |
33
|
7 |
|
$file = new File($this->getCodegenFile()); |
|
|
|
|
34
|
7 |
|
$this->codegen = $file->exists() |
35
|
|
|
? CodegenSchema::fromFile($this->getCodegenFile()) |
36
|
7 |
|
: new CodegenSchema(); |
37
|
12 |
|
} |
38
|
|
|
|
39
|
|
|
return $this->codegen; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
// /** |
43
|
|
|
// * Returns the codegen part for the given action name or an empty map |
44
|
|
|
// * |
45
|
|
|
// * @param string $name |
46
|
|
|
// * @return Map |
47
|
|
|
// */ |
48
|
|
|
// public function getCodegenAction($name) { |
49
|
|
|
// $codegen = $this->getCodegen(); |
50
|
|
|
|
51
|
|
|
// if (isset($codegen['actions'])) { |
|
|
|
|
52
|
|
|
// $actions = $codegen['actions']; |
53
|
|
|
|
54
|
|
|
// if (isset($actions[$name])) { |
|
|
|
|
55
|
|
|
// return $actions[$name]; |
56
|
|
|
// } |
57
|
|
|
// } |
58
|
|
|
|
59
|
|
|
// return null; |
|
|
|
|
60
|
|
|
// } |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Adds authors to the docblock of the given struct |
64
|
|
|
* |
65
|
15 |
|
* @param AbstractPhpStruct $struct |
66
|
15 |
|
* @param array $package |
67
|
|
|
*/ |
68
|
15 |
|
public function addAuthors(AbstractPhpStruct $struct, PackageSchema $package) { |
69
|
|
|
$docblock = $struct->getDocblock(); |
70
|
|
|
|
71
|
15 |
|
foreach ($package->getAuthors() as $author) { |
72
|
15 |
|
/* @var $author AuthorSchema */ |
73
|
15 |
|
$tag = AuthorTag::create()->setName($author->getName()); |
|
|
|
|
74
|
|
|
$mail = $author->getEmail(); |
75
|
15 |
|
|
76
|
|
|
if (!empty($mail)) { |
77
|
15 |
|
$tag->setEmail($mail); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
$docblock->appendTag($tag); |
81
|
15 |
|
} |
82
|
15 |
|
} |
83
|
15 |
|
|
84
|
|
|
/** |
85
|
|
|
* Returns code for hydrating a propel model |
86
|
|
|
* |
87
|
|
|
* @param string $modelName |
88
|
|
|
* @return string |
89
|
|
|
*/ |
90
|
|
|
public function getWriteFields($modelName) { |
91
|
5 |
|
$codegen = $this->getCodegen(); |
|
|
|
|
92
|
5 |
|
$conversions = $codegen->getWriteConversion($modelName); |
93
|
5 |
|
$filter = $codegen->getWriteFilter($modelName); |
|
|
|
|
94
|
5 |
|
$model = $this->modelService->getModel($modelName); |
|
|
|
|
95
|
5 |
|
$computed = $this->getComputedFields($model); |
|
|
|
|
96
|
5 |
|
$filter = array_merge($filter, $computed); |
|
|
|
|
97
|
5 |
|
|
98
|
|
|
$fields = ''; |
99
|
5 |
|
$cols = $model->getColumns(); |
|
|
|
|
100
|
5 |
|
foreach ($cols as $col) { |
101
|
5 |
|
$prop = $col->getName(); |
102
|
5 |
|
|
103
|
|
|
if (!in_array($prop, $filter)) { |
104
|
5 |
|
$fields .= sprintf("'%s'", $prop); |
105
|
5 |
|
|
106
|
|
|
if (isset($conversions[$prop])) { |
107
|
5 |
|
$fields .= ' => function($v) {'."\n\t".'return ' . $conversions[$prop] . ';'."\n".'}'; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
$fields .= ', '; |
111
|
5 |
|
} |
112
|
5 |
|
} |
113
|
5 |
|
|
114
|
|
|
if (strlen($fields) > 0) { |
115
|
5 |
|
$fields = substr($fields, 0, -2); |
116
|
5 |
|
} |
117
|
5 |
|
|
118
|
|
|
return sprintf('[%s]', $fields); |
119
|
5 |
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Returns the fields for a model |
123
|
|
|
* |
124
|
|
|
* @param string $modelName |
125
|
|
|
* @return array |
126
|
|
|
*/ |
127
|
|
|
public function getReadFields($modelName) { |
128
|
|
|
$codegen = $this->getCodegen(); |
129
|
|
|
$model = $this->modelService->getModel($modelName); |
|
|
|
|
130
|
|
|
// $computed = $this->getComputedFields($model); |
|
|
|
|
131
|
|
|
$filter = $codegen->getReadFilter($modelName); |
132
|
|
|
// $filter = array_merge($filter, $computed); |
|
|
|
|
133
|
|
|
|
134
|
|
|
$fields = []; |
135
|
|
|
$cols = $model->getColumns(); |
|
|
|
|
136
|
|
|
foreach ($cols as $col) { |
137
|
|
|
$prop = $col->getName(); |
138
|
|
|
|
139
|
|
|
if (!in_array($prop, $filter)) { |
140
|
|
|
$fields[] = $prop; |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
return $fields; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
// /** |
148
|
|
|
// * Returns conversions for model columns |
149
|
|
|
// * |
150
|
5 |
|
// * @param string $model |
151
|
5 |
|
// * @param string $type |
152
|
|
|
// * @return array |
153
|
|
|
// */ |
154
|
5 |
|
// public function getConversions($model, $type) { |
155
|
5 |
|
// return $this->getActionProp($model, $type, 'conversion'); |
156
|
5 |
|
// } |
157
|
5 |
|
|
158
|
5 |
|
// /** |
159
|
5 |
|
// * Returns model columns that should be filtered |
160
|
|
|
// * |
161
|
5 |
|
// * @param string $model |
162
|
|
|
// * @param string $type |
163
|
|
|
// * @return array |
164
|
5 |
|
// */ |
165
|
5 |
|
// public function getFilter($model, $type) { |
166
|
|
|
// return $this->getActionProp($model, $type, 'filter'); |
167
|
5 |
|
// } |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Returns computed model fields |
171
|
|
|
* |
172
|
|
|
* @param Table $table |
173
|
|
|
* @return array<string> |
174
|
|
|
*/ |
175
|
|
|
public function getComputedFields(Table $table) { |
176
|
2 |
|
$fields = []; |
177
|
2 |
|
|
178
|
2 |
|
// iterate over behaviors to get their respective columns |
179
|
|
|
foreach ($table->getBehaviors() as $behavior) { |
180
|
2 |
|
switch ($behavior->getName()) { |
181
|
|
|
case 'timestampable': |
182
|
2 |
|
$fields[] = $behavior->getParameter('create_column'); |
183
|
|
|
$fields[] = $behavior->getParameter('update_column'); |
184
|
|
|
break; |
185
|
|
|
|
186
|
2 |
|
case 'aggregate_column': |
187
|
|
|
$fields[] = $behavior->getParameter('name'); |
188
|
|
|
break; |
189
|
15 |
|
} |
190
|
15 |
|
} |
191
|
15 |
|
|
192
|
|
|
return $fields; |
193
|
15 |
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* Helper to represent an array as php code |
197
|
15 |
|
* |
198
|
15 |
|
* @param array $array |
199
|
15 |
|
* @return string |
200
|
15 |
|
*/ |
201
|
15 |
View Code Duplication |
public function arrayToCode(array $array) { |
|
|
|
|
202
|
|
|
$fields = ''; |
203
|
|
|
foreach ($array as $item) { |
204
|
15 |
|
$fields .= sprintf("'%s', ", $item); |
205
|
15 |
|
} |
206
|
15 |
|
|
207
|
|
|
if (strlen($fields) > 0) { |
208
|
15 |
|
$fields = substr($fields, 0, -2); |
209
|
|
|
} |
210
|
15 |
|
|
211
|
15 |
|
return sprintf('[%s]', $fields); |
212
|
|
|
} |
213
|
|
|
|
214
|
15 |
View Code Duplication |
public function mapToCode(array $array) { |
|
|
|
|
215
|
|
|
$fields = ''; |
216
|
|
|
foreach ($array as $k => $item) { |
217
|
15 |
|
$fields .= sprintf("\t'%s' => %s,\n", $k, $this->arrayToCode($item)); |
218
|
15 |
|
} |
219
|
15 |
|
|
220
|
|
|
if (strlen($fields) > 0) { |
221
|
|
|
$fields = substr($fields, 0, -2); |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
return sprintf("[\n%s\n]", $fields); |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
public function getFilename(AbstractPhpStruct $struct) { |
228
|
|
|
$package = $this->packageService->getPackage(); |
|
|
|
|
229
|
|
|
$relativeSourcePath = NamespaceResolver::getSourcePath($struct->getNamespace(), $package); |
230
|
|
|
|
231
|
|
|
if ($relativeSourcePath === null) { |
232
|
|
|
return null; |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
$jsonFile = $this->project->getComposerFileName(); |
236
|
|
|
$path = new Path(dirname($jsonFile)); |
|
|
|
|
237
|
|
|
$path = $path->append($relativeSourcePath); |
|
|
|
|
238
|
|
|
$path = $path->append($struct->getName() . '.php'); |
|
|
|
|
239
|
|
|
return $path->toString(); |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
public function dumpStruct(AbstractPhpStruct $struct, $overwrite = false) { |
243
|
|
|
$filename = $this->getFilename($struct); |
244
|
|
|
$file = new File($filename); |
|
|
|
|
245
|
|
|
|
246
|
|
|
if ($filename !== null && $file->exists() ? $overwrite : true) { |
247
|
|
|
// generate code |
248
|
|
|
$generator = new CodeFileGenerator(); |
249
|
|
|
$code = $generator->generate($struct); |
|
|
|
|
250
|
|
|
|
251
|
|
|
// write code to file |
252
|
|
|
$file->write($code); |
253
|
|
|
|
254
|
|
|
// tell user about |
255
|
|
|
$this->io->writeln(sprintf('Class <info>%s</info> written at <info>%s</info>', $struct->getQualifiedName(), $filename)); |
256
|
|
|
} |
257
|
|
|
} |
258
|
|
|
} |
259
|
|
|
|
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.