|
1
|
|
|
<?php |
|
2
|
|
|
namespace keeko\tools\services; |
|
3
|
|
|
|
|
4
|
|
|
use gossi\codegen\generator\CodeFileGenerator; |
|
5
|
|
|
use gossi\codegen\model\AbstractPhpStruct; |
|
6
|
|
|
use gossi\docblock\tags\AuthorTag; |
|
7
|
|
|
use keeko\framework\schema\CodegenSchema; |
|
8
|
|
|
use keeko\framework\schema\PackageSchema; |
|
9
|
|
|
use keeko\tools\utils\NamespaceResolver; |
|
10
|
|
|
use phootwork\file\File; |
|
11
|
|
|
use phootwork\file\Path; |
|
12
|
|
|
use Propel\Generator\Model\Table; |
|
13
|
|
|
|
|
14
|
|
|
class CodeGeneratorService extends AbstractService { |
|
15
|
|
|
|
|
16
|
|
|
private $codegen; |
|
|
|
|
|
|
17
|
|
|
|
|
18
|
|
|
public function getCodegenFile() { |
|
19
|
7 |
|
return $this->project->getCodegenFileName(); |
|
20
|
7 |
|
} |
|
21
|
7 |
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Returns codegen from project |
|
24
|
|
|
* |
|
25
|
|
|
* @throws JsonEmptyException |
|
26
|
|
|
* @throws \RuntimeException |
|
27
|
|
|
* @return CodegenSchema |
|
28
|
|
|
*/ |
|
29
|
|
|
public function getCodegen() { |
|
30
|
|
|
return $this->project->getCodegen(); |
|
31
|
12 |
|
} |
|
32
|
7 |
|
|
|
33
|
7 |
|
/** |
|
34
|
7 |
|
* Adds authors to the docblock of the given struct |
|
35
|
|
|
* |
|
36
|
7 |
|
* @param AbstractPhpStruct $struct |
|
37
|
12 |
|
* @param PackageSchema $package |
|
38
|
|
|
*/ |
|
39
|
|
|
public function addAuthors(AbstractPhpStruct $struct, PackageSchema $package = null) { |
|
40
|
|
|
if ($package === null) { |
|
41
|
|
|
$package = $this->packageService->getPackage(); |
|
42
|
|
|
} |
|
43
|
|
|
$docblock = $struct->getDocblock(); |
|
44
|
|
|
|
|
45
|
|
|
foreach ($package->getAuthors() as $author) { |
|
46
|
|
|
/* @var $author AuthorSchema */ |
|
47
|
|
|
$tag = AuthorTag::create()->setName($author->getName()); |
|
48
|
|
|
$mail = $author->getEmail(); |
|
49
|
|
|
|
|
50
|
|
|
if (!empty($mail)) { |
|
51
|
|
|
$tag->setEmail($mail); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
$docblock->appendTag($tag); |
|
55
|
|
|
} |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Returns code for hydrating a propel model |
|
60
|
|
|
* |
|
61
|
|
|
* @param string $modelName |
|
62
|
|
|
* @return string |
|
63
|
|
|
*/ |
|
64
|
|
|
public function getWriteFields($modelName) { |
|
65
|
15 |
|
$codegen = $this->getCodegen(); |
|
66
|
15 |
|
$filter = $codegen->getWriteFilter($modelName); |
|
67
|
|
|
$model = $this->modelService->getModel($modelName); |
|
68
|
15 |
|
$computed = $this->getComputedFields($model); |
|
69
|
|
|
$filter = array_merge($filter, $computed); |
|
70
|
|
|
|
|
71
|
15 |
|
$fields = []; |
|
72
|
15 |
|
$cols = $model->getColumns(); |
|
73
|
15 |
|
foreach ($cols as $col) { |
|
74
|
|
|
$prop = $col->getName(); |
|
75
|
15 |
|
|
|
76
|
|
|
if (!in_array($prop, $filter)) { |
|
77
|
15 |
|
$fields[] = $prop; |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
15 |
|
return $fields; |
|
82
|
15 |
|
} |
|
83
|
15 |
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Returns the fields for a model |
|
86
|
|
|
* |
|
87
|
|
|
* @param string $modelName |
|
88
|
|
|
* @return array |
|
89
|
|
|
*/ |
|
90
|
|
|
public function getReadFields($modelName) { |
|
91
|
5 |
|
$codegen = $this->getCodegen(); |
|
92
|
5 |
|
$model = $this->modelService->getModel($modelName); |
|
93
|
5 |
|
// $computed = $this->getComputedFields($model); |
|
|
|
|
|
|
94
|
5 |
|
$filter = $codegen->getReadFilter($modelName); |
|
95
|
5 |
|
// $filter = array_merge($filter, $computed); |
|
|
|
|
|
|
96
|
5 |
|
|
|
97
|
5 |
|
$fields = []; |
|
98
|
|
|
$cols = $model->getColumns(); |
|
99
|
5 |
|
foreach ($cols as $col) { |
|
100
|
5 |
|
$prop = $col->getName(); |
|
101
|
5 |
|
|
|
102
|
5 |
|
if (!in_array($prop, $filter) && !$col->isForeignKey() && !$col->isPrimaryKey()) { |
|
103
|
|
|
$fields[] = $prop; |
|
104
|
5 |
|
} |
|
105
|
5 |
|
} |
|
106
|
|
|
|
|
107
|
5 |
|
return $fields; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
5 |
|
* Returns computed model fields |
|
112
|
5 |
|
* |
|
113
|
5 |
|
* @param Table $table |
|
114
|
|
|
* @return array<string> |
|
115
|
5 |
|
*/ |
|
116
|
5 |
|
public function getComputedFields(Table $table) { |
|
117
|
5 |
|
$fields = []; |
|
118
|
|
|
|
|
119
|
5 |
|
// iterate over behaviors to get their respective columns |
|
120
|
|
|
foreach ($table->getBehaviors() as $behavior) { |
|
121
|
|
|
switch ($behavior->getName()) { |
|
122
|
|
|
case 'timestampable': |
|
123
|
|
|
$fields[] = $behavior->getParameter('create_column'); |
|
124
|
|
|
$fields[] = $behavior->getParameter('update_column'); |
|
125
|
|
|
break; |
|
126
|
|
|
|
|
127
|
|
|
case 'aggregate_column': |
|
128
|
|
|
$fields[] = $behavior->getParameter('name'); |
|
129
|
|
|
break; |
|
130
|
|
|
} |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
return $fields; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* Helper to represent an array as php code |
|
138
|
|
|
* |
|
139
|
|
|
* @param array $array |
|
140
|
|
|
* @return string |
|
141
|
|
|
*/ |
|
142
|
|
|
public function arrayToCode(array $array) { |
|
143
|
|
|
$fields = ''; |
|
144
|
|
|
foreach ($array as $item) { |
|
145
|
|
|
$fields .= sprintf("'%s', ", $item); |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
if (strlen($fields) > 0) { |
|
149
|
|
|
$fields = substr($fields, 0, -2); |
|
150
|
5 |
|
} |
|
151
|
5 |
|
|
|
152
|
|
|
return sprintf('[%s]', $fields); |
|
153
|
|
|
} |
|
154
|
5 |
|
|
|
155
|
5 |
|
/** |
|
156
|
5 |
|
* Returns the filename for a given struct |
|
157
|
5 |
|
* |
|
158
|
5 |
|
* @param AbstractPhpStruct $struct |
|
159
|
5 |
|
* @return string |
|
160
|
|
|
*/ |
|
161
|
5 |
|
public function getFilename(AbstractPhpStruct $struct) { |
|
162
|
|
|
$package = $this->packageService->getPackage(); |
|
163
|
|
|
$relativeSourcePath = NamespaceResolver::getSourcePath($struct->getNamespace(), $package); |
|
164
|
5 |
|
|
|
165
|
5 |
|
if ($relativeSourcePath === null) { |
|
166
|
|
|
return null; |
|
167
|
5 |
|
} |
|
168
|
|
|
|
|
169
|
|
|
$jsonFile = $this->project->getComposerFileName(); |
|
170
|
|
|
$path = new Path(dirname($jsonFile)); |
|
171
|
|
|
$path = $path->append($relativeSourcePath); |
|
172
|
|
|
$path = $path->append($struct->getName() . '.php'); |
|
173
|
|
|
return $path->toString(); |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
2 |
|
/** |
|
177
|
2 |
|
* Returns a file object for a given struct |
|
178
|
2 |
|
* |
|
179
|
|
|
* @param AbstractPhpStruct $struct |
|
180
|
2 |
|
* @return File |
|
181
|
|
|
*/ |
|
182
|
2 |
|
public function getFile(AbstractPhpStruct $struct) { |
|
183
|
|
|
return new File($this->getFilename($struct)); |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
2 |
|
public function dumpStruct(AbstractPhpStruct $struct, $overwrite = false) { |
|
187
|
|
|
$filename = $this->getFilename($struct); |
|
188
|
|
|
$file = new File($filename); |
|
189
|
15 |
|
|
|
190
|
15 |
|
if ($filename !== null && ($file->exists() ? $overwrite : true)) { |
|
191
|
15 |
|
// generate code |
|
192
|
|
|
$generator = new CodeFileGenerator(); |
|
193
|
15 |
|
$code = $generator->generate($struct); |
|
|
|
|
|
|
194
|
|
|
|
|
195
|
|
|
// write code to file |
|
196
|
|
|
$file->write($code); |
|
197
|
15 |
|
|
|
198
|
15 |
|
// tell user about |
|
199
|
15 |
|
$this->io->writeln(sprintf('Class <info>%s</info> written at <info>%s</info>', $struct->getQualifiedName(), $filename)); |
|
200
|
15 |
|
} |
|
201
|
15 |
|
} |
|
202
|
|
|
} |
|
203
|
|
|
|
This check marks private properties in classes that are never used. Those properties can be removed.