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 '\\' . $className . '::' . $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
|
|
|
$commentType = Field::$phpTYPE[$fieldRow['Type']]; |
126
|
|
|
$fieldName = lcfirst($this->transliterated($fieldRow['Name'])); |
127
|
|
|
|
128
|
|
|
$class .= "\n\t" . '/** @var ' . $commentType . ' Field #' . $fieldRow['FieldID'] . '*/'; |
129
|
|
|
$class .= "\n\t" . 'protected $' . $fieldName . ';'; |
130
|
|
|
|
131
|
|
|
// Store field metadata |
132
|
|
|
$fields[$fieldName][] = $fieldRow; |
133
|
|
|
$fieldIDs[] = $fieldRow['FieldID']; |
134
|
|
|
//$fieldMap[] = '"'.$fieldName.'" => array("Id" => "'.$fieldRow['FieldID'].'", "Type" => ' . $type . ', "Name" => "' . $fieldRow['Name'] . '")'; |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
//$class .= "\n\t" . '/** @var array Entity additional fields metadata */'; |
139
|
|
|
//$class .= "\n\t" .'protected $fieldsData = array('."\n\t\t".implode(','."\n\t\t", $fieldMap)."\n\t".');'; |
140
|
|
|
$class .= "\n\t"; |
141
|
|
|
$class .= "\n\t" . '/** @var string Not transliterated entity name */'; |
142
|
|
|
$class .= "\n\t" . 'protected static $viewName = "' . $structureRow['Name'] . '";'; |
143
|
|
|
$class .= "\n\t" . '/** @var array Collection of additional fields identifiers */'; |
144
|
|
|
$class .= "\n\t" . 'protected static $fieldIDs = array(' . implode(',', $fieldIDs) . ');'; |
145
|
|
|
$class .= "\n\t" . '/** @var array Collection of navigation identifiers */'; |
146
|
|
|
$class .= "\n\t" . 'protected static $navigationIDs = array(' . $structureRow['StructureID'] . ');'; |
147
|
|
|
$class .= "\n" . '}'; |
148
|
|
|
|
149
|
|
|
// Replace tabs with spaces |
150
|
|
|
return str_replace("\t", ' ', $class); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Generate Query::where() analog for specific field. |
155
|
|
|
* |
156
|
|
|
* @param string $fieldName Field name |
157
|
|
|
* @param string $fieldId Field primary identifier |
158
|
|
|
* @param string $fieldType Field PHP type |
159
|
|
|
* @return string Generated PHP method code |
160
|
|
|
*/ |
161
|
|
|
protected function generateFieldConditionMethod($fieldName, $fieldId, $fieldType) |
162
|
|
|
{ |
163
|
|
|
/** @var string $fieldName Get correct field name */ |
164
|
|
|
$fieldName = lcfirst($this->transliterated($fieldName)); |
165
|
|
|
|
166
|
|
|
$code = "\n\t" . '/**'; |
167
|
|
|
$code .= "\n\t" . ' * Add '.$fieldName.'(#' . $fieldId . ') field query condition.'; |
168
|
|
|
$code .= "\n\t" . ' * @param '.Field::$phpTYPE[$fieldType].' $value Field value'; |
169
|
|
|
$code .= "\n\t" . ' * @return self Chaining'; |
170
|
|
|
$code .= "\n\t" . ' * @see Generic::where()'; |
171
|
|
|
$code .= "\n\t" . ' */'; |
172
|
|
|
$code .= "\n\t" . 'public function ' . $fieldName . '($value)'; |
173
|
|
|
$code .= "\n\t" . "{"; |
174
|
|
|
$code .= "\n\t\t" . 'return $this->where("'.$fieldName.'", $value);'; |
175
|
|
|
|
176
|
|
|
return $code . "\n\t" . "}"."\n"; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* Create entity PHP class code. |
181
|
|
|
* |
182
|
|
|
* @param array $structureRow Collection of structure info |
183
|
|
|
* @return string Generated entitiy class code |
184
|
|
|
*/ |
185
|
|
|
protected function createQueryClass($structureRow) |
186
|
|
|
{ |
187
|
|
|
$structureKey = ucfirst($this->transliterated($structureRow['Name'])); |
188
|
|
|
|
189
|
|
|
$class = "\n\n" . '/** Class for getting "'.$structureRow['Name'].'" instances from database */'; |
190
|
|
|
$class .= "\n" . 'class ' . $structureKey . 'Query extends Generic'; |
191
|
|
|
$class .= "\n" . '{'; |
192
|
|
|
|
193
|
|
|
// Get structure fields |
194
|
|
|
//$fieldMap = array(); |
195
|
|
|
$fields = array(); |
|
|
|
|
196
|
|
|
$fieldIDs = array(); |
197
|
|
|
|
198
|
|
|
// TODO: Optimize queries |
199
|
|
|
foreach ($this->database->fetch('SELECT * FROM `structurefield` WHERE `StructureID` = "' . $structureRow['StructureID'] . '" AND `Active` = "1"') as $fieldStructureRow) { |
200
|
|
|
foreach ($this->database->fetch('SELECT * FROM `field` WHERE `FieldID` = "' . $fieldStructureRow['FieldID'] . '"') as $fieldRow) { |
201
|
|
|
$type = str_replace( |
|
|
|
|
202
|
|
|
'\samsoncms\api\Field', |
203
|
|
|
'Field', |
204
|
|
|
$this->constantNameByValue($fieldRow['Type']) |
205
|
|
|
); |
206
|
|
|
$commentType = Field::$phpTYPE[$fieldRow['Type']]; |
|
|
|
|
207
|
|
|
$fieldName = lcfirst($this->transliterated($fieldRow['Name'])); |
208
|
|
|
|
209
|
|
|
$class .= $this->generateFieldConditionMethod($fieldName, $fieldRow['FieldID'], $fieldRow['Type']); |
210
|
|
|
|
211
|
|
|
// Store field metadata |
212
|
|
|
$fieldIDs[] = '"'.$fieldName . '" => "'.$fieldRow['FieldID'].'"'; |
213
|
|
|
} |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
//$class .= "\n\t" . '/** @var array Entity additional fields metadata */'; |
217
|
|
|
//$class .= "\n\t" .'protected $fieldsData = array('."\n\t\t".implode(','."\n\t\t", $fieldMap)."\n\t".');'; |
218
|
|
|
//$class .= "\n\t"; |
219
|
|
|
$class .= "\n\t" . '/** @var string Not transliterated entity name */'; |
220
|
|
|
$class .= "\n\t" . 'protected static $identifier = "\\\\samsoncms\\\\api\\\\' . $structureKey . '";'; |
221
|
|
|
$class .= "\n\t" . '/** @var array Collection of additional fields identifiers */'; |
222
|
|
|
$class .= "\n\t" . 'protected static $fieldIDs = array(' . "\n\t\t". implode(','."\n\t\t", $fieldIDs) . "\n\t".');'; |
223
|
|
|
$class .= "\n\t" . '/** @var array Collection of navigation identifiers */'; |
224
|
|
|
$class .= "\n\t" . 'protected static $navigationIDs = array(' . $structureRow['StructureID'] . ');'; |
225
|
|
|
$class .= "\n" . '}'; |
226
|
|
|
|
227
|
|
|
// Replace tabs with spaces |
228
|
|
|
return str_replace("\t", ' ', $class); |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
/** @return string Entity state hash */ |
232
|
|
|
public function entityHash() |
233
|
|
|
{ |
234
|
|
|
// Получим информацию о всех таблицах из БД |
235
|
|
|
return md5(serialize($this->database->fetch( |
236
|
|
|
'SELECT `TABLES`.`TABLE_NAME` as `TABLE_NAME` |
237
|
|
|
FROM `information_schema`.`TABLES` as `TABLES` |
238
|
|
|
WHERE `TABLES`.`TABLE_SCHEMA`="' . $this->database->database() . '";' |
239
|
|
|
))); |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
/** @return mixed Get collection of structures object */ |
243
|
|
|
protected function entityStructures() |
244
|
|
|
{ |
245
|
|
|
return $this->database->fetch(' |
246
|
|
|
SELECT * FROM `structure` |
247
|
|
|
WHERE `Active` = "1" AND `Type` = "0"' |
248
|
|
|
); |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
/** @return string Generate entity classes */ |
252
|
|
|
public function createEntityClasses() |
253
|
|
|
{ |
254
|
|
|
$classes = "\n" . 'namespace ' . __NAMESPACE__ . ';'; |
255
|
|
|
$classes .= "\n"; |
256
|
|
|
$classes .= "\n" . 'use \samsoncms\api\Field;'; |
257
|
|
|
$classes .= "\n" . 'use \samsoncms\api\query\Generic;'; |
258
|
|
|
// Iterate all structures |
259
|
|
|
foreach ($this->entityStructures() as $structureRow) { |
260
|
|
|
$classes .= $this->createEntityClass($structureRow); |
261
|
|
|
$classes .= $this->createQueryClass($structureRow); |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
return $classes; |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
/** |
268
|
|
|
* Generator constructor. |
269
|
|
|
* @param DatabaseInterface $database Database instance |
270
|
|
|
*/ |
271
|
|
|
public function __construct(DatabaseInterface $database) |
272
|
|
|
{ |
273
|
|
|
$this->database = $database; |
274
|
|
|
} |
275
|
|
|
} |
276
|
|
|
|
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.