|
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->transliterated($navigationName)); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* Get correct full entity name with name space. |
|
117
|
|
|
* |
|
118
|
|
|
* @param string $navigationName Original navigation entity name |
|
119
|
|
|
* @param string $namespace Namespace |
|
120
|
|
|
* @return string Correct PHP-supported entity name |
|
121
|
|
|
*/ |
|
122
|
|
|
protected function fullEntityName($navigationName, $namespace = __NAMESPACE__) |
|
123
|
|
|
{ |
|
124
|
|
|
return str_replace('\\', '\\\\' , '\\'.$namespace.'\\'.$this->entityName($navigationName)); |
|
|
|
|
|
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* Get correct field name. |
|
129
|
|
|
* |
|
130
|
|
|
* @param string $fieldName Original field name |
|
131
|
|
|
* @return string Correct PHP-supported field name |
|
132
|
|
|
*/ |
|
133
|
|
|
protected function fieldName($fieldName) |
|
134
|
|
|
{ |
|
135
|
|
|
return $fieldName = lcfirst($this->transliterated($fieldName)); |
|
|
|
|
|
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* Get additional field type in form of Field constant name |
|
140
|
|
|
* by database additional field type identifier. |
|
141
|
|
|
* |
|
142
|
|
|
* @param integer $fieldType Additional field type identifier |
|
143
|
|
|
* @return string Additional field type constant |
|
144
|
|
|
*/ |
|
145
|
|
|
protected function additionalFieldType($fieldType) |
|
146
|
|
|
{ |
|
147
|
|
|
return 'Field::'.$this->constantNameByValue($fieldType); |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* Generate Query::where() analog for specific field. |
|
152
|
|
|
* |
|
153
|
|
|
* @param string $fieldName Field name |
|
154
|
|
|
* @param string $fieldId Field primary identifier |
|
155
|
|
|
* @param string $fieldType Field PHP type |
|
156
|
|
|
* @return string Generated PHP method code |
|
157
|
|
|
*/ |
|
158
|
|
View Code Duplication |
protected function generateFieldConditionMethod($fieldName, $fieldId, $fieldType) |
|
|
|
|
|
|
159
|
|
|
{ |
|
160
|
|
|
$code = "\n\t" . '/**'; |
|
161
|
|
|
$code .= "\n\t" . ' * Add '.$fieldName.'(#' . $fieldId . ') field query condition.'; |
|
162
|
|
|
$code .= "\n\t" . ' * @param '.Field::phpType($fieldType).' $value Field value'; |
|
163
|
|
|
$code .= "\n\t" . ' * @return self Chaining'; |
|
164
|
|
|
$code .= "\n\t" . ' * @see Generic::where()'; |
|
165
|
|
|
$code .= "\n\t" . ' */'; |
|
166
|
|
|
$code .= "\n\t" . 'public function ' . $fieldName . '($value)'; |
|
167
|
|
|
$code .= "\n\t" . "{"; |
|
168
|
|
|
$code .= "\n\t\t" . 'return $this->where("'.$fieldName.'", $value);'; |
|
169
|
|
|
|
|
170
|
|
|
return $code . "\n\t" . "}"."\n"; |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
/** |
|
174
|
|
|
* Generate Query::where() analog for specific field. |
|
175
|
|
|
* |
|
176
|
|
|
* @param string $fieldName Field name |
|
177
|
|
|
* @param string $fieldId Field primary identifier |
|
178
|
|
|
* @param string $fieldType Field PHP type |
|
179
|
|
|
* @return string Generated PHP method code |
|
180
|
|
|
*/ |
|
181
|
|
View Code Duplication |
protected function generateLocalizedFieldConditionMethod($fieldName, $fieldId, $fieldType) |
|
|
|
|
|
|
182
|
|
|
{ |
|
183
|
|
|
$code = "\n\t" . '/**'; |
|
184
|
|
|
$code .= "\n\t" . ' * Add '.$fieldName.'(#' . $fieldId . ') field query condition.'; |
|
185
|
|
|
$code .= "\n\t" . ' * @param '.Field::phpType($fieldType).' $value Field value'; |
|
186
|
|
|
$code .= "\n\t" . ' * @return self Chaining'; |
|
187
|
|
|
$code .= "\n\t" . ' * @see Generic::where()'; |
|
188
|
|
|
$code .= "\n\t" . ' */'; |
|
189
|
|
|
$code .= "\n\t" . 'public function ' . $fieldName . '($value)'; |
|
190
|
|
|
$code .= "\n\t" . "{"; |
|
191
|
|
|
$code .= "\n\t\t" . 'return $this->where("'.$fieldName.'", $value);'; |
|
192
|
|
|
|
|
193
|
|
|
return $code . "\n\t" . "}"."\n"; |
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
|
|
/** |
|
197
|
|
|
* Create entity PHP class code. |
|
198
|
|
|
* |
|
199
|
|
|
* @param string $navigationName Original entity name |
|
200
|
|
|
* @param string $entityName PHP entity name |
|
201
|
|
|
* @param array $navigationFields Collection of entity additional fields |
|
202
|
|
|
* @return string Generated entity query PHP class code |
|
203
|
|
|
*/ |
|
204
|
|
|
protected function createEntityClass($navigationName, $entityName, $navigationFields) |
|
205
|
|
|
{ |
|
206
|
|
|
$this->generator->multicomment(array('"'.$navigationName.'" entity class')); |
|
207
|
|
|
$this->generator->defclass($entityName, 'Entity'); |
|
208
|
|
|
|
|
209
|
|
|
$this->generator->comment('Entity full class name'); |
|
210
|
|
|
$this->generator->defvar('const ENTITY', $this->fullEntityName($entityName)); |
|
211
|
|
|
|
|
212
|
|
|
$this->generator->comment('@var string Not transliterated entity name'); |
|
213
|
|
|
$this->generator->defvar('protected static $viewName', $navigationName); |
|
214
|
|
|
|
|
215
|
|
|
// Get old AR collections of metadata |
|
216
|
|
|
$select = \samson\activerecord\material::$_sql_select; |
|
217
|
|
|
$attributes = \samson\activerecord\material::$_attributes; |
|
218
|
|
|
$map = \samson\activerecord\material::$_map; |
|
219
|
|
|
$from = \samson\activerecord\material::$_sql_from; |
|
220
|
|
|
$group = \samson\activerecord\material::$_own_group; |
|
221
|
|
|
|
|
222
|
|
|
// Add SamsonCMS material needed data |
|
223
|
|
|
$select['this'] = ' STRAIGHT_JOIN ' . $select['this']; |
|
224
|
|
|
$from['this'] .= "\n" . 'LEFT JOIN ' . dbMySQLConnector::$prefix . 'materialfield as _mf on ' . dbMySQLConnector::$prefix . 'material.MaterialID = _mf.MaterialID'; |
|
225
|
|
|
$group[] = dbMySQLConnector::$prefix . 'material.MaterialID'; |
|
226
|
|
|
|
|
227
|
|
|
foreach ($navigationFields as $fieldID => $fieldRow) { |
|
228
|
|
|
$fieldName = $this->fieldName($fieldRow['Name']); |
|
229
|
|
|
|
|
230
|
|
|
$attributes[$fieldName] = $fieldName; |
|
231
|
|
|
$map[$fieldName] = dbMySQLConnector::$prefix . 'material.' . $fieldName; |
|
232
|
|
|
|
|
233
|
|
|
$equal = '((_mf.FieldID = ' . $fieldID . ')&&(_mf.locale ' . ($fieldRow['local'] ? ' = "'.locale().'"' : 'IS NULL').'))'; |
|
234
|
|
|
|
|
235
|
|
|
// Save additional field |
|
236
|
|
|
$select['this'] .= "\n\t\t".',MAX(IF(' . $equal . ', _mf.`' . Field::valueColumn($fieldRow['Type']) . '`, NULL)) as `' . $fieldName . '`'; |
|
237
|
|
|
|
|
238
|
|
|
$this->generator->comment(Field::phpType($fieldRow['Type']) . ' '.$fieldRow['Description'].' Field #' . $fieldID . ' variable name'); |
|
239
|
|
|
$this->generator->defvar('const F_' . strtoupper($fieldName), $fieldName); |
|
240
|
|
|
$this->generator->comment(Field::phpType($fieldRow['Type']) . ' '.$fieldRow['Description'].' Field #' . $fieldID); |
|
241
|
|
|
$this->generator->defvar('public $'.$fieldName.';'); |
|
242
|
|
|
} |
|
243
|
|
|
|
|
244
|
|
|
$this->generator->defvar('public static $_sql_select', $select); |
|
|
|
|
|
|
245
|
|
|
$this->generator->defvar('public static $_attributes', $attributes); |
|
|
|
|
|
|
246
|
|
|
$this->generator->defvar('public static $_map', $map); |
|
|
|
|
|
|
247
|
|
|
$this->generator->defvar('public static $_sql_from', $from); |
|
|
|
|
|
|
248
|
|
|
$this->generator->defvar('public static $_own_group', $group); |
|
|
|
|
|
|
249
|
|
|
|
|
250
|
|
|
$this->generator->endclass(); |
|
251
|
|
|
|
|
252
|
|
|
return $this->generator->flush(); |
|
253
|
|
|
} |
|
254
|
|
|
|
|
255
|
|
|
/** |
|
256
|
|
|
* Generate FieldsTable::values() analog for specific field. |
|
257
|
|
|
* |
|
258
|
|
|
* @param string $fieldName Field name |
|
259
|
|
|
* @param string $fieldId Field primary identifier |
|
260
|
|
|
* @param string $fieldType Field PHP type |
|
261
|
|
|
* @return string Generated PHP method code |
|
262
|
|
|
*/ |
|
263
|
|
|
protected function generateTableFieldMethod($fieldName, $fieldId, $fieldType) |
|
264
|
|
|
{ |
|
265
|
|
|
$code = "\n\t" . '/**'; |
|
266
|
|
|
$code .= "\n\t" . ' * Get table column '.$fieldName.'(#' . $fieldId . ') values.'; |
|
267
|
|
|
$code .= "\n\t" . ' * @return array Collection('.Field::phpType($fieldType).') of table column values'; |
|
268
|
|
|
$code .= "\n\t" . ' */'; |
|
269
|
|
|
$code .= "\n\t" . 'public function ' . $fieldName . '()'; |
|
270
|
|
|
$code .= "\n\t" . "{"; |
|
271
|
|
|
$code .= "\n\t\t" . 'return $this->values('.$fieldId.');'; |
|
272
|
|
|
|
|
273
|
|
|
return $code . "\n\t" . "}"."\n"; |
|
274
|
|
|
} |
|
275
|
|
|
|
|
276
|
|
|
/** |
|
277
|
|
|
* Create fields table PHP class code. |
|
278
|
|
|
* |
|
279
|
|
|
* @param integer $navigationID Entity navigation identifier |
|
280
|
|
|
* @param string $navigationName Original entity name |
|
281
|
|
|
* @param string $entityName PHP entity name |
|
282
|
|
|
* @param array $navigationFields Collection of entity additional fields |
|
283
|
|
|
* @return string Generated entity query PHP class code |
|
284
|
|
|
*/ |
|
285
|
|
|
protected function createTableClass($navigationID, $navigationName, $entityName, $navigationFields) |
|
286
|
|
|
{ |
|
287
|
|
|
$class = "\n"; |
|
288
|
|
|
$class .= "\n" . '/**'; |
|
289
|
|
|
$class .= "\n" . ' * Class for getting "'.$navigationName.'" fields table'; |
|
290
|
|
|
$class .= "\n" . ' */'; |
|
291
|
|
|
$class .= "\n" . 'class ' . $entityName . ' extends FieldsTable'; |
|
292
|
|
|
$class .= "\n" . '{'; |
|
293
|
|
|
|
|
294
|
|
|
// Iterate additional fields |
|
295
|
|
|
$constants = ''; |
|
296
|
|
|
$variables = ''; |
|
297
|
|
|
$methods = ''; |
|
298
|
|
|
foreach ($navigationFields as $fieldID => $fieldRow) { |
|
299
|
|
|
$fieldName = $this->fieldName($fieldRow['Name']); |
|
300
|
|
|
|
|
301
|
|
|
$methods .= $this->generateTableFieldMethod( |
|
302
|
|
|
$fieldName, |
|
303
|
|
|
$fieldRow[Field::F_PRIMARY], |
|
304
|
|
|
$fieldRow[Field::F_TYPE] |
|
305
|
|
|
); |
|
306
|
|
|
$constants .= "\n\t" . '/** ' . Field::phpType($fieldRow['Type']) . ' '.$fieldRow['Description'].' Field #' . $fieldID . ' variable name */'; |
|
307
|
|
|
$constants .= "\n\t" . 'const F_' . strtoupper($fieldName) . ' = "'.$fieldName.'";'; |
|
308
|
|
|
|
|
309
|
|
|
$variables .= "\n\t" . '/** @var array Collection of '.$fieldRow['Description'].' Field #' . $fieldID . ' values */'; |
|
310
|
|
|
$variables .= "\n\t" . 'protected $' . $fieldName . ';'; |
|
311
|
|
|
} |
|
312
|
|
|
|
|
313
|
|
|
$class .= $constants; |
|
314
|
|
|
$class .= "\n\t"; |
|
315
|
|
|
$class .= "\n\t" . '/** @var array Collection of navigation identifiers */'; |
|
316
|
|
|
$class .= "\n\t" . 'protected static $navigationIDs = array(' . $navigationID . ');'; |
|
317
|
|
|
$class .= "\n\t"; |
|
318
|
|
|
$class .= $variables; |
|
319
|
|
|
$class .= "\n\t"; |
|
320
|
|
|
$class .= $methods; |
|
321
|
|
|
$class .= "\n\t".'/**'; |
|
322
|
|
|
$class .= "\n\t".' * @param QueryInterface $query Database query instance'; |
|
323
|
|
|
$class .= "\n\t".' * @param integer $entityID Entity identifier to whom this table belongs'; |
|
324
|
|
|
$class .= "\n\t".' * @param string $locale Localization identifier'; |
|
325
|
|
|
$class .= "\n\t".' */'; |
|
326
|
|
|
$class .= "\n\t".'public function __construct(QueryInterface $query, $entityID, $locale = "")'; |
|
327
|
|
|
$class .= "\n\t".'{'; |
|
328
|
|
|
$class .= "\n\t\t".'parent::__construct($query, static::$navigationIDs, $entityID, $locale);'; |
|
329
|
|
|
$class .= "\n\t".'}'; |
|
330
|
|
|
$class .= "\n" . '}'; |
|
331
|
|
|
|
|
332
|
|
|
return $class; |
|
333
|
|
|
} |
|
334
|
|
|
|
|
335
|
|
|
/** |
|
336
|
|
|
* Create entity query PHP class code. |
|
337
|
|
|
* |
|
338
|
|
|
* @param integer $navigationID Entity navigation identifier |
|
339
|
|
|
* @param string $navigationName Original entity name |
|
340
|
|
|
* @param string $entityName PHP entity name |
|
341
|
|
|
* @param array $navigationFields Collection of entity additional fields |
|
342
|
|
|
* @return string Generated entity query PHP class code |
|
343
|
|
|
*/ |
|
344
|
|
|
protected function createQueryClass($navigationID, $navigationName, $entityName, $navigationFields) |
|
345
|
|
|
{ |
|
346
|
|
|
$class = "\n"; |
|
347
|
|
|
$class .= "\n" . '/**'; |
|
348
|
|
|
$class .= "\n" . ' * Class for getting "'.$navigationName.'" instances from database'; |
|
349
|
|
|
$class .= "\n" . ' * @method '.$this->entityName($navigationName).'[] find() Get entities collection'; |
|
350
|
|
|
$class .= "\n" . ' * @method '.$this->entityName($navigationName).' first() Get entity'; |
|
351
|
|
|
$class .= "\n" . ' * @method '.$entityName.' where($fieldName, $fieldValue = null, $fieldRelation = ArgumentInterface::EQUAL)'; |
|
352
|
|
|
$class .= "\n" . ' * @method '.$entityName.' primary($value) Query for chaining'; |
|
353
|
|
|
$class .= "\n" . ' * @method '.$entityName.' identifier($value) Query for chaining'; |
|
354
|
|
|
$class .= "\n" . ' * @method '.$entityName.' created($value) Query for chaining'; |
|
355
|
|
|
$class .= "\n" . ' * @method '.$entityName.' modified($value) Query for chaining'; |
|
356
|
|
|
$class .= "\n" . ' * @method '.$entityName.' published($value) Query for chaining'; |
|
357
|
|
|
$class .= "\n" . ' */'; |
|
358
|
|
|
$class .= "\n" . 'class ' . $entityName . ' extends \samsoncms\api\query\Entity'; |
|
359
|
|
|
$class .= "\n" . '{'; |
|
360
|
|
|
|
|
361
|
|
|
// Iterate additional fields |
|
362
|
|
|
$localizedFieldIDs = array(); |
|
363
|
|
|
$notLocalizedFieldIDs = array(); |
|
364
|
|
|
$allFieldIDs = array(); |
|
365
|
|
|
$allFieldNames = array(); |
|
366
|
|
|
$allFieldValueColumns = array(); |
|
367
|
|
|
foreach ($navigationFields as $fieldID => $fieldRow) { |
|
368
|
|
|
$fieldName = $this->fieldName($fieldRow['Name']); |
|
369
|
|
|
|
|
370
|
|
|
// TODO: Add different method generation depending on their field type |
|
371
|
|
|
$class .= $this->generateFieldConditionMethod( |
|
372
|
|
|
$fieldName, |
|
373
|
|
|
$fieldRow[Field::F_PRIMARY], |
|
374
|
|
|
$fieldRow[Field::F_TYPE] |
|
375
|
|
|
); |
|
376
|
|
|
|
|
377
|
|
|
// Store field metadata |
|
378
|
|
|
$allFieldIDs[] = '"' . $fieldID . '" => "' . $fieldName . '"'; |
|
379
|
|
|
$allFieldNames[] = '"' . $fieldName . '" => "' . $fieldID . '"'; |
|
380
|
|
|
$allFieldValueColumns[] = '"' . $fieldID . '" => "' . Field::valueColumn($fieldRow[Field::F_TYPE]) . '"'; |
|
381
|
|
|
if ($fieldRow[Field::F_LOCALIZED] == 1) { |
|
382
|
|
|
$localizedFieldIDs[] = '"' . $fieldID . '" => "' . $fieldName . '"'; |
|
383
|
|
|
} else { |
|
384
|
|
|
$notLocalizedFieldIDs[] = '"' . $fieldID . '" => "' . $fieldName . '"'; |
|
385
|
|
|
} |
|
386
|
|
|
} |
|
387
|
|
|
|
|
388
|
|
|
$class .= "\n\t"; |
|
389
|
|
|
$class .= "\n\t" . '/** @var string Not transliterated entity name */'; |
|
390
|
|
|
$class .= "\n\t" . 'protected static $identifier = "'.$this->fullEntityName($navigationName).'";'; |
|
391
|
|
|
$class .= "\n\t" . '/** @var array Collection of navigation identifiers */'; |
|
392
|
|
|
$class .= "\n\t" . 'protected static $navigationIDs = array(' . $navigationID . ');'; |
|
393
|
|
|
$class .= "\n\t" . '/** @var array Collection of localized additional fields identifiers */'; |
|
394
|
|
|
$class .= "\n\t" . 'protected static $localizedFieldIDs = array(' . "\n\t\t". implode(','."\n\t\t", $localizedFieldIDs) . "\n\t".');'; |
|
395
|
|
|
$class .= "\n\t" . '/** @var array Collection of NOT localized additional fields identifiers */'; |
|
396
|
|
|
$class .= "\n\t" . 'protected static $notLocalizedFieldIDs = array(' . "\n\t\t". implode(','."\n\t\t", $notLocalizedFieldIDs) . "\n\t".');'; |
|
397
|
|
|
$class .= "\n\t" . '/** @var array Collection of all additional fields identifiers */'; |
|
398
|
|
|
$class .= "\n\t" . 'protected static $fieldIDs = array(' . "\n\t\t". implode(','."\n\t\t", $allFieldIDs) . "\n\t".');'; |
|
399
|
|
|
$class .= "\n\t" . '/** @var array Collection of additional fields value column names */'; |
|
400
|
|
|
$class .= "\n\t" . 'protected static $fieldValueColumns = array(' . "\n\t\t". implode(','."\n\t\t", $allFieldValueColumns) . "\n\t".');'; |
|
401
|
|
|
$class .= "\n\t" . '/** @var array Collection of additional field names */'; |
|
402
|
|
|
$class .= "\n\t" . 'public static $fieldNames = array(' . "\n\t\t". implode(','."\n\t\t", $allFieldNames) . "\n\t".');'; |
|
403
|
|
|
$class .= "\n" . '}'; |
|
404
|
|
|
|
|
405
|
|
|
// Replace tabs with spaces |
|
406
|
|
|
return $class; |
|
407
|
|
|
} |
|
408
|
|
|
|
|
409
|
|
|
/** @return string Entity state hash */ |
|
410
|
|
|
public function entityHash() |
|
411
|
|
|
{ |
|
412
|
|
|
// Получим информацию о всех таблицах из БД |
|
413
|
|
|
return md5(serialize($this->database->fetch( |
|
414
|
|
|
'SELECT `TABLES`.`TABLE_NAME` as `TABLE_NAME` |
|
415
|
|
|
FROM `information_schema`.`TABLES` as `TABLES` |
|
416
|
|
|
WHERE `TABLES`.`TABLE_SCHEMA`="' . $this->database->database() . '";' |
|
417
|
|
|
))); |
|
418
|
|
|
} |
|
419
|
|
|
|
|
420
|
|
|
/** @return array Get collection of navigation objects */ |
|
421
|
|
|
protected function entityNavigations($type = 0) |
|
422
|
|
|
{ |
|
423
|
|
|
return $this->database->fetch(' |
|
424
|
|
|
SELECT * FROM `structure` |
|
425
|
|
|
WHERE `Active` = "1" AND `Type` = "'.$type.'"' |
|
426
|
|
|
); |
|
427
|
|
|
} |
|
428
|
|
|
|
|
429
|
|
|
/** @return array Collection of navigation additional fields */ |
|
430
|
|
|
protected function navigationFields($navigationID) |
|
431
|
|
|
{ |
|
432
|
|
|
$return = array(); |
|
433
|
|
|
// TODO: Optimize queries make one single query with only needed data |
|
434
|
|
|
foreach ($this->database->fetch('SELECT * FROM `structurefield` WHERE `StructureID` = "' . $navigationID . '" AND `Active` = "1"') as $fieldStructureRow) { |
|
435
|
|
|
foreach ($this->database->fetch('SELECT * FROM `field` WHERE `FieldID` = "' . $fieldStructureRow['FieldID'] . '"') as $fieldRow) { |
|
436
|
|
|
$return[$fieldRow['FieldID']] = $fieldRow; |
|
437
|
|
|
} |
|
438
|
|
|
} |
|
439
|
|
|
|
|
440
|
|
|
return $return; |
|
441
|
|
|
} |
|
442
|
|
|
|
|
443
|
|
|
/** |
|
444
|
|
|
* Generate entity classes. |
|
445
|
|
|
* |
|
446
|
|
|
* @param string $namespace Base namespace for generated classes |
|
447
|
|
|
* @return string Generated PHP code for entity classes |
|
448
|
|
|
*/ |
|
449
|
|
|
public function createEntityClasses($namespace = __NAMESPACE__) |
|
450
|
|
|
{ |
|
451
|
|
|
$classes = "\n" . 'namespace ' . $namespace . ';'; |
|
452
|
|
|
$classes .= "\n"; |
|
453
|
|
|
$classes .= "\n" . 'use '.$namespace.'\Field;'; |
|
454
|
|
|
$classes .= "\n" . 'use '.$namespace.'\FieldsTable;'; |
|
455
|
|
|
$classes .= "\n" . 'use \samsonframework\orm\ArgumentInterface;'; |
|
456
|
|
|
$classes .= "\n" . 'use \samsonframework\orm\QueryInterface;'; |
|
457
|
|
|
|
|
458
|
|
|
// Iterate all structures |
|
459
|
|
|
foreach ($this->entityNavigations() as $structureRow) { |
|
460
|
|
|
$navigationFields = $this->navigationFields($structureRow['StructureID']); |
|
461
|
|
|
$entityName = $this->entityName($structureRow['Name']); |
|
462
|
|
|
|
|
463
|
|
|
$classes .= $this->createEntityClass( |
|
464
|
|
|
$structureRow['Name'], |
|
465
|
|
|
$entityName, |
|
466
|
|
|
$navigationFields |
|
467
|
|
|
); |
|
468
|
|
|
|
|
469
|
|
|
$classes .= $this->createQueryClass( |
|
470
|
|
|
$structureRow['StructureID'], |
|
471
|
|
|
$structureRow['Name'], |
|
472
|
|
|
$entityName.'Query', |
|
473
|
|
|
$navigationFields |
|
474
|
|
|
); |
|
475
|
|
|
} |
|
476
|
|
|
|
|
477
|
|
|
// Iterate table structures |
|
478
|
|
|
foreach ($this->entityNavigations(2) as $structureRow) { |
|
479
|
|
|
$navigationFields = $this->navigationFields($structureRow['StructureID']); |
|
480
|
|
|
$entityName = $this->entityName($structureRow['Name']); |
|
481
|
|
|
|
|
482
|
|
|
$classes .= $this->createTableClass( |
|
483
|
|
|
$structureRow['StructureID'], |
|
484
|
|
|
$structureRow['Name'], |
|
485
|
|
|
$entityName.'Table', |
|
486
|
|
|
$navigationFields |
|
487
|
|
|
); |
|
488
|
|
|
|
|
489
|
|
|
} |
|
490
|
|
|
|
|
491
|
|
|
// Make correct code formatting |
|
492
|
|
|
return str_replace("\t", ' ', $classes); |
|
493
|
|
|
} |
|
494
|
|
|
|
|
495
|
|
|
/** |
|
496
|
|
|
* Generator constructor. |
|
497
|
|
|
* @param DatabaseInterface $database Database instance |
|
498
|
|
|
*/ |
|
499
|
|
|
public function __construct(DatabaseInterface $database) |
|
500
|
|
|
{ |
|
501
|
|
|
$this->generator = new \samsonphp\generator\Generator(__NAMESPACE__); |
|
502
|
|
|
$this->database = $database; |
|
503
|
|
|
} |
|
504
|
|
|
} |
|
505
|
|
|
|