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 samson\activerecord\dbMySQLConnector; |
12
|
|
|
use samsoncms\api\generator\exception\ParentEntityNotFound; |
13
|
|
|
use samsoncms\api\generator\Metadata; |
14
|
|
|
use samsoncms\api\query\Generic; |
15
|
|
|
use samsonframework\orm\ArgumentInterface; |
16
|
|
|
use samsonframework\orm\DatabaseInterface; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Entity classes generator. |
20
|
|
|
* @package samsoncms\api |
21
|
|
|
*/ |
22
|
|
|
class Generator |
23
|
|
|
{ |
24
|
|
|
/** @var DatabaseInterface */ |
25
|
|
|
protected $database; |
26
|
|
|
|
27
|
|
|
/** @var \samsonphp\generator\Generator */ |
28
|
|
|
protected $generator; |
29
|
|
|
|
30
|
|
|
/** @var Metadata[] Collection of entities metadata */ |
31
|
|
|
protected $metadata; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Transliterate string to english. |
35
|
|
|
* |
36
|
|
|
* @param string $string Source string |
37
|
|
|
* @return string Transliterated string |
38
|
|
|
*/ |
39
|
|
|
protected function transliterated($string) |
40
|
|
|
{ |
41
|
|
|
return str_replace( |
42
|
|
|
' ', |
43
|
|
|
'', |
44
|
|
|
ucwords(iconv("UTF-8", "UTF-8//IGNORE", strtr($string, array( |
45
|
|
|
"'" => "", |
46
|
|
|
"`" => "", |
47
|
|
|
"-" => " ", |
48
|
|
|
"_" => " ", |
49
|
|
|
"а" => "a", "А" => "a", |
50
|
|
|
"б" => "b", "Б" => "b", |
51
|
|
|
"в" => "v", "В" => "v", |
52
|
|
|
"г" => "g", "Г" => "g", |
53
|
|
|
"д" => "d", "Д" => "d", |
54
|
|
|
"е" => "e", "Е" => "e", |
55
|
|
|
"ж" => "zh", "Ж" => "zh", |
56
|
|
|
"з" => "z", "З" => "z", |
57
|
|
|
"и" => "i", "И" => "i", |
58
|
|
|
"й" => "y", "Й" => "y", |
59
|
|
|
"к" => "k", "К" => "k", |
60
|
|
|
"л" => "l", "Л" => "l", |
61
|
|
|
"м" => "m", "М" => "m", |
62
|
|
|
"н" => "n", "Н" => "n", |
63
|
|
|
"о" => "o", "О" => "o", |
64
|
|
|
"п" => "p", "П" => "p", |
65
|
|
|
"р" => "r", "Р" => "r", |
66
|
|
|
"с" => "s", "С" => "s", |
67
|
|
|
"т" => "t", "Т" => "t", |
68
|
|
|
"у" => "u", "У" => "u", |
69
|
|
|
"ф" => "f", "Ф" => "f", |
70
|
|
|
"х" => "h", "Х" => "h", |
71
|
|
|
"ц" => "c", "Ц" => "c", |
72
|
|
|
"ч" => "ch", "Ч" => "ch", |
73
|
|
|
"ш" => "sh", "Ш" => "sh", |
74
|
|
|
"щ" => "sch", "Щ" => "sch", |
75
|
|
|
"ъ" => "", "Ъ" => "", |
76
|
|
|
"ы" => "y", "Ы" => "y", |
77
|
|
|
"ь" => "", "Ь" => "", |
78
|
|
|
"э" => "e", "Э" => "e", |
79
|
|
|
"ю" => "yu", "Ю" => "yu", |
80
|
|
|
"я" => "ya", "Я" => "ya", |
81
|
|
|
"і" => "i", "І" => "i", |
82
|
|
|
"ї" => "yi", "Ї" => "yi", |
83
|
|
|
"є" => "e", "Є" => "e" |
84
|
|
|
) |
85
|
|
|
) |
86
|
|
|
) |
87
|
|
|
) |
88
|
|
|
); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Get class constant name by its value. |
93
|
|
|
* |
94
|
|
|
* @param string $value Constant value |
95
|
|
|
* @param string $className Class name |
96
|
|
|
* @return string Full constant name |
97
|
|
|
*/ |
98
|
|
|
protected function constantNameByValue($value, $className = Field::ENTITY) |
99
|
|
|
{ |
100
|
|
|
// Get array where class constants are values and their values are keys |
101
|
|
|
$nameByValue = array_flip((new \ReflectionClass($className))->getConstants()); |
102
|
|
|
|
103
|
|
|
// Try to find constant by its value |
104
|
|
|
if (null !== $nameByValue[$value]) { |
105
|
|
|
// Return constant name |
106
|
|
|
return $nameByValue[$value]; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
return ''; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Get correct entity name. |
114
|
|
|
* |
115
|
|
|
* @param string $navigationName Original navigation entity name |
116
|
|
|
* @return string Correct PHP-supported entity name |
117
|
|
|
*/ |
118
|
|
|
protected function entityName($navigationName) |
119
|
|
|
{ |
120
|
|
|
return ucfirst($this->getValidName($this->transliterated($navigationName))); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Remove all wrong characters from entity name |
125
|
|
|
* |
126
|
|
|
* @param string $navigationName Original navigation entity name |
127
|
|
|
* @return string Correct PHP-supported entity name |
128
|
|
|
*/ |
129
|
|
|
protected function getValidName($navigationName) |
130
|
|
|
{ |
131
|
|
|
return preg_replace('/(^\d*)|([^\w\d_])/', '', $navigationName); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Get correct full entity name with name space. |
136
|
|
|
* |
137
|
|
|
* @param string $navigationName Original navigation entity name |
138
|
|
|
* @param string $namespace Namespace |
139
|
|
|
* @return string Correct PHP-supported entity name |
140
|
|
|
*/ |
141
|
|
|
protected function fullEntityName($navigationName, $namespace = __NAMESPACE__) |
142
|
|
|
{ |
143
|
|
|
return '\\'.$namespace.'\\'.$this->entityName($navigationName); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Get correct field name. |
148
|
|
|
* |
149
|
|
|
* @param string $fieldName Original field name |
150
|
|
|
* @return string Correct PHP-supported field name |
151
|
|
|
*/ |
152
|
|
|
protected function fieldName($fieldName) |
153
|
|
|
{ |
154
|
|
|
return $fieldName = lcfirst($this->transliterated($fieldName)); |
|
|
|
|
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Get additional field type in form of Field constant name |
159
|
|
|
* by database additional field type identifier. |
160
|
|
|
* |
161
|
|
|
* @param integer $fieldType Additional field type identifier |
162
|
|
|
* @return string Additional field type constant |
163
|
|
|
*/ |
164
|
|
|
protected function additionalFieldType($fieldType) |
165
|
|
|
{ |
166
|
|
|
return 'Field::'.$this->constantNameByValue($fieldType); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Generate Query::where() analog for specific field. |
171
|
|
|
* |
172
|
|
|
* @param string $fieldName Field name |
173
|
|
|
* @param string $fieldId Field primary identifier |
174
|
|
|
* @param string $fieldType Field PHP type |
175
|
|
|
* @return string Generated PHP method code |
176
|
|
|
*/ |
177
|
|
|
protected function generateFieldConditionMethod($fieldName, $fieldId, $fieldType) |
178
|
|
|
{ |
179
|
|
|
$code = "\n\t" . '/**'; |
180
|
|
|
$code .= "\n\t" . ' * Add '.$fieldName.'(#' . $fieldId . ') field query condition.'; |
181
|
|
|
$code .= "\n\t" . ' * @param '.$fieldType.' $value Field value'; |
182
|
|
|
$code .= "\n\t" . ' * @return $this Chaining'; |
183
|
|
|
$code .= "\n\t" . ' * @see Generic::where()'; |
184
|
|
|
$code .= "\n\t" . ' */'; |
185
|
|
|
$code .= "\n\t" . 'public function ' . $fieldName . '($value, $relation = ArgumentInterface::EQUAL)'; |
186
|
|
|
$code .= "\n\t" . "{"; |
187
|
|
|
$code .= "\n\t\t" . 'return $this->where("'.$fieldName.'", $value, $relation);'; |
188
|
|
|
|
189
|
|
|
return $code . "\n\t" . "}"."\n"; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* Generate Query::where() analog for specific field. |
194
|
|
|
* |
195
|
|
|
* @param string $fieldName Field name |
196
|
|
|
* @param string $fieldId Field primary identifier |
197
|
|
|
* @param string $fieldType Field PHP type |
198
|
|
|
* @return string Generated PHP method code |
199
|
|
|
*/ |
200
|
|
|
protected function generateLocalizedFieldConditionMethod($fieldName, $fieldId, $fieldType) |
201
|
|
|
{ |
202
|
|
|
$code = "\n\t" . '/**'; |
203
|
|
|
$code .= "\n\t" . ' * Add '.$fieldName.'(#' . $fieldId . ') field query condition.'; |
204
|
|
|
$code .= "\n\t" . ' * @param '.Field::phpType($fieldType).' $value Field value'; |
205
|
|
|
$code .= "\n\t" . ' * @return $this Chaining'; |
206
|
|
|
$code .= "\n\t" . ' * @see Generic::where()'; |
207
|
|
|
$code .= "\n\t" . ' */'; |
208
|
|
|
$code .= "\n\t" . 'public function ' . $fieldName . '($value)'; |
209
|
|
|
$code .= "\n\t" . "{"; |
210
|
|
|
$code .= "\n\t\t" . 'return $this->where("'.$fieldName.'", $value);'; |
211
|
|
|
|
212
|
|
|
return $code . "\n\t" . "}"."\n"; |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* Generate FieldsTable::values() analog for specific field. |
217
|
|
|
* |
218
|
|
|
* @param string $fieldName Field name |
219
|
|
|
* @param string $fieldId Field primary identifier |
220
|
|
|
* @param string $fieldType Field PHP type |
221
|
|
|
* @return string Generated PHP method code |
222
|
|
|
*/ |
223
|
|
|
protected function generateTableFieldMethod($fieldName, $fieldId, $fieldType) |
224
|
|
|
{ |
225
|
|
|
$code = "\n\t" . '/**'; |
226
|
|
|
$code .= "\n\t" . ' * Get table column '.$fieldName.'(#' . $fieldId . ') values.'; |
227
|
|
|
$code .= "\n\t" . ' * @return array Collection('.Field::phpType($fieldType).') of table column values'; |
228
|
|
|
$code .= "\n\t" . ' */'; |
229
|
|
|
$code .= "\n\t" . 'public function ' . $fieldName . '()'; |
230
|
|
|
$code .= "\n\t" . "{"; |
231
|
|
|
$code .= "\n\t\t" . 'return $this->values('.$fieldId.');'; |
232
|
|
|
|
233
|
|
|
return $code . "\n\t" . "}"."\n"; |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
/** |
237
|
|
|
* Generate constructor for table class. |
238
|
|
|
*/ |
239
|
|
|
protected function generateConstructorTableClass() |
240
|
|
|
{ |
241
|
|
|
$class = "\n\t".'/**'; |
242
|
|
|
$class .= "\n\t".' * @param QueryInterface $query Database query instance'; |
243
|
|
|
$class .= "\n\t".' * @param ViewInterface $renderer Rendering instance'; |
244
|
|
|
$class .= "\n\t".' * @param integer $entityID Entity identifier to whom this table belongs'; |
245
|
|
|
$class .= "\n\t".' * @param string $locale Localization identifier'; |
246
|
|
|
$class .= "\n\t".' */'; |
247
|
|
|
$class .= "\n\t".'public function __construct(QueryInterface $query, ViewInterface $renderer, $entityID, $locale = null)'; |
248
|
|
|
$class .= "\n\t".'{'; |
249
|
|
|
$class .= "\n\t\t".'parent::__construct($query, $renderer, static::$navigationIDs, $entityID, $locale);'; |
250
|
|
|
$class .= "\n\t".'}'."\n"; |
251
|
|
|
|
252
|
|
|
return $class; |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
/** |
256
|
|
|
* Create fields table row PHP class code. |
257
|
|
|
* |
258
|
|
|
* @param Metadata $metadata metadata of entity |
259
|
|
|
* @return string Generated entity query PHP class code |
260
|
|
|
* @throws exception\AdditionalFieldTypeNotFound |
261
|
|
|
*/ |
262
|
|
|
protected function createTableRowClass(Metadata $metadata) |
263
|
|
|
{ |
264
|
|
|
$navigationName = $metadata->entityRealName; |
265
|
|
|
$entityName = $this->entityName($metadata->entityRealName) . 'TableRow'; |
266
|
|
|
$navigationFields = $this->navigationFields($metadata->entityID); |
267
|
|
|
|
268
|
|
|
$this->generator |
269
|
|
|
->multiComment(array('Class for getting "' . $navigationName . '" fields table rows')) |
270
|
|
|
->defClass($entityName, 'Row'); |
271
|
|
|
|
272
|
|
|
$fieldIDs = array(); |
273
|
|
|
foreach ($navigationFields as $fieldID => $fieldRow) { |
274
|
|
|
$fieldName = $this->fieldName($fieldRow['Name']); |
275
|
|
|
|
276
|
|
|
// Fill field ids array |
277
|
|
|
$fieldIDs[$fieldName] = $fieldID; |
278
|
|
|
|
279
|
|
|
$this->generator |
280
|
|
|
->commentVar(Field::phpType($fieldRow['Type']), $fieldRow['Description'] . ' Field #' . $fieldID . ' variable name') |
281
|
|
|
->defClassConst('F_' . strtoupper($fieldName), $fieldName) |
282
|
|
|
->commentVar(Field::phpType($fieldRow['Type']), $fieldRow['Description'] . ' Field #' . $fieldID . ' row value') |
283
|
|
|
->defVar('public $' . $fieldName) |
284
|
|
|
->text("\n"); |
285
|
|
|
} |
286
|
|
|
|
287
|
|
|
return $this->generator |
288
|
|
|
->commentVar('array', 'Collection of additional fields identifiers') |
289
|
|
|
->defClassVar('$fieldIDs', 'public static', $fieldIDs) |
|
|
|
|
290
|
|
|
->endClass() |
291
|
|
|
->flush(); |
292
|
|
|
} |
293
|
|
|
|
294
|
|
|
/** |
295
|
|
|
* Create fields table PHP class code. |
296
|
|
|
* |
297
|
|
|
* @param Metadata $metadata metadata of entity |
298
|
|
|
* |
299
|
|
|
* @return string Generated entity query PHP class code |
300
|
|
|
* @throws exception\AdditionalFieldTypeNotFound |
301
|
|
|
*/ |
302
|
|
|
protected function createTableClass(Metadata $metadata) |
303
|
|
|
{ |
304
|
|
|
$navigationID = $metadata->entityID; |
305
|
|
|
$navigationName = $metadata->entityRealName; |
306
|
|
|
$entityName = $this->entityName($metadata->entityRealName).'Table'; |
307
|
|
|
$navigationFields = $this->navigationFields($metadata->entityID); |
308
|
|
|
$rowClassName = $this->entityName($metadata->entityRealName).'TableRow'; |
309
|
|
|
|
310
|
|
|
$this->generator |
311
|
|
|
->multiComment(array('Class for getting "'.$navigationName.'" fields table')) |
312
|
|
|
->defClass($entityName, 'FieldsTable'); |
313
|
|
|
|
314
|
|
|
// Iterate additional fields |
315
|
|
|
$fields = array(); |
316
|
|
|
foreach ($navigationFields as $fieldID => $fieldRow) { |
317
|
|
|
$fieldName = $this->fieldName($fieldRow['Name']); |
318
|
|
|
|
319
|
|
|
$this->generator |
320
|
|
|
->text($this->generateTableFieldMethod( |
321
|
|
|
$fieldName, |
322
|
|
|
$fieldRow[Field::F_PRIMARY], |
323
|
|
|
$fieldRow[Field::F_TYPE] |
324
|
|
|
)) |
325
|
|
|
->commentVar(Field::phpType($fieldRow['Type']), $fieldRow['Description'] . ' Field #' . $fieldID . ' variable name') |
326
|
|
|
->defClassConst('F_' . $fieldName, $fieldName); |
327
|
|
|
|
328
|
|
|
// Collection original to new one field names |
329
|
|
|
$fields[$fieldRow['Name']] = $fieldName; |
330
|
|
|
} |
331
|
|
|
|
332
|
|
|
// TODO: Add generator method generation logic |
333
|
|
|
$constructor = $this->generateConstructorTableClass(); |
334
|
|
|
|
335
|
|
|
$this->generator->text($constructor); |
336
|
|
|
|
337
|
|
|
return $this->generator |
338
|
|
|
->commentVar('array', 'Collection of real additional field names') |
339
|
|
|
->defClassVar('$fieldsRealNames', 'public static', $fields) |
|
|
|
|
340
|
|
|
->commentVar('array', 'Collection of navigation identifiers') |
341
|
|
|
->defClassVar('$navigationIDs', 'protected static', array($navigationID)) |
|
|
|
|
342
|
|
|
->commentVar('string', 'Row class name') |
343
|
|
|
->defClassVar('$identifier', 'protected', $this->fullEntityName($rowClassName)) |
344
|
|
|
->endClass() |
345
|
|
|
->flush(); |
346
|
|
|
} |
347
|
|
|
|
348
|
|
|
/** |
349
|
|
|
* Create entity PHP class code. |
350
|
|
|
* |
351
|
|
|
* @param Metadata $metadata Entity metadata |
352
|
|
|
* @return string Generated entity query PHP class code |
353
|
|
|
*/ |
354
|
|
|
protected function createEntityClass(Metadata $metadata) |
355
|
|
|
{ |
356
|
|
|
/** |
357
|
|
|
* TODO: Parent problem |
358
|
|
|
* Should be changed to merging fields instead of extending with OOP for structure_relation support |
359
|
|
|
* or creating traits and using them on shared parent entities. |
360
|
|
|
*/ |
361
|
|
|
|
362
|
|
|
$this->generator |
363
|
|
|
->multiComment(array('"'.$metadata->entityRealName.'" entity class')) |
364
|
|
|
->defClass($metadata->entity, null !== $metadata->parent ? $metadata->parent->className : 'Entity') |
365
|
|
|
->commentVar('string', '@deprecated Entity full class name, use ::class') |
366
|
|
|
->defClassConst('ENTITY', $metadata->className) |
367
|
|
|
->commentVar('string', 'Entity manager full class name') |
368
|
|
|
->defClassConst('MANAGER', $metadata->className.'Query') |
369
|
|
|
->commentVar('string', 'Entity database identifier') |
370
|
|
|
->defClassConst('IDENTIFIER', $metadata->entityID) |
371
|
|
|
->commentVar('string', 'Not transliterated entity name') |
372
|
|
|
->defClassVar('$viewName', 'protected static', $metadata->entityRealName); |
373
|
|
|
|
374
|
|
|
foreach ($metadata->allFieldIDs as $fieldID => $fieldName) { |
375
|
|
|
$this->generator |
376
|
|
|
->commentVar('string', $metadata->fieldDescriptions[$fieldID].' variable name') |
377
|
|
|
->defClassConst('F_' . $fieldName, $fieldName) |
378
|
|
|
->commentVar($metadata->allFieldTypes[$fieldID], $metadata->fieldDescriptions[$fieldID]) |
379
|
|
|
->defClassVar('$' . $fieldName, 'public'); |
380
|
|
|
} |
381
|
|
|
|
382
|
|
|
return $this->generator |
383
|
|
|
->defClassVar('$_sql_select', 'public static ', $metadata->arSelect) |
|
|
|
|
384
|
|
|
->defClassVar('$_attributes', 'public static ', $metadata->arAttributes) |
|
|
|
|
385
|
|
|
->defClassVar('$_map', 'public static ', $metadata->arMap) |
|
|
|
|
386
|
|
|
->defClassVar('$_sql_from', 'public static ', $metadata->arFrom) |
|
|
|
|
387
|
|
|
->defClassVar('$_own_group', 'public static ', $metadata->arGroup) |
|
|
|
|
388
|
|
|
->defClassVar('$_relation_alias', 'public static ', $metadata->arRelationAlias) |
|
|
|
|
389
|
|
|
->defClassVar('$_relation_type', 'public static ', $metadata->arRelationType) |
|
|
|
|
390
|
|
|
->defClassVar('$_relations', 'public static ', $metadata->arRelations) |
|
|
|
|
391
|
|
|
->defClassVar('$fieldIDs', 'protected static ', $metadata->allFieldIDs) |
|
|
|
|
392
|
|
|
->defClassVar('$fieldValueColumns', 'protected static ', $metadata->allFieldValueColumns) |
|
|
|
|
393
|
|
|
->endClass() |
394
|
|
|
->flush(); |
395
|
|
|
} |
396
|
|
|
|
397
|
|
|
/** |
398
|
|
|
* Create entity query PHP class code. |
399
|
|
|
* |
400
|
|
|
* @param Metadata $metadata Entity metadata |
401
|
|
|
* @param string $suffix Generated class name suffix |
402
|
|
|
* @param string $defaultParent Parent class name |
403
|
|
|
* |
404
|
|
|
* @return string Generated entity query PHP class code |
405
|
|
|
*/ |
406
|
|
|
protected function createQueryClass(Metadata $metadata, $suffix = 'Query', $defaultParent = '\samsoncms\api\query\Entity') |
407
|
|
|
{ |
408
|
|
|
//$navigationID, $navigationName, $entityName, $navigationFields, $parentClass = '\samsoncms\api\query\Entity' |
409
|
|
|
$this->generator |
410
|
|
|
->multiComment(array('Class for fetching "'.$metadata->entityRealName.'" instances from database')) |
411
|
|
|
->defClass($metadata->entity.$suffix, $defaultParent) |
412
|
|
|
; |
413
|
|
|
|
414
|
|
|
foreach ($metadata->allFieldIDs as $fieldID => $fieldName) { |
415
|
|
|
// TODO: Add different method generation depending on their field type |
416
|
|
|
$this->generator->text($this->generateFieldConditionMethod( |
417
|
|
|
$fieldName, |
418
|
|
|
$fieldID, |
419
|
|
|
$metadata->allFieldTypes[$fieldID] |
420
|
|
|
)); |
421
|
|
|
} |
422
|
|
|
|
423
|
|
|
return $this->generator |
424
|
|
|
->commentVar('array', 'Collection of real additional field names') |
425
|
|
|
->defClassVar('$fieldRealNames', 'public static', $metadata->realNames) |
|
|
|
|
426
|
|
|
->commentVar('array', 'Collection of additional field names') |
427
|
|
|
->defClassVar('$fieldNames', 'public static', $metadata->allFieldNames) |
|
|
|
|
428
|
|
|
// TODO: two above fields should be protected |
429
|
|
|
->commentVar('array', 'Collection of navigation identifiers') |
430
|
|
|
->defClassVar('$navigationIDs', 'protected static', array($metadata->entityID)) |
|
|
|
|
431
|
|
|
->commentVar('string', 'Entity full class name') |
432
|
|
|
->defClassVar('$identifier', 'protected static', $metadata->className) |
433
|
|
|
->commentVar('array', 'Collection of localized additional fields identifiers') |
434
|
|
|
->defClassVar('$localizedFieldIDs', 'protected static', $metadata->localizedFieldIDs) |
|
|
|
|
435
|
|
|
->commentVar('array', 'Collection of NOT localized additional fields identifiers') |
436
|
|
|
->defClassVar('$notLocalizedFieldIDs', 'protected static', $metadata->notLocalizedFieldIDs) |
|
|
|
|
437
|
|
|
->commentVar('array', 'Collection of localized additional fields identifiers') |
438
|
|
|
->defClassVar('$fieldIDs', 'protected static', $metadata->allFieldIDs) |
|
|
|
|
439
|
|
|
->commentVar('array', 'Collection of additional fields value column names') |
440
|
|
|
->defClassVar('$fieldValueColumns', 'protected static', $metadata->allFieldValueColumns) |
|
|
|
|
441
|
|
|
->endClass() |
442
|
|
|
->flush() |
443
|
|
|
; |
444
|
|
|
} |
445
|
|
|
|
446
|
|
|
/** @return string Entity state hash */ |
447
|
|
|
public function entityHash() |
448
|
|
|
{ |
449
|
|
|
// Получим информацию о всех таблицах из БД |
450
|
|
|
return md5(serialize($this->database->fetch( |
451
|
|
|
'SELECT `TABLES`.`TABLE_NAME` as `TABLE_NAME` |
452
|
|
|
FROM `information_schema`.`TABLES` as `TABLES` |
453
|
|
|
WHERE `TABLES`.`TABLE_SCHEMA`="' . $this->database->database() . '";' |
454
|
|
|
))); |
455
|
|
|
} |
456
|
|
|
|
457
|
|
|
/** |
458
|
|
|
* Find entity parent. |
459
|
|
|
* |
460
|
|
|
* @param $entityID |
461
|
|
|
* |
462
|
|
|
* @return null|int Parent entity identifier |
463
|
|
|
*/ |
464
|
|
|
public function entityParent($entityID) |
465
|
|
|
{ |
466
|
|
|
$parentData = $this->database->fetch(' |
467
|
|
|
SELECT * |
468
|
|
|
FROM structure_relation as sm |
469
|
|
|
JOIN structure as s ON s.StructureID = sm.parent_id |
470
|
|
|
WHERE sm.child_id = "' . $entityID . '" |
471
|
|
|
AND s.StructureID != "' . $entityID . '" |
472
|
|
|
'); |
473
|
|
|
|
474
|
|
|
// Get parent entity identifier |
475
|
|
|
return count($parentData) ? $parentData[0]['StructureID'] : null; |
476
|
|
|
} |
477
|
|
|
|
478
|
|
|
/** @return array Get collection of navigation objects */ |
479
|
|
|
protected function entityNavigations($type = 0) |
480
|
|
|
{ |
481
|
|
|
return $this->database->fetch(' |
482
|
|
|
SELECT * FROM `structure` |
483
|
|
|
WHERE `Active` = "1" AND `Type` = "'.$type.'" |
484
|
|
|
ORDER BY `ParentID` ASC |
485
|
|
|
'); |
486
|
|
|
} |
487
|
|
|
|
488
|
|
|
/** @return array Collection of navigation additional fields */ |
489
|
|
|
protected function navigationFields($navigationID) |
490
|
|
|
{ |
491
|
|
|
$return = array(); |
492
|
|
|
// TODO: Optimize queries make one single query with only needed data |
493
|
|
|
foreach ($this->database->fetch('SELECT * FROM `structurefield` WHERE `StructureID` = "' . $navigationID . '" AND `Active` = "1"') as $fieldStructureRow) { |
494
|
|
|
foreach ($this->database->fetch('SELECT * FROM `field` WHERE `FieldID` = "' . $fieldStructureRow['FieldID'] . '"') as $fieldRow) { |
495
|
|
|
$return[$fieldRow['FieldID']] = $fieldRow; |
496
|
|
|
} |
497
|
|
|
} |
498
|
|
|
|
499
|
|
|
return $return; |
500
|
|
|
} |
501
|
|
|
|
502
|
|
|
/** |
503
|
|
|
* Generate entity classes. |
504
|
|
|
* |
505
|
|
|
* @param string $namespace Base namespace for generated classes |
506
|
|
|
* @return string Generated PHP code for entity classes |
507
|
|
|
*/ |
508
|
|
|
public function createEntityClasses($namespace = __NAMESPACE__) |
509
|
|
|
{ |
510
|
|
|
$classes = "\n" . 'namespace ' . $namespace . ';'; |
511
|
|
|
$classes .= "\n"; |
512
|
|
|
$classes .= "\n" . 'use '.$namespace.'\renderable\FieldsTable;'; |
513
|
|
|
$classes .= "\n" . 'use '.$namespace.'\field\Row;'; |
514
|
|
|
$classes .= "\n" . 'use \samsonframework\core\ViewInterface;'; |
515
|
|
|
$classes .= "\n" . 'use \samsonframework\orm\ArgumentInterface;'; |
516
|
|
|
$classes .= "\n" . 'use \samsonframework\orm\QueryInterface;'; |
517
|
|
|
$classes .= "\n" . 'use \samson\activerecord\dbQuery;'; |
518
|
|
|
$classes .= "\n"; |
519
|
|
|
|
520
|
|
|
// Iterate all metadata types |
521
|
|
|
foreach (Metadata::$types as $type) { |
522
|
|
|
|
523
|
|
|
// Iterate all structures, parents first |
524
|
|
|
foreach ($this->entityNavigations($type) as $structureRow) { |
525
|
|
|
|
526
|
|
|
// Fill in entity metadata |
527
|
|
|
$metadata = new Metadata($type); |
528
|
|
|
|
529
|
|
|
// Get CapsCase and transliterated entity name |
530
|
|
|
$metadata->entity = $this->entityName($structureRow['Name']); |
531
|
|
|
// Try to find entity parent identifier for building future relations |
532
|
|
|
$metadata->parentID = $this->entityParent($structureRow['StructureID']); |
533
|
|
|
|
534
|
|
|
// TODO: Add multiple parent and fetching their data in a loop |
535
|
|
|
|
536
|
|
|
// Set pointer to parent entity |
537
|
|
|
if (null !== $metadata->parentID) { |
538
|
|
|
if (array_key_exists($metadata->parentID, $this->metadata)) { |
539
|
|
|
$metadata->parent = $this->metadata[$metadata->parentID]; |
540
|
|
|
// Add all parent metadata to current object |
541
|
|
|
$metadata->realNames = $metadata->parent->realNames; |
542
|
|
|
$metadata->allFieldIDs = $metadata->parent->allFieldIDs; |
543
|
|
|
$metadata->allFieldNames = $metadata->parent->allFieldNames; |
544
|
|
|
$metadata->allFieldValueColumns = $metadata->parent->allFieldValueColumns; |
545
|
|
|
$metadata->allFieldTypes = $metadata->parent->allFieldTypes; |
546
|
|
|
$metadata->fieldDescriptions = $metadata->parent->fieldDescriptions; |
547
|
|
|
$metadata->localizedFieldIDs = $metadata->parent->localizedFieldIDs; |
548
|
|
|
$metadata->notLocalizedFieldIDs = $metadata->parent->notLocalizedFieldIDs; |
549
|
|
|
} else { |
550
|
|
|
throw new ParentEntityNotFound($metadata->parentID); |
551
|
|
|
} |
552
|
|
|
} |
553
|
|
|
|
554
|
|
|
// Store entity original data |
555
|
|
|
$metadata->entityRealName = $structureRow['Name']; |
556
|
|
|
$metadata->entityID = $structureRow['StructureID']; |
557
|
|
|
$metadata->className = $this->fullEntityName($metadata->entity, __NAMESPACE__); |
558
|
|
|
|
559
|
|
|
// Get old AR collections of metadata |
560
|
|
|
$metadata->arSelect = \samson\activerecord\material::$_sql_select; |
561
|
|
|
$metadata->arAttributes = \samson\activerecord\material::$_attributes; |
562
|
|
|
$metadata->arMap = \samson\activerecord\material::$_map; |
563
|
|
|
$metadata->arFrom = \samson\activerecord\material::$_sql_from; |
564
|
|
|
$metadata->arGroup = \samson\activerecord\material::$_own_group; |
565
|
|
|
$metadata->arRelationAlias = \samson\activerecord\material::$_relation_alias; |
566
|
|
|
$metadata->arRelationType = \samson\activerecord\material::$_relation_type; |
567
|
|
|
$metadata->arRelations = \samson\activerecord\material::$_relations; |
568
|
|
|
|
569
|
|
|
// Add SamsonCMS material needed data |
570
|
|
|
$metadata->arSelect['this'] = ' STRAIGHT_JOIN ' . $metadata->arSelect['this']; |
571
|
|
|
$metadata->arFrom['this'] .= "\n" . |
572
|
|
|
'LEFT JOIN ' . dbMySQLConnector::$prefix . 'materialfield as _mf |
573
|
|
|
ON ' . dbMySQLConnector::$prefix . 'material.MaterialID = _mf.MaterialID'; |
574
|
|
|
$metadata->arGroup[] = dbMySQLConnector::$prefix . 'material.MaterialID'; |
575
|
|
|
|
576
|
|
|
// Iterate entity fields |
577
|
|
|
foreach ($this->navigationFields($structureRow['StructureID']) as $fieldID => $fieldRow) { |
578
|
|
|
// Get camelCase and transliterated field name |
579
|
|
|
$fieldName = $this->fieldName($fieldRow['Name']); |
580
|
|
|
|
581
|
|
|
// Store field metadata |
582
|
|
|
$metadata->realNames[$fieldRow['Name']] = $fieldName; |
583
|
|
|
$metadata->allFieldIDs[$fieldID] = $fieldName; |
584
|
|
|
$metadata->allFieldNames[$fieldName] = $fieldID; |
585
|
|
|
$metadata->allFieldValueColumns[$fieldID] = Field::valueColumn($fieldRow[Field::F_TYPE]); |
586
|
|
|
$metadata->allFieldTypes[$fieldID] = Field::phpType($fieldRow['Type']); |
587
|
|
|
$metadata->fieldDescriptions[$fieldID] = $fieldRow['Description'] . ', ' . $fieldRow['Name'] . '#' . $fieldID; |
588
|
|
|
|
589
|
|
|
// Fill localization fields collections |
590
|
|
|
if ($fieldRow[Field::F_LOCALIZED] === 1) { |
591
|
|
|
$metadata->localizedFieldIDs[$fieldID] = $fieldName; |
592
|
|
|
} else { |
593
|
|
|
$metadata->notLocalizedFieldIDs[$fieldID] = $fieldName; |
594
|
|
|
} |
595
|
|
|
|
596
|
|
|
// Set old AR collections of metadata |
597
|
|
|
$metadata->arAttributes[$fieldName] = $fieldName; |
598
|
|
|
$metadata->arMap[$fieldName] = dbMySQLConnector::$prefix . 'material.' . $fieldName; |
599
|
|
|
|
600
|
|
|
// Add additional field column to entity query |
601
|
|
|
$equal = '((_mf.FieldID = ' . $fieldID . ')&&(_mf.locale ' . ($fieldRow['local'] ? ' = "@locale"' : 'IS NULL') . '))'; |
602
|
|
|
$metadata->arSelect['this'] .= "\n\t\t" . ',MAX(IF(' . $equal . ', _mf.`' . Field::valueColumn($fieldRow['Type']) . '`, NULL)) as `' . $fieldName . '`'; |
603
|
|
|
} |
604
|
|
|
|
605
|
|
|
// Store metadata by entity identifier |
606
|
|
|
$this->metadata[$structureRow['StructureID']] = $metadata; |
607
|
|
|
} |
608
|
|
|
} |
609
|
|
|
|
610
|
|
|
// Iterate all entities metadata |
611
|
|
|
foreach ($this->metadata as $metadata) { |
612
|
|
|
|
613
|
|
|
// Generate classes of default type |
614
|
|
|
if ($metadata->type === Metadata::TYPE_DEFAULT) { |
615
|
|
|
|
616
|
|
|
$classes .= $this->createEntityClass($metadata); |
617
|
|
|
$classes .= $this->createQueryClass($metadata); |
618
|
|
|
$classes .= $this->createQueryClass($metadata, 'Collection', '\samsoncms\api\renderable\Collection'); |
619
|
|
|
|
620
|
|
|
// Generate classes of table type |
621
|
|
|
} else if ($metadata->type === Metadata::TYPE_TABLE) { |
622
|
|
|
|
623
|
|
|
$classes .= $this->createTableRowClass($metadata); |
624
|
|
|
$classes .= $this->createTableClass($metadata); |
625
|
|
|
} |
626
|
|
|
} |
627
|
|
|
|
628
|
|
|
// Make correct code formatting |
629
|
|
|
return str_replace("\t", ' ', $classes); |
630
|
|
|
} |
631
|
|
|
|
632
|
|
|
/** |
633
|
|
|
* Generator constructor. |
634
|
|
|
* @param DatabaseInterface $database Database instance |
635
|
|
|
*/ |
636
|
|
|
public function __construct(DatabaseInterface $database) |
637
|
|
|
{ |
638
|
|
|
$this->generator = new \samsonphp\generator\Generator(); |
639
|
|
|
$this->database = $database; |
640
|
|
|
} |
641
|
|
|
} |
642
|
|
|
|
643
|
|
|
//[PHPCOMPRESSOR(remove,end)] |
644
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.