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