1
|
|
|
<?php |
2
|
|
|
//[PHPCOMPRESSOR(remove,start)] |
3
|
|
|
/** |
4
|
|
|
* Created by PhpStorm. |
5
|
|
|
* User: VITALYIEGOROV |
6
|
|
|
* Date: 09.12.15 |
7
|
|
|
* Time: 14:34 |
8
|
|
|
*/ |
9
|
|
|
namespace samsoncms\api; |
10
|
|
|
|
11
|
|
|
use samsoncms\api\generator\exception\ParentEntityNotFound; |
12
|
|
|
use samsoncms\api\generator\Generator; |
13
|
|
|
use samsoncms\api\generator\Metadata; |
14
|
|
|
use samsonframework\orm\DatabaseInterface; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Entity classes generator. |
18
|
|
|
* @package samsoncms\api |
19
|
|
|
*/ |
20
|
|
|
class GeneratorApi extends Generator |
21
|
|
|
{ |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Generator constructor. |
25
|
|
|
* @param DatabaseInterface $database Database instance |
26
|
|
|
* @throws ParentEntityNotFound |
27
|
|
|
* @throws \samsoncms\api\exception\AdditionalFieldTypeNotFound |
28
|
|
|
*/ |
29
|
|
|
public function __construct(DatabaseInterface $database) |
30
|
|
|
{ |
31
|
|
|
parent::__construct($database); |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Fill metadata only with structures which have to be generated |
35
|
|
|
*/ |
36
|
|
|
$this->fillMetadata(); |
|
|
|
|
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Generate entity related structure table instance. |
41
|
|
|
* |
42
|
|
|
* @param string $tableClassName Table entity class name |
43
|
|
|
* |
44
|
|
|
* @return string Generated PHP method code |
45
|
|
|
*/ |
46
|
|
View Code Duplication |
protected function generateEntityTableMethod($tableClassName) |
|
|
|
|
47
|
|
|
{ |
48
|
|
|
$code = "\n\t" . '/**'; |
49
|
|
|
$code .= "\n\t" . ' * Create '.$tableClassName.' instance.'; |
50
|
|
|
$code .= "\n\t" . ' * @param ViewInterface $renderer Renderer'; |
51
|
|
|
$code .= "\n\t" . ' * @param string $locale Locale'; |
52
|
|
|
$code .= "\n\t" . ' * @return '.$tableClassName.'Query Instance of entity table'; |
53
|
|
|
$code .= "\n\t" . ' */'; |
54
|
|
|
$code .= "\n\t" . 'public function ' . lcfirst($tableClassName) . '(ViewInterface $renderer = null, $locale = null)'; |
55
|
|
|
$code .= "\n\t" . "{"; |
56
|
|
|
$code .= "\n\t\t" . 'return new '.$tableClassName.'Query($this->id, $this->query, $renderer, $this->id, $locale);'; |
57
|
|
|
|
58
|
|
|
return $code . "\n\t" . "}"."\n"; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Generate entity classes. |
63
|
|
|
* |
64
|
|
|
* @param string $namespace Base namespace for generated classes |
65
|
|
|
* |
66
|
|
|
* @return string Generated PHP code for entity classes |
67
|
|
|
* @throws ParentEntityNotFound |
68
|
|
|
* @throws \samsoncms\api\exception\AdditionalFieldTypeNotFound |
69
|
|
|
*/ |
70
|
|
|
public function createEntityClasses($namespace = __NAMESPACE__) |
71
|
|
|
{ |
72
|
|
|
$classes = "\n" . 'namespace ' . $namespace . '\\generated;'; |
73
|
|
|
$classes .= "\n"; |
74
|
|
|
$classes .= "\n" . 'use ' . $namespace . '\field\Row;'; |
75
|
|
|
$classes .= "\n" . 'use \samsoncms\api\Entity;'; |
76
|
|
|
$classes .= "\n" . 'use \samsonframework\core\ViewInterface;'; |
77
|
|
|
$classes .= "\n" . 'use \samsonframework\orm\ArgumentInterface;'; |
78
|
|
|
$classes .= "\n" . 'use \samsonframework\orm\QueryInterface;'; |
79
|
|
|
$classes .= "\n" . 'use \samson\activerecord\dbQuery;'; |
80
|
|
|
$classes .= "\n"; |
81
|
|
|
|
82
|
|
|
// Iterate all entities metadata |
83
|
|
|
foreach ($this->metadata as $metadata) { |
84
|
|
|
// Generate classes of default type |
85
|
|
|
if ($metadata->type === Metadata::TYPE_DEFAULT) { |
86
|
|
|
// Generate entity class |
87
|
|
|
$classes .= $this->createEntityClass($metadata); |
88
|
|
|
// Generate query class for queries |
89
|
|
|
$classes .= $this->createQueryClass($metadata); |
90
|
|
|
// Generate collection class for rendering |
91
|
|
|
$classes .= $this->createCollectionClass( |
92
|
|
|
$metadata, |
93
|
|
|
$this->fullEntityName($metadata->entity . 'Query', $namespace), |
94
|
|
|
array(\samsoncms\api\Renderable::class) |
95
|
|
|
); |
96
|
|
|
|
97
|
|
|
// Generate Gallery classes for this entity |
98
|
|
|
$classes .= $this->createGalleryClass($metadata); |
99
|
|
|
|
100
|
|
|
// Generate classes of table type |
101
|
|
|
} elseif ($metadata->type === Metadata::TYPE_TABLE) { |
102
|
|
|
$classes .= $this->createTableQueryClass($metadata); |
103
|
|
|
$classes .= $this->createTableRowEntityClass($metadata); |
104
|
|
|
$classes .= $this->createTableRowQueryClass($metadata); |
105
|
|
|
|
106
|
|
|
// $classes .= $this->createTableRowClass($metadata); |
107
|
|
|
// $classes .= $this->createQueryClass($metadata, '\samsoncms\api\query\EntityTable' , array(), $namespace, $metadata->entity.'Row'); |
108
|
|
|
$classes .= $this->createTableClass($metadata); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
// Make correct code formatting |
113
|
|
|
return $this->formatTab($classes); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Create entity table row entity PHP class code. |
118
|
|
|
* |
119
|
|
|
* @param Metadata $metadata Entity metadata |
120
|
|
|
* @param string $namespace Namespace of generated class |
121
|
|
|
* @return string Generated entity query PHP class code |
122
|
|
|
*/ |
123
|
|
|
protected function createTableRowEntityClass(Metadata $metadata, $namespace = __NAMESPACE__) |
124
|
|
|
{ |
125
|
|
|
$metadata = clone $metadata; |
126
|
|
|
$metadata->entity = str_replace('Table', '', $metadata->entity); |
127
|
|
|
return $this->createEntityClass($metadata, $namespace); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Create entity table row query PHP class code. |
132
|
|
|
* |
133
|
|
|
* @param Metadata $metadata Entity metadata |
134
|
|
|
* @param string $namespace Namespace of generated class |
135
|
|
|
* @return string Generated entity query PHP class code |
136
|
|
|
*/ |
137
|
|
|
protected function createTableRowQueryClass(Metadata $metadata, $namespace = __NAMESPACE__) |
|
|
|
|
138
|
|
|
{ |
139
|
|
|
$metadata = clone $metadata; |
140
|
|
|
$metadata->entity = str_replace('Table', '', $metadata->entity); |
141
|
|
|
return $this->createQueryClass($metadata); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Create entity PHP class code. |
146
|
|
|
* |
147
|
|
|
* @param Metadata $metadata Entity metadata |
148
|
|
|
* @param string $namespace Namespace of generated class |
149
|
|
|
* @return string Generated entity query PHP class code |
150
|
|
|
*/ |
151
|
|
|
protected function createEntityClass(Metadata $metadata, $namespace = __NAMESPACE__) |
152
|
|
|
{ |
153
|
|
|
/** |
154
|
|
|
* TODO: Parent problem |
155
|
|
|
* Should be changed to merging fields instead of extending with OOP for structure_relation support |
156
|
|
|
* or creating traits and using them on shared parent entities. |
157
|
|
|
*/ |
158
|
|
|
|
159
|
|
|
$this->generator |
160
|
|
|
->multiComment(array('"' . $metadata->entityRealName . '" entity class')) |
161
|
|
|
->defClass($metadata->entity, null !== $metadata->parent ? $this->fullEntityName($metadata->parent->entity, $namespace) : 'Entity') |
162
|
|
|
->commentVar('string', '@deprecated Entity full class name, use ::class') |
163
|
|
|
->defClassConst('ENTITY', $this->fullEntityName($metadata->entity, $namespace)) |
164
|
|
|
->commentVar('string', 'Entity manager full class name') |
165
|
|
|
->defClassConst('MANAGER', $this->fullEntityName($metadata->entity, $namespace) . 'Query') |
166
|
|
|
->commentVar('string', 'Entity database identifier') |
167
|
|
|
->defClassConst('IDENTIFIER', $metadata->entityID) |
168
|
|
|
->commentVar('string', 'Not transliterated entity name') |
169
|
|
|
->defClassVar('$viewName', 'protected static', $metadata->entityRealName); |
170
|
|
|
|
171
|
|
|
foreach ($metadata->allFieldIDs as $fieldID => $fieldName) { |
172
|
|
|
$this->generator |
173
|
|
|
->commentVar('string', $metadata->fieldDescriptions[$fieldID] . ' variable name') |
174
|
|
|
->defClassConst('F_' . $fieldName, $fieldName) |
175
|
|
|
->commentVar('string', $metadata->fieldDescriptions[$fieldID] . ' additional field identifier') |
176
|
|
|
->defClassConst('F_' . $fieldName . '_ID', $fieldID) |
177
|
|
|
->commentVar($metadata->allFieldTypes[$fieldID], $metadata->fieldDescriptions[$fieldID]) |
178
|
|
|
->defClassVar('$' . $fieldName, 'public'); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** Iterate all metadata to find nested structure tables */ |
182
|
|
|
foreach ($this->metadata as $structureID => $tableMetadata) { |
183
|
|
|
// Check if this is nested table structure metadata |
184
|
|
|
if ($tableMetadata->parentID === $metadata->entityID && $tableMetadata->type === Metadata::TYPE_TABLE) { |
|
|
|
|
185
|
|
|
$this->generator->text($this->generateEntityTableMethod($tableMetadata->entity, $tableMetadata->entityID)); |
|
|
|
|
186
|
|
|
} |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
return $this->generator |
190
|
|
|
->commentVar('array', 'Collection of navigation identifiers') |
191
|
|
|
->defClassVar('$navigationIDs', 'protected static', array($metadata->entityID)) |
192
|
|
|
->defClassVar('$_sql_select', 'public static ', $metadata->arSelect) |
193
|
|
|
->defClassVar('$_attributes', 'public static ', $metadata->arAttributes) |
194
|
|
|
->defClassVar('$_map', 'public static ', $metadata->arMap) |
195
|
|
|
->defClassVar('$_sql_from', 'public static ', $metadata->arFrom) |
196
|
|
|
->defClassVar('$_own_group', 'public static ', $metadata->arGroup) |
197
|
|
|
->defClassVar('$_relation_alias', 'public static ', $metadata->arRelationAlias) |
198
|
|
|
->defClassVar('$_relation_type', 'public static ', $metadata->arRelationType) |
199
|
|
|
->defClassVar('$_relations', 'public static ', $metadata->arRelations) |
200
|
|
|
->defClassVar('$fieldIDs', 'protected static ', $metadata->allFieldIDs) |
201
|
|
|
->defClassVar('$fieldValueColumns', 'protected static ', $metadata->allFieldValueColumns) |
202
|
|
|
->endClass() |
203
|
|
|
->flush(); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* Generate entity table row creation method. |
208
|
|
|
* |
209
|
|
|
* @param string $tableClassName Table entity class name |
210
|
|
|
* @return string Generated PHP method code |
211
|
|
|
*/ |
212
|
|
View Code Duplication |
protected function generateEntityTableRowMethod($tableClassName) |
|
|
|
|
213
|
|
|
{ |
214
|
|
|
$code = "\n\t" . '/**'; |
215
|
|
|
$code .= "\n\t" . ' * Create '.$tableClassName.' instance.'; |
216
|
|
|
$code .= "\n\t" . ' * @param ViewInterface $renderer Renderer'; |
217
|
|
|
$code .= "\n\t" . ' * @param string $locale Locale'; |
218
|
|
|
$code .= "\n\t" . ' * @return '.$tableClassName.'Query Instance of entity table'; |
219
|
|
|
$code .= "\n\t" . ' */'; |
220
|
|
|
$code .= "\n\t" . 'public function createRow(ViewInterface $renderer = null, $locale = null)'; |
221
|
|
|
$code .= "\n\t" . "{"; |
222
|
|
|
$code .= "\n\t\t" . 'return new '.$tableClassName.'($this->query, $renderer, $this->id, $locale);'; |
223
|
|
|
|
224
|
|
|
return $code . "\n\t" . "}"."\n"; |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* Create entity table query PHP class code. |
229
|
|
|
* |
230
|
|
|
* @param Metadata $metadata Entity metadata |
231
|
|
|
* @param array $use Collection of traits |
232
|
|
|
* @param string $namespace Namespace of generated class |
233
|
|
|
* @param string $returnClass Query methods return class |
234
|
|
|
* |
235
|
|
|
* @return string Generated entity query PHP class code |
236
|
|
|
*/ |
237
|
|
|
protected function createTableQueryClass(Metadata $metadata, $use = array(), $namespace = __NAMESPACE__, $returnClass = null) |
|
|
|
|
238
|
|
|
{ |
239
|
|
|
$this->generateQuerableClassHeader( |
240
|
|
|
$metadata, |
241
|
|
|
'Query', |
242
|
|
|
'\\'.\samsoncms\api\query\EntityTable::class, |
243
|
|
|
$use, |
244
|
|
|
$namespace, |
245
|
|
|
$metadata->entity.'Row' |
246
|
|
|
); |
247
|
|
|
|
248
|
|
View Code Duplication |
foreach ($metadata->allFieldIDs as $fieldID => $fieldName) { |
|
|
|
|
249
|
|
|
// TODO: Add different method generation depending on their field type |
250
|
|
|
$this->generator->text($this->generateFieldConditionMethod( |
251
|
|
|
$fieldName, |
252
|
|
|
$fieldID, |
253
|
|
|
$metadata->allFieldTypes[$fieldID] |
254
|
|
|
)); |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
return $this->generator |
258
|
|
|
->text($this->generateConstructorTableQueryClass()) |
259
|
|
|
->commentVar('array', 'Collection of real additional field names') |
260
|
|
|
->defClassVar('$fieldRealNames', 'public static', $metadata->realNames) |
261
|
|
|
->commentVar('array', 'Collection of additional field names') |
262
|
|
|
->defClassVar('$fieldNames', 'public static', $metadata->allFieldNames) |
263
|
|
|
// TODO: two above fields should be protected |
264
|
|
|
->commentVar('array', 'Collection of navigation identifiers') |
265
|
|
|
->defClassVar('$navigationIDs', 'protected static', array($metadata->entityID)) |
266
|
|
|
->commentVar('string', 'Entity full class name') |
267
|
|
|
->defClassVar('$identifier', 'protected static', $this->fullEntityName($metadata->entity, $namespace)) |
268
|
|
|
->commentVar('array', 'Collection of localized additional fields identifiers') |
269
|
|
|
->defClassVar('$localizedFieldIDs', 'protected static', $metadata->localizedFieldIDs) |
270
|
|
|
->commentVar('array', 'Collection of NOT localized additional fields identifiers') |
271
|
|
|
->defClassVar('$notLocalizedFieldIDs', 'protected static', $metadata->notLocalizedFieldIDs) |
272
|
|
|
->commentVar('array', 'Collection of localized additional fields identifiers') |
273
|
|
|
->defClassVar('$fieldIDs', 'protected static', $metadata->allFieldIDs) |
274
|
|
|
->commentVar('array', 'Collection of additional fields value column names') |
275
|
|
|
->defClassVar('$fieldValueColumns', 'protected static', $metadata->allFieldValueColumns) |
276
|
|
|
->endClass() |
277
|
|
|
->flush(); |
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
/** |
281
|
|
|
* Create entity query PHP class code. |
282
|
|
|
* |
283
|
|
|
* @param Metadata $metadata Entity metadata |
284
|
|
|
* @param string $defaultParent Parent class name |
285
|
|
|
* @param array $use Collection of traits |
286
|
|
|
* @param string $namespace Namespace of generated class |
287
|
|
|
* @param string $returnClass Query methods return class |
288
|
|
|
* |
289
|
|
|
* @return string Generated entity query PHP class code |
290
|
|
|
*/ |
291
|
|
|
protected function createQueryClass(Metadata $metadata, $defaultParent = '\samsoncms\api\query\Entity', $use = array(), $namespace = __NAMESPACE__, $returnClass = null) |
292
|
|
|
{ |
293
|
|
|
$this->generateQuerableClassHeader($metadata, 'Query', $defaultParent, $use, $namespace, $returnClass); |
294
|
|
|
|
295
|
|
View Code Duplication |
foreach ($metadata->allFieldIDs as $fieldID => $fieldName) { |
|
|
|
|
296
|
|
|
// TODO: Add different method generation depending on their field type |
297
|
|
|
$this->generator->text($this->generateFieldConditionMethod( |
298
|
|
|
$fieldName, |
299
|
|
|
$fieldID, |
300
|
|
|
$metadata->allFieldTypes[$fieldID] |
301
|
|
|
)); |
302
|
|
|
} |
303
|
|
|
|
304
|
|
|
return $this->generator |
305
|
|
|
->commentVar('array', 'Collection of real additional field names') |
306
|
|
|
->defClassVar('$fieldRealNames', 'public static', $metadata->realNames) |
307
|
|
|
->commentVar('array', 'Collection of additional field names') |
308
|
|
|
->defClassVar('$fieldNames', 'public static', $metadata->allFieldNames) |
309
|
|
|
// TODO: two above fields should be protected |
310
|
|
|
->commentVar('array', 'Collection of navigation identifiers') |
311
|
|
|
->defClassVar('$navigationIDs', 'protected static', array($metadata->entityID)) |
312
|
|
|
->commentVar('string', 'Entity full class name') |
313
|
|
|
->defClassVar('$identifier', 'protected static', $this->fullEntityName($metadata->entity, $namespace)) |
314
|
|
|
->commentVar('array', 'Collection of localized additional fields identifiers') |
315
|
|
|
->defClassVar('$localizedFieldIDs', 'protected static', $metadata->localizedFieldIDs) |
316
|
|
|
->commentVar('array', 'Collection of NOT localized additional fields identifiers') |
317
|
|
|
->defClassVar('$notLocalizedFieldIDs', 'protected static', $metadata->notLocalizedFieldIDs) |
318
|
|
|
->commentVar('array', 'Collection of localized additional fields identifiers') |
319
|
|
|
->defClassVar('$fieldIDs', 'protected static', $metadata->allFieldIDs) |
320
|
|
|
->commentVar('array', 'Collection of additional fields value column names') |
321
|
|
|
->defClassVar('$fieldValueColumns', 'protected static', $metadata->allFieldValueColumns) |
322
|
|
|
->endClass() |
323
|
|
|
->flush(); |
324
|
|
|
} |
325
|
|
|
|
326
|
|
|
/** |
327
|
|
|
* Start quearable class declaration. |
328
|
|
|
* |
329
|
|
|
* @param Metadata $metadata Entity metadata |
330
|
|
|
* @param string $suffix Entity class name suffix |
331
|
|
|
* @param string $defaultParent Parent class name |
332
|
|
|
* @param array $use Collection of traits |
333
|
|
|
* @param string $namespace Namespace of generated class |
334
|
|
|
* @param string $returnClass Query methods return class |
335
|
|
|
*/ |
336
|
|
|
protected function generateQuerableClassHeader(Metadata $metadata, $suffix, $defaultParent, $use, $namespace = __NAMESPACE__, $returnClass = null) |
337
|
|
|
{ |
338
|
|
|
$returnClass = null === $returnClass ? $this->fullEntityName($metadata->entity, $namespace) : $returnClass; |
339
|
|
|
|
340
|
|
|
$this->generator |
341
|
|
|
->multiComment(array( |
342
|
|
|
'Class for fetching "' . $metadata->entityRealName . '" instances from database', |
343
|
|
|
'@method ' . $returnClass . ' first();', |
344
|
|
|
'@method ' . $returnClass . '[] find();', |
345
|
|
|
)) |
346
|
|
|
->defClass($metadata->entity . $suffix, $defaultParent); |
347
|
|
|
|
348
|
|
|
|
349
|
|
|
// Add traits to generated classes |
350
|
|
|
$this->generateTraitsUsage($use); |
351
|
|
|
} |
352
|
|
|
|
353
|
|
|
/** |
354
|
|
|
* Generate class traits usage. |
355
|
|
|
* |
356
|
|
|
* @param array $use Collection of trait names |
357
|
|
|
*/ |
358
|
|
|
protected function generateTraitsUsage($use = array()) |
359
|
|
|
{ |
360
|
|
|
// Add traits to generated classes |
361
|
|
|
foreach ($use as $trait) { |
362
|
|
|
$this->generator->newLine(); |
363
|
|
|
$this->generator->tabs('use \\' . ltrim($trait, '\\') . ';'); |
364
|
|
|
} |
365
|
|
|
$this->generator->newLine(); |
366
|
|
|
} |
367
|
|
|
|
368
|
|
|
/** |
369
|
|
|
* Generate Query::where() analog for specific field. |
370
|
|
|
* |
371
|
|
|
* @param string $fieldName Field name |
372
|
|
|
* @param string $fieldId Field primary identifier |
373
|
|
|
* @param string $fieldType Field PHP type |
374
|
|
|
* @return string Generated PHP method code |
375
|
|
|
*/ |
376
|
|
|
protected function generateFieldConditionMethod($fieldName, $fieldId, $fieldType) |
377
|
|
|
{ |
378
|
|
|
$code = "\n\t" . '/**'; |
379
|
|
|
$code .= "\n\t" . ' * Add '.$fieldName.'(#' . $fieldId . ') field query condition.'; |
380
|
|
|
$code .= "\n\t" . ' * @param ' . $fieldType . ' $value Field value'; |
381
|
|
|
$code .= "\n\t" . ' * @return $this Chaining'; |
382
|
|
|
$code .= "\n\t" . ' * @see Generic::where()'; |
383
|
|
|
$code .= "\n\t" . ' */'; |
384
|
|
|
$code .= "\n\t" . 'public function ' . $fieldName . '($value, $relation = ArgumentInterface::EQUAL)'; |
385
|
|
|
$code .= "\n\t" . "{"; |
386
|
|
|
$code .= "\n\t\t" . 'return $this->where("' . $fieldName . '", $value, $relation);'; |
387
|
|
|
|
388
|
|
|
return $code . "\n\t" . "}"."\n"; |
389
|
|
|
} |
390
|
|
|
|
391
|
|
|
/** |
392
|
|
|
* Create entity collection PHP class code. |
393
|
|
|
* |
394
|
|
|
* @param Metadata $metadata Entity metadata |
395
|
|
|
* @param string $suffix Generated class name suffix |
396
|
|
|
* @param string $defaultParent Parent class name |
397
|
|
|
* @param array $use Collection of traits |
398
|
|
|
* @param string $namespace Namespace of generated class |
399
|
|
|
* |
400
|
|
|
* @return string Generated entity query PHP class code |
401
|
|
|
*/ |
402
|
|
|
protected function createCollectionClass(Metadata $metadata, $defaultParent, $use = array(), $suffix = 'Collection', $namespace = __NAMESPACE__) |
|
|
|
|
403
|
|
|
{ |
404
|
|
|
$this->generateQuerableClassHeader($metadata, 'Collection', $defaultParent, $use, $namespace); |
405
|
|
|
|
406
|
|
|
return $this->generator |
407
|
|
|
->text($this->generateConstructorCollectionClass()) |
408
|
|
|
->endClass() |
409
|
|
|
->flush(); |
410
|
|
|
} |
411
|
|
|
|
412
|
|
|
/** |
413
|
|
|
* Generate constructor for collection class. |
414
|
|
|
*/ |
415
|
|
View Code Duplication |
protected function generateConstructorCollectionClass() |
|
|
|
|
416
|
|
|
{ |
417
|
|
|
$class = "\n\t".'/**'; |
418
|
|
|
$class .= "\n\t".' * @param ViewInterface $renderer Rendering instance'; |
419
|
|
|
$class .= "\n\t" . ' * @param QueryInterface $query Database query instance'; |
420
|
|
|
$class .= "\n\t".' * @param string $locale Localization identifier'; |
421
|
|
|
$class .= "\n\t".' */'; |
422
|
|
|
$class .= "\n\t" . 'public function __construct(ViewInterface $renderer, QueryInterface $query = null, $locale = null)'; |
423
|
|
|
$class .= "\n\t".'{'; |
424
|
|
|
$class .= "\n\t\t" . '$this->renderer = $renderer;'; |
425
|
|
|
$class .= "\n\t\t" . 'parent::__construct(isset($query) ? $query : new dbQuery(), $locale);'; |
426
|
|
|
$class .= "\n\t".'}'."\n"; |
427
|
|
|
|
428
|
|
|
return $class; |
429
|
|
|
} |
430
|
|
|
|
431
|
|
|
/** |
432
|
|
|
* Generate classes for entity additional field gallery. |
433
|
|
|
* |
434
|
|
|
* @param Metadata $metadata Entity metadata |
435
|
|
|
* |
436
|
|
|
* @return string Generated Gallery additional field class |
437
|
|
|
*/ |
438
|
|
|
public function createGalleryClass(Metadata $metadata) |
439
|
|
|
{ |
440
|
|
|
// Iterate entity additional fields |
441
|
|
View Code Duplication |
foreach ($metadata->allFieldCmsTypes as $fieldID => $fieldType) { |
|
|
|
|
442
|
|
|
// We need only gallery fields |
443
|
|
|
if ($fieldType === Field::TYPE_GALLERY) { |
444
|
|
|
$fieldName = $metadata->allFieldIDs[$fieldID]; |
445
|
|
|
// Declare class |
446
|
|
|
$this->generateQuerableClassHeader( |
447
|
|
|
$metadata, |
448
|
|
|
ucfirst($fieldName) . 'Gallery', |
449
|
|
|
'\\' . \samsoncms\api\Gallery::class, |
450
|
|
|
array(\samsoncms\api\Renderable::class) |
451
|
|
|
); |
452
|
|
|
|
453
|
|
|
return $this->generator |
454
|
|
|
->text($this->generateConstructorGalleryClass( |
455
|
|
|
$metadata->entity . '::F_' . strtoupper($fieldName) . '_ID', |
456
|
|
|
$metadata->entity |
457
|
|
|
)) |
458
|
|
|
->endClass() |
459
|
|
|
->flush(); |
460
|
|
|
} |
461
|
|
|
} |
462
|
|
|
|
463
|
|
|
|
464
|
|
|
} |
465
|
|
|
|
466
|
|
|
/** |
467
|
|
|
* Generate constructor for gallery class. |
468
|
|
|
*/ |
469
|
|
|
protected function generateConstructorGalleryClass($fieldID, $entityType) |
470
|
|
|
{ |
471
|
|
|
$class = "\n\t" . '/**'; |
472
|
|
|
$class .= "\n\t" . ' * @param ViewInterface $renderer Rendering instance'; |
473
|
|
|
$class .= "\n\t" . ' * @param ' . $entityType . ' $entity Parent entity'; |
474
|
|
|
$class .= "\n\t" . ' * @param QueryInterface $query Database query instance'; |
475
|
|
|
$class .= "\n\t" . ' */'; |
476
|
|
|
$class .= "\n\t" . 'public function __construct(ViewInterface $renderer, ' . $entityType . ' $entity, QueryInterface $query = null)'; |
477
|
|
|
$class .= "\n\t" . '{'; |
478
|
|
|
$class .= "\n\t\t" . '$this->renderer = $renderer;'; |
479
|
|
|
$class .= "\n\t\t" . 'parent::__construct(isset($query) ? $query : new dbQuery(), $entity->id, ' . $fieldID . ');'; |
480
|
|
|
$class .= "\n\t" . '}' . "\n"; |
481
|
|
|
|
482
|
|
|
return $class; |
483
|
|
|
} |
484
|
|
|
|
485
|
|
|
/** |
486
|
|
|
* Create fields table row PHP class code. |
487
|
|
|
* |
488
|
|
|
* @param Metadata $metadata metadata of entity |
489
|
|
|
* @param string $namespace Namespace of generated class |
490
|
|
|
* |
491
|
|
|
* @return string Generated entity query PHP class code |
492
|
|
|
* @throws exception\AdditionalFieldTypeNotFound |
493
|
|
|
*/ |
494
|
|
|
protected function createTableRowClass(Metadata $metadata, $namespace = __NAMESPACE__) |
|
|
|
|
495
|
|
|
{ |
496
|
|
|
$this->generator |
497
|
|
|
->multiComment(array('Class for getting "' . $metadata->entityRealName . '" fields table rows')) |
498
|
|
|
->defClass($metadata->entity . 'Row', 'Row'); |
499
|
|
|
|
500
|
|
|
$fieldIDs = array(); |
501
|
|
|
foreach ($this->navigationFields($metadata->entityID) as $fieldID => $fieldRow) { |
502
|
|
|
$fieldName = $this->fieldName($fieldRow['Name']); |
503
|
|
|
|
504
|
|
|
// Fill field ids array |
505
|
|
|
$fieldIDs[$fieldName] = $fieldID; |
506
|
|
|
|
507
|
|
|
$this->generator |
508
|
|
|
->commentVar($metadata->allFieldTypes[$fieldID], $fieldRow['Description'] . ' Field #' . $fieldID . ' variable name') |
509
|
|
|
->defClassConst('F_' . strtoupper($fieldName), $fieldName) |
510
|
|
|
->commentVar($metadata->allFieldTypes[$fieldID], $fieldRow['Description'] . ' Field #' . $fieldID . ' row value') |
511
|
|
|
->defVar('public $' . $fieldName) |
512
|
|
|
->text("\n"); |
513
|
|
|
} |
514
|
|
|
|
515
|
|
|
return $this->generator |
516
|
|
|
->commentVar('array', 'Collection of additional fields identifiers') |
517
|
|
|
->defClassVar('$fieldIDs', 'public static', $fieldIDs) |
518
|
|
|
->endClass() |
519
|
|
|
->flush(); |
520
|
|
|
} |
521
|
|
|
|
522
|
|
|
/** |
523
|
|
|
* Create fields table PHP class code. |
524
|
|
|
* |
525
|
|
|
* @param Metadata $metadata metadata of entity |
526
|
|
|
* @param string $namespace Namespace of generated class |
527
|
|
|
* @return string Generated entity query PHP class code |
528
|
|
|
* @throws exception\AdditionalFieldTypeNotFound |
529
|
|
|
*/ |
530
|
|
|
protected function createTableClass(Metadata $metadata, $namespace = __NAMESPACE__) |
|
|
|
|
531
|
|
|
{ |
532
|
|
|
$this->generator |
533
|
|
|
->multiComment(array('Class for getting "'.$metadata->entityRealName.'" fields table')) |
534
|
|
|
->defClass($metadata->entity, '\samsoncms\api\field\Table'); |
535
|
|
|
|
536
|
|
|
// Add renderable trait |
537
|
|
|
$this->generateTraitsUsage(array(\samsoncms\api\Renderable::class)); |
538
|
|
|
|
539
|
|
|
// Iterate additional fields |
540
|
|
|
$fields = array(); |
541
|
|
|
foreach ($this->navigationFields($metadata->entityID) as $fieldID => $fieldRow) { |
542
|
|
|
$fieldName = $this->fieldName($fieldRow['Name']); |
543
|
|
|
|
544
|
|
|
$this->generator |
545
|
|
|
->text($this->generateTableFieldMethod( |
546
|
|
|
$fieldName, |
547
|
|
|
$fieldRow[Field::F_PRIMARY], |
548
|
|
|
$fieldRow[Field::F_TYPE] |
549
|
|
|
)) |
550
|
|
|
->commentVar($metadata->allFieldTypes[$fieldID], $fieldRow['Description'] . ' Field #' . $fieldID . ' variable name') |
551
|
|
|
->defClassConst('F_' . $fieldName, $fieldName); |
552
|
|
|
|
553
|
|
|
// Collection original to new one field names |
554
|
|
|
$fields[$fieldRow['Name']] = $fieldName; |
555
|
|
|
} |
556
|
|
|
|
557
|
|
|
return $this->generator |
558
|
|
|
->text($this->generateConstructorTableClass()) |
559
|
|
|
->commentVar('string', 'Entity database identifier') |
560
|
|
|
->defClassConst('IDENTIFIER', $metadata->entityID) |
561
|
|
|
->commentVar('array', 'Collection of real additional field names') |
562
|
|
|
->defClassVar('$fieldsRealNames', 'public static', $fields) |
563
|
|
|
->commentVar('array', 'Collection of navigation identifiers') |
564
|
|
|
->defClassVar('$navigationIDs', 'protected static', array($metadata->entityID)) |
565
|
|
|
->commentVar('string', 'Row class name') |
566
|
|
|
->defClassVar('$identifier', 'protected', $this->fullEntityName($this->entityName($metadata->entityRealName) . 'TableRow')) |
567
|
|
|
->endClass() |
568
|
|
|
->flush(); |
569
|
|
|
} |
570
|
|
|
|
571
|
|
|
/** |
572
|
|
|
* Generate FieldsTable::values() analog for specific field. |
573
|
|
|
* |
574
|
|
|
* @param string $fieldName Field name |
575
|
|
|
* @param string $fieldId Field primary identifier |
576
|
|
|
* @param string $fieldType Field PHP type |
577
|
|
|
* |
578
|
|
|
* @return string Generated PHP method code |
579
|
|
|
*/ |
580
|
|
|
protected function generateTableFieldMethod($fieldName, $fieldId, $fieldType) |
581
|
|
|
{ |
582
|
|
|
$code = "\n\t" . '/**'; |
583
|
|
|
$code .= "\n\t" . ' * Get table column ' . $fieldName . '(#' . $fieldId . ') values.'; |
584
|
|
|
$code .= "\n\t" . ' * @return array Collection(' . Field::phpType($fieldType) . ') of table column values'; |
585
|
|
|
$code .= "\n\t" . ' */'; |
586
|
|
|
$code .= "\n\t" . 'public function ' . $fieldName . '()'; |
587
|
|
|
$code .= "\n\t" . "{"; |
588
|
|
|
$code .= "\n\t\t" . 'return $this->values(' . $fieldId . ');'; |
589
|
|
|
|
590
|
|
|
return $code . "\n\t" . "}" . "\n"; |
591
|
|
|
} |
592
|
|
|
|
593
|
|
|
/** |
594
|
|
|
* Generate constructor for table class. |
595
|
|
|
*/ |
596
|
|
View Code Duplication |
protected function generateConstructorTableClass() |
|
|
|
|
597
|
|
|
{ |
598
|
|
|
$class = "\n\t" . '/**'; |
599
|
|
|
$class .= "\n\t" . ' * @param QueryInterface $query Database query instance'; |
600
|
|
|
$class .= "\n\t" . ' * @param ViewInterface $renderer Rendering instance'; |
601
|
|
|
$class .= "\n\t" . ' * @param integer $entityID Entity identifier to whom this table belongs'; |
602
|
|
|
$class .= "\n\t" . ' * @param string $locale Localization identifier'; |
603
|
|
|
$class .= "\n\t" . ' */'; |
604
|
|
|
$class .= "\n\t" . 'public function __construct(QueryInterface $query, ViewInterface $renderer, $entityID, $locale = null)'; |
605
|
|
|
$class .= "\n\t" . '{'; |
606
|
|
|
$class .= "\n\t\t" . '$this->renderer = $renderer;'; |
607
|
|
|
$class .= "\n\t\t" . 'parent::__construct($query, static::$navigationIDs, $entityID, $locale);'; |
608
|
|
|
$class .= "\n\t" . '}' . "\n"; |
609
|
|
|
|
610
|
|
|
return $class; |
611
|
|
|
} |
612
|
|
|
|
613
|
|
|
/** |
614
|
|
|
* Generate constructor for table query class. |
615
|
|
|
*/ |
616
|
|
View Code Duplication |
protected function generateConstructorTableQueryClass() |
|
|
|
|
617
|
|
|
{ |
618
|
|
|
$class = "\n\t" . '/**'; |
619
|
|
|
$class .= "\n\t" . ' * @param int $entityID Entity identifier to whom this table belongs'; |
620
|
|
|
$class .= "\n\t" . ' * @param QueryInterface $query Database query instance'; |
621
|
|
|
$class .= "\n\t" . ' * @param string $locale Localization identifier'; |
622
|
|
|
$class .= "\n\t" . ' */'; |
623
|
|
|
$class .= "\n\t" . 'public function __construct($entityID, QueryInterface $query, $locale = null)'; |
624
|
|
|
$class .= "\n\t" . '{'; |
625
|
|
|
$class .= "\n\t\t" . 'parent::__construct(static::$navigationIDs, $entityID, $query, $locale);'; |
626
|
|
|
$class .= "\n\t" . '}' . "\n"; |
627
|
|
|
|
628
|
|
|
return $class; |
629
|
|
|
} |
630
|
|
|
|
631
|
|
|
/** |
632
|
|
|
* Generate Query::where() analog for specific field. |
633
|
|
|
* |
634
|
|
|
* @param string $fieldName Field name |
635
|
|
|
* @param string $fieldId Field primary identifier |
636
|
|
|
* @param string $fieldType Field PHP type |
637
|
|
|
* |
638
|
|
|
* @return string Generated PHP method code |
639
|
|
|
*/ |
640
|
|
|
protected function generateLocalizedFieldConditionMethod($fieldName, $fieldId, $fieldType) |
641
|
|
|
{ |
642
|
|
|
$code = "\n\t" . '/**'; |
643
|
|
|
$code .= "\n\t" . ' * Add ' . $fieldName . '(#' . $fieldId . ') field query condition.'; |
644
|
|
|
$code .= "\n\t" . ' * @param ' . Field::phpType($fieldType) . ' $value Field value'; |
645
|
|
|
$code .= "\n\t" . ' * @return $this Chaining'; |
646
|
|
|
$code .= "\n\t" . ' * @see Generic::where()'; |
647
|
|
|
$code .= "\n\t" . ' */'; |
648
|
|
|
$code .= "\n\t" . 'public function ' . $fieldName . '($value)'; |
649
|
|
|
$code .= "\n\t" . "{"; |
650
|
|
|
$code .= "\n\t\t" . 'return $this->where("' . $fieldName . '", $value);'; |
651
|
|
|
|
652
|
|
|
return $code . "\n\t" . "}" . "\n"; |
653
|
|
|
} |
654
|
|
|
|
655
|
|
|
/** |
656
|
|
|
* Generate constructor for application class. |
657
|
|
|
*/ |
658
|
|
View Code Duplication |
protected function generateConstructorApplicationClass() |
|
|
|
|
659
|
|
|
{ |
660
|
|
|
$class = "\n\t" . '/**'; |
661
|
|
|
$class .= "\n\t" . ' * Render materials list with pager'; |
662
|
|
|
$class .= "\n\t" . ' *'; |
663
|
|
|
$class .= "\n\t" . ' * @param string $navigationId Structure identifier'; |
664
|
|
|
$class .= "\n\t" . ' * @param string $search Keywords to filter table'; |
665
|
|
|
$class .= "\n\t" . ' * @param int $page Current table page'; |
666
|
|
|
$class .= "\n\t" . ' * @return array Asynchronous response containing status and materials list with pager on success'; |
667
|
|
|
$class .= "\n\t" . ' * or just status on asynchronous controller failure'; |
668
|
|
|
$class .= "\n\t" . ' */'; |
669
|
|
|
$class .= "\n\t" . 'public function __async_collection($navigationId = \'0\', $search = \'\', $page = 1)'; |
670
|
|
|
$class .= "\n\t" . '{'; |
671
|
|
|
$class .= "\n\t\t" . 'return parent::__async_collection(self::$navigation, $search, $page);'; |
672
|
|
|
$class .= "\n\t" . '}' . "\n"; |
673
|
|
|
|
674
|
|
|
return $class; |
675
|
|
|
} |
676
|
|
|
|
677
|
|
|
/** |
678
|
|
|
* Generate constructor for application class. |
679
|
|
|
*/ |
680
|
|
View Code Duplication |
protected function generateConstructorApplicationCollectionClass() |
|
|
|
|
681
|
|
|
{ |
682
|
|
|
$class = "\n\t" . '/**'; |
683
|
|
|
$class .= "\n\t" . ' * Generic collection constructor'; |
684
|
|
|
$class .= "\n\t" . ' *'; |
685
|
|
|
$class .= "\n\t" . ' * @param RenderInterface $renderer View render object'; |
686
|
|
|
$class .= "\n\t" . ' * @param QueryInterface $query Query object'; |
687
|
|
|
$class .= "\n\t" . ' */'; |
688
|
|
|
$class .= "\n\t" . 'public function __async_collection($renderer, $query = null, $pager = null)'; |
689
|
|
|
$class .= "\n\t" . '{'; |
690
|
|
|
$class .= "\n\t\t" . 'return parent::__async_collection($renderer, $query = null, $pager = null);'; |
691
|
|
|
$class .= "\n\t\t" . '$this->fields = array('; |
692
|
|
|
$class .= "\n\t\t\t" . 'new Control(),'; |
693
|
|
|
$class .= "\n\t\t" . ');'; |
694
|
|
|
$class .= "\n\t" . '}' . "\n"; |
695
|
|
|
|
696
|
|
|
return $class; |
697
|
|
|
} |
698
|
|
|
} |
699
|
|
|
//[PHPCOMPRESSOR(remove,end)] |
700
|
|
|
|
This check looks for function calls that miss required arguments.