1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by PhpStorm. |
4
|
|
|
* User: VITALYIEGOROV |
5
|
|
|
* Date: 09.12.15 |
6
|
|
|
* Time: 14:34 |
7
|
|
|
*/ |
8
|
|
|
namespace samsoncms\api; |
9
|
|
|
|
10
|
|
|
use samson\activerecord\dbMySQLConnector; |
11
|
|
|
use samson\cms\CMSMaterial; |
12
|
|
|
use samsoncms\api\query\Generic; |
13
|
|
|
use samsonframework\orm\DatabaseInterface; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Entity classes generator. |
17
|
|
|
* @package samsoncms\api |
18
|
|
|
*/ |
19
|
|
|
class Generator |
20
|
|
|
{ |
21
|
|
|
/** @var DatabaseInterface */ |
22
|
|
|
protected $database; |
23
|
|
|
|
24
|
|
|
/** @var \samsonphp\generator\Generator */ |
25
|
|
|
protected $generator; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Transliterate string to english. |
29
|
|
|
* |
30
|
|
|
* @param string $string Source string |
31
|
|
|
* @return string Transliterated string |
32
|
|
|
*/ |
33
|
|
|
protected function transliterated($string) |
34
|
|
|
{ |
35
|
|
|
return str_replace( |
36
|
|
|
' ', |
37
|
|
|
'', |
38
|
|
|
ucwords(iconv("UTF-8", "UTF-8//IGNORE", strtr($string, array( |
39
|
|
|
"'" => "", |
40
|
|
|
"`" => "", |
41
|
|
|
"-" => " ", |
42
|
|
|
"_" => " ", |
43
|
|
|
"а" => "a", "А" => "a", |
44
|
|
|
"б" => "b", "Б" => "b", |
45
|
|
|
"в" => "v", "В" => "v", |
46
|
|
|
"г" => "g", "Г" => "g", |
47
|
|
|
"д" => "d", "Д" => "d", |
48
|
|
|
"е" => "e", "Е" => "e", |
49
|
|
|
"ж" => "zh", "Ж" => "zh", |
50
|
|
|
"з" => "z", "З" => "z", |
51
|
|
|
"и" => "i", "И" => "i", |
52
|
|
|
"й" => "y", "Й" => "y", |
53
|
|
|
"к" => "k", "К" => "k", |
54
|
|
|
"л" => "l", "Л" => "l", |
55
|
|
|
"м" => "m", "М" => "m", |
56
|
|
|
"н" => "n", "Н" => "n", |
57
|
|
|
"о" => "o", "О" => "o", |
58
|
|
|
"п" => "p", "П" => "p", |
59
|
|
|
"р" => "r", "Р" => "r", |
60
|
|
|
"с" => "s", "С" => "s", |
61
|
|
|
"т" => "t", "Т" => "t", |
62
|
|
|
"у" => "u", "У" => "u", |
63
|
|
|
"ф" => "f", "Ф" => "f", |
64
|
|
|
"х" => "h", "Х" => "h", |
65
|
|
|
"ц" => "c", "Ц" => "c", |
66
|
|
|
"ч" => "ch", "Ч" => "ch", |
67
|
|
|
"ш" => "sh", "Ш" => "sh", |
68
|
|
|
"щ" => "sch", "Щ" => "sch", |
69
|
|
|
"ъ" => "", "Ъ" => "", |
70
|
|
|
"ы" => "y", "Ы" => "y", |
71
|
|
|
"ь" => "", "Ь" => "", |
72
|
|
|
"э" => "e", "Э" => "e", |
73
|
|
|
"ю" => "yu", "Ю" => "yu", |
74
|
|
|
"я" => "ya", "Я" => "ya", |
75
|
|
|
"і" => "i", "І" => "i", |
76
|
|
|
"ї" => "yi", "Ї" => "yi", |
77
|
|
|
"є" => "e", "Є" => "e" |
78
|
|
|
) |
79
|
|
|
) |
80
|
|
|
) |
81
|
|
|
) |
82
|
|
|
); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Get class constant name by its value. |
87
|
|
|
* |
88
|
|
|
* @param string $value Constant value |
89
|
|
|
* @param string $className Class name |
90
|
|
|
* @return string Full constant name |
91
|
|
|
*/ |
92
|
|
|
protected function constantNameByValue($value, $className = Field::ENTITY) |
93
|
|
|
{ |
94
|
|
|
// Get array where class constants are values and their values are keys |
95
|
|
|
$nameByValue = array_flip((new \ReflectionClass($className))->getConstants()); |
96
|
|
|
|
97
|
|
|
// Try to find constant by its value |
98
|
|
|
if (isset($nameByValue[$value])) { |
99
|
|
|
// Return constant name |
100
|
|
|
return $nameByValue[$value]; |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Get correct entity name. |
106
|
|
|
* |
107
|
|
|
* @param string $navigationName Original navigation entity name |
108
|
|
|
* @return string Correct PHP-supported entity name |
109
|
|
|
*/ |
110
|
|
|
protected function entityName($navigationName) |
111
|
|
|
{ |
112
|
|
|
return ucfirst($this->getValidName($this->transliterated($navigationName))); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Remove all wrong characters from entity name |
117
|
|
|
* |
118
|
|
|
* @param string $navigationName Original navigation entity name |
119
|
|
|
* @return string Correct PHP-supported entity name |
120
|
|
|
*/ |
121
|
|
|
protected function getValidName($navigationName) |
122
|
|
|
{ |
123
|
|
|
return preg_replace('/(^\d*)|([^\w\d_])/', '', $navigationName); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Get correct full entity name with name space. |
128
|
|
|
* |
129
|
|
|
* @param string $navigationName Original navigation entity name |
130
|
|
|
* @param string $namespace Namespace |
131
|
|
|
* @return string Correct PHP-supported entity name |
132
|
|
|
*/ |
133
|
|
|
protected function fullEntityName($navigationName, $namespace = __NAMESPACE__) |
134
|
|
|
{ |
135
|
|
|
return str_replace('\\', '\\\\' , '\\'.$namespace.'\\'.$this->entityName($navigationName)); |
|
|
|
|
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Get correct field name. |
140
|
|
|
* |
141
|
|
|
* @param string $fieldName Original field name |
142
|
|
|
* @return string Correct PHP-supported field name |
143
|
|
|
*/ |
144
|
|
|
protected function fieldName($fieldName) |
145
|
|
|
{ |
146
|
|
|
return $fieldName = lcfirst($this->transliterated($fieldName)); |
|
|
|
|
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Get additional field type in form of Field constant name |
151
|
|
|
* by database additional field type identifier. |
152
|
|
|
* |
153
|
|
|
* @param integer $fieldType Additional field type identifier |
154
|
|
|
* @return string Additional field type constant |
155
|
|
|
*/ |
156
|
|
|
protected function additionalFieldType($fieldType) |
157
|
|
|
{ |
158
|
|
|
return 'Field::'.$this->constantNameByValue($fieldType); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Generate Query::where() analog for specific field. |
163
|
|
|
* |
164
|
|
|
* @param string $fieldName Field name |
165
|
|
|
* @param string $fieldId Field primary identifier |
166
|
|
|
* @param string $fieldType Field PHP type |
167
|
|
|
* @return string Generated PHP method code |
168
|
|
|
*/ |
169
|
|
View Code Duplication |
protected function generateFieldConditionMethod($fieldName, $fieldId, $fieldType) |
|
|
|
|
170
|
|
|
{ |
171
|
|
|
$code = "\n\t" . '/**'; |
172
|
|
|
$code .= "\n\t" . ' * Add '.$fieldName.'(#' . $fieldId . ') field query condition.'; |
173
|
|
|
$code .= "\n\t" . ' * @param '.Field::phpType($fieldType).' $value Field value'; |
174
|
|
|
$code .= "\n\t" . ' * @return self Chaining'; |
175
|
|
|
$code .= "\n\t" . ' * @see Generic::where()'; |
176
|
|
|
$code .= "\n\t" . ' */'; |
177
|
|
|
$code .= "\n\t" . 'public function ' . $fieldName . '($value)'; |
178
|
|
|
$code .= "\n\t" . "{"; |
179
|
|
|
$code .= "\n\t\t" . 'return $this->where("'.$fieldName.'", $value);'; |
180
|
|
|
|
181
|
|
|
return $code . "\n\t" . "}"."\n"; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* Generate Query::where() analog for specific field. |
186
|
|
|
* |
187
|
|
|
* @param string $fieldName Field name |
188
|
|
|
* @param string $fieldId Field primary identifier |
189
|
|
|
* @param string $fieldType Field PHP type |
190
|
|
|
* @return string Generated PHP method code |
191
|
|
|
*/ |
192
|
|
View Code Duplication |
protected function generateLocalizedFieldConditionMethod($fieldName, $fieldId, $fieldType) |
|
|
|
|
193
|
|
|
{ |
194
|
|
|
$code = "\n\t" . '/**'; |
195
|
|
|
$code .= "\n\t" . ' * Add '.$fieldName.'(#' . $fieldId . ') field query condition.'; |
196
|
|
|
$code .= "\n\t" . ' * @param '.Field::phpType($fieldType).' $value Field value'; |
197
|
|
|
$code .= "\n\t" . ' * @return self Chaining'; |
198
|
|
|
$code .= "\n\t" . ' * @see Generic::where()'; |
199
|
|
|
$code .= "\n\t" . ' */'; |
200
|
|
|
$code .= "\n\t" . 'public function ' . $fieldName . '($value)'; |
201
|
|
|
$code .= "\n\t" . "{"; |
202
|
|
|
$code .= "\n\t\t" . 'return $this->where("'.$fieldName.'", $value);'; |
203
|
|
|
|
204
|
|
|
return $code . "\n\t" . "}"."\n"; |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* Create entity PHP class code. |
209
|
|
|
* |
210
|
|
|
* @param string $navigationName Original entity name |
211
|
|
|
* @param string $entityName PHP entity name |
212
|
|
|
* @param array $navigationFields Collection of entity additional fields |
213
|
|
|
* @return string Generated entity query PHP class code |
214
|
|
|
*/ |
215
|
|
|
protected function createEntityClass($navigationName, $entityName, $navigationFields) |
216
|
|
|
{ |
217
|
|
|
$this->generator->multicomment(array('"'.$navigationName.'" entity class')); |
218
|
|
|
$this->generator->defclass($entityName, 'Entity'); |
219
|
|
|
|
220
|
|
|
$this->generator->comment('Entity full class name'); |
221
|
|
|
$this->generator->defvar('const ENTITY', $this->fullEntityName($entityName)); |
222
|
|
|
|
223
|
|
|
$this->generator->comment('@var string Not transliterated entity name'); |
224
|
|
|
$this->generator->defvar('protected static $viewName', $navigationName); |
225
|
|
|
|
226
|
|
|
// Get old AR collections of metadata |
227
|
|
|
$select = \samson\activerecord\material::$_sql_select; |
228
|
|
|
$attributes = \samson\activerecord\material::$_attributes; |
229
|
|
|
$map = \samson\activerecord\material::$_map; |
230
|
|
|
$from = \samson\activerecord\material::$_sql_from; |
231
|
|
|
$group = \samson\activerecord\material::$_own_group; |
232
|
|
|
|
233
|
|
|
// Add SamsonCMS material needed data |
234
|
|
|
$select['this'] = ' STRAIGHT_JOIN ' . $select['this']; |
235
|
|
|
$from['this'] .= "\n" . 'LEFT JOIN ' . dbMySQLConnector::$prefix . 'materialfield as _mf on ' . dbMySQLConnector::$prefix . 'material.MaterialID = _mf.MaterialID'; |
236
|
|
|
$group[] = dbMySQLConnector::$prefix . 'material.MaterialID'; |
237
|
|
|
|
238
|
|
|
foreach ($navigationFields as $fieldID => $fieldRow) { |
239
|
|
|
$fieldName = $this->fieldName($fieldRow['Name']); |
240
|
|
|
|
241
|
|
|
$attributes[$fieldName] = $fieldName; |
242
|
|
|
$map[$fieldName] = dbMySQLConnector::$prefix . 'material.' . $fieldName; |
243
|
|
|
|
244
|
|
|
$equal = '((_mf.FieldID = ' . $fieldID . ')&&(_mf.locale ' . ($fieldRow['local'] ? ' = "'.locale().'"' : 'IS NULL').'))'; |
245
|
|
|
|
246
|
|
|
// Save additional field |
247
|
|
|
$select['this'] .= "\n\t\t".',MAX(IF(' . $equal . ', _mf.`' . Field::valueColumn($fieldRow['Type']) . '`, NULL)) as `' . $fieldName . '`'; |
248
|
|
|
|
249
|
|
|
$this->generator->comment(Field::phpType($fieldRow['Type']) . ' '.$fieldRow['Description'].' Field #' . $fieldID . ' variable name'); |
250
|
|
|
$this->generator->defvar('const F_' . strtoupper($fieldName), $fieldName); |
251
|
|
|
$this->generator->comment(Field::phpType($fieldRow['Type']) . ' '.$fieldRow['Description'].' Field #' . $fieldID); |
252
|
|
|
$this->generator->defvar('public $'.$fieldName.';'); |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
$this->generator->defvar('public static $_sql_select', $select); |
|
|
|
|
256
|
|
|
$this->generator->defvar('public static $_attributes', $attributes); |
|
|
|
|
257
|
|
|
$this->generator->defvar('public static $_map', $map); |
|
|
|
|
258
|
|
|
$this->generator->defvar('public static $_sql_from', $from); |
|
|
|
|
259
|
|
|
$this->generator->defvar('public static $_own_group', $group); |
|
|
|
|
260
|
|
|
|
261
|
|
|
$this->generator->endclass(); |
262
|
|
|
|
263
|
|
|
return $this->generator->flush(); |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
/** |
267
|
|
|
* Generate FieldsTable::values() analog for specific field. |
268
|
|
|
* |
269
|
|
|
* @param string $fieldName Field name |
270
|
|
|
* @param string $fieldId Field primary identifier |
271
|
|
|
* @param string $fieldType Field PHP type |
272
|
|
|
* @return string Generated PHP method code |
273
|
|
|
*/ |
274
|
|
|
protected function generateTableFieldMethod($fieldName, $fieldId, $fieldType) |
275
|
|
|
{ |
276
|
|
|
$code = "\n\t" . '/**'; |
277
|
|
|
$code .= "\n\t" . ' * Get table column '.$fieldName.'(#' . $fieldId . ') values.'; |
278
|
|
|
$code .= "\n\t" . ' * @return array Collection('.Field::phpType($fieldType).') of table column values'; |
279
|
|
|
$code .= "\n\t" . ' */'; |
280
|
|
|
$code .= "\n\t" . 'public function ' . $fieldName . '()'; |
281
|
|
|
$code .= "\n\t" . "{"; |
282
|
|
|
$code .= "\n\t\t" . 'return $this->values('.$fieldId.');'; |
283
|
|
|
|
284
|
|
|
return $code . "\n\t" . "}"."\n"; |
285
|
|
|
} |
286
|
|
|
|
287
|
|
|
/** |
288
|
|
|
* Create fields table PHP class code. |
289
|
|
|
* |
290
|
|
|
* @param integer $navigationID Entity navigation identifier |
291
|
|
|
* @param string $navigationName Original entity name |
292
|
|
|
* @param string $entityName PHP entity name |
293
|
|
|
* @param array $navigationFields Collection of entity additional fields |
294
|
|
|
* @return string Generated entity query PHP class code |
295
|
|
|
*/ |
296
|
|
|
protected function createTableClass($navigationID, $navigationName, $entityName, $navigationFields) |
297
|
|
|
{ |
298
|
|
|
$class = "\n"; |
299
|
|
|
$class .= "\n" . '/**'; |
300
|
|
|
$class .= "\n" . ' * Class for getting "'.$navigationName.'" fields table'; |
301
|
|
|
$class .= "\n" . ' */'; |
302
|
|
|
$class .= "\n" . 'class ' . $entityName . ' extends FieldsTable'; |
303
|
|
|
$class .= "\n" . '{'; |
304
|
|
|
|
305
|
|
|
// Iterate additional fields |
306
|
|
|
$constants = ''; |
307
|
|
|
$variables = ''; |
308
|
|
|
$methods = ''; |
309
|
|
|
foreach ($navigationFields as $fieldID => $fieldRow) { |
310
|
|
|
$fieldName = $this->fieldName($fieldRow['Name']); |
311
|
|
|
|
312
|
|
|
$methods .= $this->generateTableFieldMethod( |
313
|
|
|
$fieldName, |
314
|
|
|
$fieldRow[Field::F_PRIMARY], |
315
|
|
|
$fieldRow[Field::F_TYPE] |
316
|
|
|
); |
317
|
|
|
$constants .= "\n\t" . '/** ' . Field::phpType($fieldRow['Type']) . ' '.$fieldRow['Description'].' Field #' . $fieldID . ' variable name */'; |
318
|
|
|
$constants .= "\n\t" . 'const F_' . strtoupper($fieldName) . ' = "'.$fieldName.'";'; |
319
|
|
|
|
320
|
|
|
$variables .= "\n\t" . '/** @var array Collection of '.$fieldRow['Description'].' Field #' . $fieldID . ' values */'; |
321
|
|
|
$variables .= "\n\t" . 'protected $' . $fieldName . ';'; |
322
|
|
|
} |
323
|
|
|
|
324
|
|
|
$class .= $constants; |
325
|
|
|
$class .= "\n\t"; |
326
|
|
|
$class .= "\n\t" . '/** @var array Collection of navigation identifiers */'; |
327
|
|
|
$class .= "\n\t" . 'protected static $navigationIDs = array(' . $navigationID . ');'; |
328
|
|
|
$class .= "\n\t"; |
329
|
|
|
$class .= $variables; |
330
|
|
|
$class .= "\n\t"; |
331
|
|
|
$class .= $methods; |
332
|
|
|
$class .= "\n\t".'/**'; |
333
|
|
|
$class .= "\n\t".' * @param QueryInterface $query Database query instance'; |
334
|
|
|
$class .= "\n\t".' * @param integer $entityID Entity identifier to whom this table belongs'; |
335
|
|
|
$class .= "\n\t".' * @param string $locale Localization identifier'; |
336
|
|
|
$class .= "\n\t".' */'; |
337
|
|
|
$class .= "\n\t".'public function __construct(QueryInterface $query, $entityID, $locale = "")'; |
338
|
|
|
$class .= "\n\t".'{'; |
339
|
|
|
$class .= "\n\t\t".'parent::__construct($query, static::$navigationIDs, $entityID, $locale);'; |
340
|
|
|
$class .= "\n\t".'}'; |
341
|
|
|
$class .= "\n" . '}'; |
342
|
|
|
|
343
|
|
|
return $class; |
344
|
|
|
} |
345
|
|
|
|
346
|
|
|
/** |
347
|
|
|
* Create entity query PHP class code. |
348
|
|
|
* |
349
|
|
|
* @param integer $navigationID Entity navigation identifier |
350
|
|
|
* @param string $navigationName Original entity name |
351
|
|
|
* @param string $entityName PHP entity name |
352
|
|
|
* @param array $navigationFields Collection of entity additional fields |
353
|
|
|
* @return string Generated entity query PHP class code |
354
|
|
|
*/ |
355
|
|
|
protected function createQueryClass($navigationID, $navigationName, $entityName, $navigationFields) |
356
|
|
|
{ |
357
|
|
|
$class = "\n"; |
358
|
|
|
$class .= "\n" . '/**'; |
359
|
|
|
$class .= "\n" . ' * Class for getting "'.$navigationName.'" instances from database'; |
360
|
|
|
$class .= "\n" . ' * @method '.$this->entityName($navigationName).'[] find() Get entities collection'; |
361
|
|
|
$class .= "\n" . ' * @method '.$this->entityName($navigationName).' first() Get entity'; |
362
|
|
|
$class .= "\n" . ' * @method '.$entityName.' where($fieldName, $fieldValue = null, $fieldRelation = ArgumentInterface::EQUAL)'; |
363
|
|
|
$class .= "\n" . ' * @method '.$entityName.' primary($value) Query for chaining'; |
364
|
|
|
$class .= "\n" . ' * @method '.$entityName.' identifier($value) Query for chaining'; |
365
|
|
|
$class .= "\n" . ' * @method '.$entityName.' created($value) Query for chaining'; |
366
|
|
|
$class .= "\n" . ' * @method '.$entityName.' modified($value) Query for chaining'; |
367
|
|
|
$class .= "\n" . ' * @method '.$entityName.' published($value) Query for chaining'; |
368
|
|
|
$class .= "\n" . ' */'; |
369
|
|
|
$class .= "\n" . 'class ' . $entityName . ' extends \samsoncms\api\query\Entity'; |
370
|
|
|
$class .= "\n" . '{'; |
371
|
|
|
|
372
|
|
|
// Iterate additional fields |
373
|
|
|
$localizedFieldIDs = array(); |
374
|
|
|
$notLocalizedFieldIDs = array(); |
375
|
|
|
$allFieldIDs = array(); |
376
|
|
|
$allFieldNames = array(); |
377
|
|
|
$allFieldValueColumns = array(); |
378
|
|
|
foreach ($navigationFields as $fieldID => $fieldRow) { |
379
|
|
|
$fieldName = $this->fieldName($fieldRow['Name']); |
380
|
|
|
|
381
|
|
|
// TODO: Add different method generation depending on their field type |
382
|
|
|
$class .= $this->generateFieldConditionMethod( |
383
|
|
|
$fieldName, |
384
|
|
|
$fieldRow[Field::F_PRIMARY], |
385
|
|
|
$fieldRow[Field::F_TYPE] |
386
|
|
|
); |
387
|
|
|
|
388
|
|
|
// Store field metadata |
389
|
|
|
$allFieldIDs[] = '"' . $fieldID . '" => "' . $fieldName . '"'; |
390
|
|
|
$allFieldNames[] = '"' . $fieldName . '" => "' . $fieldID . '"'; |
391
|
|
|
$allFieldValueColumns[] = '"' . $fieldID . '" => "' . Field::valueColumn($fieldRow[Field::F_TYPE]) . '"'; |
392
|
|
|
if ($fieldRow[Field::F_LOCALIZED] == 1) { |
393
|
|
|
$localizedFieldIDs[] = '"' . $fieldID . '" => "' . $fieldName . '"'; |
394
|
|
|
} else { |
395
|
|
|
$notLocalizedFieldIDs[] = '"' . $fieldID . '" => "' . $fieldName . '"'; |
396
|
|
|
} |
397
|
|
|
} |
398
|
|
|
|
399
|
|
|
$class .= "\n\t"; |
400
|
|
|
$class .= "\n\t" . '/** @var string Not transliterated entity name */'; |
401
|
|
|
$class .= "\n\t" . 'protected static $identifier = "'.$this->fullEntityName($navigationName).'";'; |
402
|
|
|
$class .= "\n\t" . '/** @var array Collection of navigation identifiers */'; |
403
|
|
|
$class .= "\n\t" . 'protected static $navigationIDs = array(' . $navigationID . ');'; |
404
|
|
|
$class .= "\n\t" . '/** @var array Collection of localized additional fields identifiers */'; |
405
|
|
|
$class .= "\n\t" . 'protected static $localizedFieldIDs = array(' . "\n\t\t". implode(','."\n\t\t", $localizedFieldIDs) . "\n\t".');'; |
406
|
|
|
$class .= "\n\t" . '/** @var array Collection of NOT localized additional fields identifiers */'; |
407
|
|
|
$class .= "\n\t" . 'protected static $notLocalizedFieldIDs = array(' . "\n\t\t". implode(','."\n\t\t", $notLocalizedFieldIDs) . "\n\t".');'; |
408
|
|
|
$class .= "\n\t" . '/** @var array Collection of all additional fields identifiers */'; |
409
|
|
|
$class .= "\n\t" . 'protected static $fieldIDs = array(' . "\n\t\t". implode(','."\n\t\t", $allFieldIDs) . "\n\t".');'; |
410
|
|
|
$class .= "\n\t" . '/** @var array Collection of additional fields value column names */'; |
411
|
|
|
$class .= "\n\t" . 'protected static $fieldValueColumns = array(' . "\n\t\t". implode(','."\n\t\t", $allFieldValueColumns) . "\n\t".');'; |
412
|
|
|
$class .= "\n\t" . '/** @var array Collection of additional field names */'; |
413
|
|
|
$class .= "\n\t" . 'public static $fieldNames = array(' . "\n\t\t". implode(','."\n\t\t", $allFieldNames) . "\n\t".');'; |
414
|
|
|
$class .= "\n" . '}'; |
415
|
|
|
|
416
|
|
|
// Replace tabs with spaces |
417
|
|
|
return $class; |
418
|
|
|
} |
419
|
|
|
|
420
|
|
|
/** @return string Entity state hash */ |
421
|
|
|
public function entityHash() |
422
|
|
|
{ |
423
|
|
|
// Получим информацию о всех таблицах из БД |
424
|
|
|
return md5(serialize($this->database->fetch( |
425
|
|
|
'SELECT `TABLES`.`TABLE_NAME` as `TABLE_NAME` |
426
|
|
|
FROM `information_schema`.`TABLES` as `TABLES` |
427
|
|
|
WHERE `TABLES`.`TABLE_SCHEMA`="' . $this->database->database() . '";' |
428
|
|
|
))); |
429
|
|
|
} |
430
|
|
|
|
431
|
|
|
/** @return array Get collection of navigation objects */ |
432
|
|
|
protected function entityNavigations($type = 0) |
433
|
|
|
{ |
434
|
|
|
return $this->database->fetch(' |
435
|
|
|
SELECT * FROM `structure` |
436
|
|
|
WHERE `Active` = "1" AND `Type` = "'.$type.'"' |
437
|
|
|
); |
438
|
|
|
} |
439
|
|
|
|
440
|
|
|
/** @return array Collection of navigation additional fields */ |
441
|
|
|
protected function navigationFields($navigationID) |
442
|
|
|
{ |
443
|
|
|
$return = array(); |
444
|
|
|
// TODO: Optimize queries make one single query with only needed data |
445
|
|
|
foreach ($this->database->fetch('SELECT * FROM `structurefield` WHERE `StructureID` = "' . $navigationID . '" AND `Active` = "1"') as $fieldStructureRow) { |
446
|
|
|
foreach ($this->database->fetch('SELECT * FROM `field` WHERE `FieldID` = "' . $fieldStructureRow['FieldID'] . '"') as $fieldRow) { |
447
|
|
|
$return[$fieldRow['FieldID']] = $fieldRow; |
448
|
|
|
} |
449
|
|
|
} |
450
|
|
|
|
451
|
|
|
return $return; |
452
|
|
|
} |
453
|
|
|
|
454
|
|
|
/** |
455
|
|
|
* Generate entity classes. |
456
|
|
|
* |
457
|
|
|
* @param string $namespace Base namespace for generated classes |
458
|
|
|
* @return string Generated PHP code for entity classes |
459
|
|
|
*/ |
460
|
|
|
public function createEntityClasses($namespace = __NAMESPACE__) |
461
|
|
|
{ |
462
|
|
|
$classes = "\n" . 'namespace ' . $namespace . ';'; |
463
|
|
|
$classes .= "\n"; |
464
|
|
|
$classes .= "\n" . 'use '.$namespace.'\Field;'; |
465
|
|
|
$classes .= "\n" . 'use '.$namespace.'\FieldsTable;'; |
466
|
|
|
$classes .= "\n" . 'use \samsonframework\orm\ArgumentInterface;'; |
467
|
|
|
$classes .= "\n" . 'use \samsonframework\orm\QueryInterface;'; |
468
|
|
|
|
469
|
|
|
// Iterate all structures |
470
|
|
|
foreach ($this->entityNavigations() as $structureRow) { |
471
|
|
|
$navigationFields = $this->navigationFields($structureRow['StructureID']); |
472
|
|
|
$entityName = $this->entityName($structureRow['Name']); |
473
|
|
|
|
474
|
|
|
$classes .= $this->createEntityClass( |
475
|
|
|
$structureRow['Name'], |
476
|
|
|
$entityName, |
477
|
|
|
$navigationFields |
478
|
|
|
); |
479
|
|
|
|
480
|
|
|
$classes .= $this->createQueryClass( |
481
|
|
|
$structureRow['StructureID'], |
482
|
|
|
$structureRow['Name'], |
483
|
|
|
$entityName.'Query', |
484
|
|
|
$navigationFields |
485
|
|
|
); |
486
|
|
|
} |
487
|
|
|
|
488
|
|
|
// Iterate table structures |
489
|
|
|
foreach ($this->entityNavigations(2) as $structureRow) { |
490
|
|
|
$navigationFields = $this->navigationFields($structureRow['StructureID']); |
491
|
|
|
$entityName = $this->entityName($structureRow['Name']); |
492
|
|
|
|
493
|
|
|
$classes .= $this->createTableClass( |
494
|
|
|
$structureRow['StructureID'], |
495
|
|
|
$structureRow['Name'], |
496
|
|
|
$entityName.'Table', |
497
|
|
|
$navigationFields |
498
|
|
|
); |
499
|
|
|
|
500
|
|
|
} |
501
|
|
|
|
502
|
|
|
// Make correct code formatting |
503
|
|
|
return str_replace("\t", ' ', $classes); |
504
|
|
|
} |
505
|
|
|
|
506
|
|
|
/** |
507
|
|
|
* Generator constructor. |
508
|
|
|
* @param DatabaseInterface $database Database instance |
509
|
|
|
*/ |
510
|
|
|
public function __construct(DatabaseInterface $database) |
511
|
|
|
{ |
512
|
|
|
$this->generator = new \samsonphp\generator\Generator(__NAMESPACE__); |
513
|
|
|
$this->database = $database; |
514
|
|
|
} |
515
|
|
|
} |
516
|
|
|
|