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 samsonframework\orm\DatabaseInterface; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Entity classes generator. |
14
|
|
|
* @package samsoncms\api |
15
|
|
|
*/ |
16
|
|
|
class Generator |
17
|
|
|
{ |
18
|
|
|
/** @var DatabaseInterface */ |
19
|
|
|
protected $database; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Transliterate string to english. |
23
|
|
|
* |
24
|
|
|
* @param string $string Source string |
25
|
|
|
* @return string Transliterated string |
26
|
|
|
*/ |
27
|
|
|
protected function transliterated($string) |
28
|
|
|
{ |
29
|
|
|
return str_replace( |
30
|
|
|
' ', |
31
|
|
|
'', |
32
|
|
|
ucwords(iconv("UTF-8", "UTF-8//IGNORE", strtr($string, array( |
33
|
|
|
"'" => "", |
34
|
|
|
"`" => "", |
35
|
|
|
"-" => " ", |
36
|
|
|
"_" => " ", |
37
|
|
|
"а" => "a", "А" => "a", |
38
|
|
|
"б" => "b", "Б" => "b", |
39
|
|
|
"в" => "v", "В" => "v", |
40
|
|
|
"г" => "g", "Г" => "g", |
41
|
|
|
"д" => "d", "Д" => "d", |
42
|
|
|
"е" => "e", "Е" => "e", |
43
|
|
|
"ж" => "zh", "Ж" => "zh", |
44
|
|
|
"з" => "z", "З" => "z", |
45
|
|
|
"и" => "i", "И" => "i", |
46
|
|
|
"й" => "y", "Й" => "y", |
47
|
|
|
"к" => "k", "К" => "k", |
48
|
|
|
"л" => "l", "Л" => "l", |
49
|
|
|
"м" => "m", "М" => "m", |
50
|
|
|
"н" => "n", "Н" => "n", |
51
|
|
|
"о" => "o", "О" => "o", |
52
|
|
|
"п" => "p", "П" => "p", |
53
|
|
|
"р" => "r", "Р" => "r", |
54
|
|
|
"с" => "s", "С" => "s", |
55
|
|
|
"т" => "t", "Т" => "t", |
56
|
|
|
"у" => "u", "У" => "u", |
57
|
|
|
"ф" => "f", "Ф" => "f", |
58
|
|
|
"х" => "h", "Х" => "h", |
59
|
|
|
"ц" => "c", "Ц" => "c", |
60
|
|
|
"ч" => "ch", "Ч" => "ch", |
61
|
|
|
"ш" => "sh", "Ш" => "sh", |
62
|
|
|
"щ" => "sch", "Щ" => "sch", |
63
|
|
|
"ъ" => "", "Ъ" => "", |
64
|
|
|
"ы" => "y", "Ы" => "y", |
65
|
|
|
"ь" => "", "Ь" => "", |
66
|
|
|
"э" => "e", "Э" => "e", |
67
|
|
|
"ю" => "yu", "Ю" => "yu", |
68
|
|
|
"я" => "ya", "Я" => "ya", |
69
|
|
|
"і" => "i", "І" => "i", |
70
|
|
|
"ї" => "yi", "Ї" => "yi", |
71
|
|
|
"є" => "e", "Є" => "e" |
72
|
|
|
) |
73
|
|
|
) |
74
|
|
|
) |
75
|
|
|
) |
76
|
|
|
); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Get class constant name by its value. |
81
|
|
|
* |
82
|
|
|
* @param string $value Constant value |
83
|
|
|
* @param string $className Class name |
84
|
|
|
* @return string Full constant name |
85
|
|
|
*/ |
86
|
|
|
protected function constantNameByValue($value, $className = Field::ENTITY) |
87
|
|
|
{ |
88
|
|
|
// Get array where class constants are values and their values are keys |
89
|
|
|
$nameByValue = array_flip((new \ReflectionClass($className))->getConstants()); |
90
|
|
|
|
91
|
|
|
// Try to find constant by its value |
92
|
|
|
if (isset($nameByValue[$value])) { |
93
|
|
|
// Return constant name |
94
|
|
|
return $nameByValue[$value]; |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Create entity PHP class code. |
100
|
|
|
* |
101
|
|
|
* @param array $structureRow Collection of structure info |
102
|
|
|
* @return string Generated entitiy class code |
103
|
|
|
*/ |
104
|
|
|
protected function createEntityClass($structureRow) |
105
|
|
|
{ |
106
|
|
|
$structureKey = ucfirst($this->transliterated($structureRow['Name'])); |
107
|
|
|
|
108
|
|
|
$class = "\n\n" . '/** "'.$structureRow['Name'].'" entity */'; |
109
|
|
|
$class .= "\n" . 'class ' . $structureKey . ' extends Entity'; |
110
|
|
|
$class .= "\n" . '{'; |
111
|
|
|
|
112
|
|
|
// Get structure fields |
113
|
|
|
//$fieldMap = array(); |
114
|
|
|
$fields = array(); |
115
|
|
|
$fieldIDs = array(); |
116
|
|
|
|
117
|
|
|
// TODO: Optimize queries |
118
|
|
|
foreach ($this->database->fetch('SELECT * FROM `structurefield` WHERE `StructureID` = "' . $structureRow['StructureID'] . '" AND `Active` = "1"') as $fieldStructureRow) { |
119
|
|
|
foreach ($this->database->fetch('SELECT * FROM `field` WHERE `FieldID` = "' . $fieldStructureRow['FieldID'] . '"') as $fieldRow) { |
120
|
|
|
$type = str_replace( |
|
|
|
|
121
|
|
|
'\samsoncms\api\Field', |
122
|
|
|
'Field', |
123
|
|
|
$this->constantNameByValue($fieldRow['Type']) |
124
|
|
|
); |
125
|
|
|
$fieldName = lcfirst($this->transliterated($fieldRow['Name'])); |
126
|
|
|
|
127
|
|
|
$class .= "\n\t" . '/** @var ' . Field::phpType($fieldRow['Type']) . ' Field #' . $fieldRow['FieldID'] . '*/'; |
128
|
|
|
$class .= "\n\t" . 'protected $' . $fieldName . ';'; |
129
|
|
|
|
130
|
|
|
// Store field metadata |
131
|
|
|
$fields[$fieldName][] = $fieldRow; |
132
|
|
|
$fieldIDs[] = $fieldRow['FieldID']; |
133
|
|
|
//$fieldMap[] = '"'.$fieldName.'" => array("Id" => "'.$fieldRow['FieldID'].'", "Type" => ' . $type . ', "Name" => "' . $fieldRow['Name'] . '")'; |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
//$class .= "\n\t" . '/** @var array Entity additional fields metadata */'; |
138
|
|
|
//$class .= "\n\t" .'protected $fieldsData = array('."\n\t\t".implode(','."\n\t\t", $fieldMap)."\n\t".');'; |
139
|
|
|
$class .= "\n\t"; |
140
|
|
|
$class .= "\n\t" . '/** @var string Not transliterated entity name */'; |
141
|
|
|
$class .= "\n\t" . 'protected static $viewName = "' . $structureRow['Name'] . '";'; |
142
|
|
|
$class .= "\n\t" . '/** @var array Collection of additional fields identifiers */'; |
143
|
|
|
$class .= "\n\t" . 'protected static $fieldIDs = array(' . implode(',', $fieldIDs) . ');'; |
144
|
|
|
$class .= "\n\t" . '/** @var array Collection of navigation identifiers */'; |
145
|
|
|
$class .= "\n\t" . 'protected static $navigationIDs = array(' . $structureRow['StructureID'] . ');'; |
146
|
|
|
$class .= "\n" . '}'; |
147
|
|
|
|
148
|
|
|
// Replace tabs with spaces |
149
|
|
|
return str_replace("\t", ' ', $class); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Get correct entity name. |
154
|
|
|
* |
155
|
|
|
* @param string $navigationName Original navigation entity name |
156
|
|
|
* @return string Correct PHP-supported entity name |
157
|
|
|
*/ |
158
|
|
|
protected function entityName($navigationName) |
159
|
|
|
{ |
160
|
|
|
return ucfirst($this->transliterated($navigationName)); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* Get correct full entity name with name space. |
165
|
|
|
* |
166
|
|
|
* @param string $navigationName Original navigation entity name |
167
|
|
|
* @param string $namespace Namespace |
168
|
|
|
* @return string Correct PHP-supported entity name |
169
|
|
|
*/ |
170
|
|
|
protected function fullEntityName($navigationName, $namespace = __NAMESPACE__) |
171
|
|
|
{ |
172
|
|
|
return $namespace.$this->entityName($navigationName); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* Get correct field name. |
177
|
|
|
* |
178
|
|
|
* @param string $fieldName Original field name |
179
|
|
|
* @return string Correct PHP-supported field name |
180
|
|
|
*/ |
181
|
|
|
protected function fieldName($fieldName) |
182
|
|
|
{ |
183
|
|
|
return $fieldName = lcfirst($this->transliterated($fieldName)); |
|
|
|
|
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* Get additional field type in form of Field constant name |
188
|
|
|
* by database additional field type identifier. |
189
|
|
|
* |
190
|
|
|
* @param integer $fieldType Additional field type identifier |
191
|
|
|
* @return string Additional field type constant |
192
|
|
|
*/ |
193
|
|
|
protected function additionalFieldType($fieldType) |
194
|
|
|
{ |
195
|
|
|
return 'Field::'.$this->constantNameByValue($fieldType); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* Generate Query::where() analog for specific field. |
200
|
|
|
* |
201
|
|
|
* @param string $fieldName Field name |
202
|
|
|
* @param string $fieldId Field primary identifier |
203
|
|
|
* @param string $fieldType Field PHP type |
204
|
|
|
* @return string Generated PHP method code |
205
|
|
|
*/ |
206
|
|
|
protected function generateFieldConditionMethod($fieldName, $fieldId, $fieldType) |
207
|
|
|
{ |
208
|
|
|
$code = "\n\t" . '/**'; |
209
|
|
|
$code .= "\n\t" . ' * Add '.$fieldName.'(#' . $fieldId . ') field query condition.'; |
210
|
|
|
$code .= "\n\t" . ' * @param '.Field::phpType($fieldType).' $value Field value'; |
211
|
|
|
$code .= "\n\t" . ' * @return self Chaining'; |
212
|
|
|
$code .= "\n\t" . ' * @see Generic::where()'; |
213
|
|
|
$code .= "\n\t" . ' */'; |
214
|
|
|
$code .= "\n\t" . 'public function ' . $fieldName . '($value)'; |
215
|
|
|
$code .= "\n\t" . "{"; |
216
|
|
|
$code .= "\n\t\t" . 'return $this->where("'.$fieldName.'", $value);'; |
217
|
|
|
|
218
|
|
|
return $code . "\n\t" . "}"."\n"; |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* Create entity PHP class code. |
223
|
|
|
* |
224
|
|
|
* @param $navigationID |
225
|
|
|
* @param $navigationName |
226
|
|
|
* @param $entityName |
227
|
|
|
* @param $navigationFields |
228
|
|
|
* @return string Generated entitiy class code |
229
|
|
|
* @internal param array $navigationData Collection of structure info |
230
|
|
|
*/ |
231
|
|
|
protected function createQueryClass($navigationID, $navigationName, $entityName, $navigationFields) |
232
|
|
|
{ |
233
|
|
|
$class = "\n\n" . '/** Class for getting "'.$navigationName.'" instances from database */'; |
234
|
|
|
$class .= "\n" . 'class ' . $entityName . ' extends Generic'; |
235
|
|
|
$class .= "\n" . '{'; |
236
|
|
|
|
237
|
|
|
// Iterate additional fields |
238
|
|
|
$fieldIDs = array(); |
239
|
|
|
foreach ($navigationFields as $fieldRow) { |
240
|
|
|
$fieldName = $this->fieldName($fieldRow['Name']); |
241
|
|
|
|
242
|
|
|
$class .= $this->generateFieldConditionMethod( |
243
|
|
|
$fieldName, |
244
|
|
|
$fieldRow['FieldID'], |
245
|
|
|
$fieldRow['Type'] |
246
|
|
|
); |
247
|
|
|
|
248
|
|
|
// Store field metadata |
249
|
|
|
$fieldIDs[] = '"' . $fieldName . '" => "' . $fieldRow['FieldID'] . '"'; |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
$class .= "\n\t"; |
253
|
|
|
$class .= "\n\t" . '/** @var string Not transliterated entity name */'; |
254
|
|
|
$class .= "\n\t" . 'protected static $identifier = "'.$this->fullEntityName($navigationName).'";'; |
255
|
|
|
$class .= "\n\t" . '/** @var array Collection of navigation identifiers */'; |
256
|
|
|
$class .= "\n\t" . 'protected static $navigationIDs = array(' . $navigationID . ');'; |
257
|
|
|
$class .= "\n\t" . '/** @var array Collection of additional fields identifiers */'; |
258
|
|
|
$class .= "\n\t" . 'protected static $fieldIDs = array(' . "\n\t\t". implode(','."\n\t\t", $fieldIDs) . "\n\t".');'; |
259
|
|
|
$class .= "\n" . '}'; |
260
|
|
|
|
261
|
|
|
// Replace tabs with spaces |
262
|
|
|
return $class; |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
/** @return string Entity state hash */ |
266
|
|
|
public function entityHash() |
267
|
|
|
{ |
268
|
|
|
// Получим информацию о всех таблицах из БД |
269
|
|
|
return md5(serialize($this->database->fetch( |
270
|
|
|
'SELECT `TABLES`.`TABLE_NAME` as `TABLE_NAME` |
271
|
|
|
FROM `information_schema`.`TABLES` as `TABLES` |
272
|
|
|
WHERE `TABLES`.`TABLE_SCHEMA`="' . $this->database->database() . '";' |
273
|
|
|
))); |
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
/** @return array Get collection of navigation objects */ |
277
|
|
|
protected function entityNavigations($type = 0) |
278
|
|
|
{ |
279
|
|
|
return $this->database->fetch(' |
280
|
|
|
SELECT * FROM `structure` |
281
|
|
|
WHERE `Active` = "1" AND `Type` = "'.$type.'"' |
282
|
|
|
); |
283
|
|
|
} |
284
|
|
|
|
285
|
|
|
/** @return array Collection of navigation additional fields */ |
286
|
|
|
protected function navigationFields($navigationID) |
287
|
|
|
{ |
288
|
|
|
$return = array(); |
289
|
|
|
// TODO: Optimize queries make one single query with only needed data |
290
|
|
|
foreach ($this->database->fetch('SELECT * FROM `structurefield` WHERE `StructureID` = "' . $navigationID . '" AND `Active` = "1"') as $fieldStructureRow) { |
291
|
|
|
foreach ($this->database->fetch('SELECT * FROM `field` WHERE `FieldID` = "' . $fieldStructureRow['FieldID'] . '"') as $fieldRow) { |
292
|
|
|
$return[] = $fieldRow; |
293
|
|
|
} |
294
|
|
|
} |
295
|
|
|
|
296
|
|
|
return $return; |
297
|
|
|
} |
298
|
|
|
|
299
|
|
|
/** @return string Generate entity classes */ |
300
|
|
|
public function createEntityClasses() |
301
|
|
|
{ |
302
|
|
|
$classes = "\n" . 'namespace ' . __NAMESPACE__ . ';'; |
303
|
|
|
$classes .= "\n"; |
304
|
|
|
$classes .= "\n" . 'use \samsoncms\api\Field;'; |
305
|
|
|
$classes .= "\n" . 'use \samsoncms\api\query\Generic;'; |
306
|
|
|
// Iterate all structures |
307
|
|
|
foreach ($this-> entityNavigations() as $structureRow) { |
308
|
|
|
$navigationFields = $this->navigationFields($structureRow['StructureID']); |
309
|
|
|
$classes .= $this->createEntityClass($structureRow); |
310
|
|
|
$classes .= $this->createQueryClass( |
311
|
|
|
$structureRow['StructureID'], |
312
|
|
|
$structureRow['Name'], |
313
|
|
|
$this->entityName($structureRow['Name'].'Query'), |
314
|
|
|
$navigationFields |
315
|
|
|
); |
316
|
|
|
} |
317
|
|
|
|
318
|
|
|
// Make correct code formatting |
319
|
|
|
return str_replace("\t", ' ', $classes); |
320
|
|
|
} |
321
|
|
|
|
322
|
|
|
/** |
323
|
|
|
* Generator constructor. |
324
|
|
|
* @param DatabaseInterface $database Database instance |
325
|
|
|
*/ |
326
|
|
|
public function __construct(DatabaseInterface $database) |
327
|
|
|
{ |
328
|
|
|
$this->database = $database; |
329
|
|
|
} |
330
|
|
|
} |
331
|
|
|
|
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.