|
1
|
|
|
<?php |
|
2
|
|
|
//[PHPCOMPRESSOR(remove,start)] |
|
3
|
|
|
/** |
|
4
|
|
|
* Created by Vitaly Iegorov <[email protected]>. |
|
5
|
|
|
* on 23.03.16 at 11:45 |
|
6
|
|
|
*/ |
|
7
|
|
|
namespace samsoncms\api\generator\analyzer; |
|
8
|
|
|
|
|
9
|
|
|
use samsoncms\api\Field; |
|
10
|
|
|
use samsoncms\api\generator\exception\ParentEntityNotFound; |
|
11
|
|
|
use samsoncms\api\generator\metadata\RealMetadata; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Generic real table entities metadata analyzer. |
|
15
|
|
|
* |
|
16
|
|
|
* @package samsoncms\api\analyzer |
|
17
|
|
|
*/ |
|
18
|
|
|
class RealAnalyzer extends GenericAnalyzer |
|
19
|
|
|
{ |
|
20
|
|
|
/** @var string Metadata class */ |
|
21
|
|
|
protected $metadataClass = RealMetadata::class; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Analyze virtual entities and gather their metadata. |
|
25
|
|
|
* |
|
26
|
|
|
* @return RealMetadata[] Collection of filled metadata |
|
27
|
|
|
* @throws ParentEntityNotFound |
|
28
|
|
|
*/ |
|
29
|
|
|
public function analyze() |
|
30
|
|
|
{ |
|
31
|
|
|
/** @var RealMetadata[] $metadataCollection Set pointer to global metadata collection */ |
|
32
|
|
|
$metadataCollection = []; |
|
33
|
|
|
|
|
34
|
|
|
// Iterate all structures, parents first |
|
35
|
|
|
foreach ($this->getEntities() as $columnRow) { |
|
36
|
|
|
$table = $columnRow['Table']; |
|
37
|
|
|
|
|
38
|
|
|
/** @var RealMetadata $metadata Set pointer to metadata instance by table name */ |
|
39
|
|
|
$metadata = &$metadataCollection[$table]; |
|
40
|
|
|
|
|
41
|
|
|
// If this is a new table - create metadata instance |
|
42
|
|
|
if (null === $metadata) { |
|
43
|
|
|
$metadata = new $this->metadataClass; |
|
44
|
|
|
$metadata->entity = $this->entityName($table); |
|
45
|
|
|
$metadata->entityName = $table; |
|
46
|
|
|
$metadata->entityClassName = $this->fullEntityName($metadata->entity); |
|
47
|
|
|
|
|
48
|
|
|
// Get old AR collections of metadata |
|
49
|
|
|
$arEntity = '\samson\activerecord\\'.$metadata->entity; |
|
50
|
|
|
if (class_exists($arEntity)) { |
|
51
|
|
|
foreach ($arEntity::$_attributes as $attribute) { |
|
52
|
|
|
$metadata->arAttributes[$this->fieldName($attribute)] = $attribute; |
|
53
|
|
|
} |
|
54
|
|
|
foreach ($arEntity::$_table_attributes as $attribute) { |
|
55
|
|
|
$metadata->arTableAttributes[$this->fieldName($attribute)] = $attribute; |
|
56
|
|
|
} |
|
57
|
|
|
foreach ($arEntity::$_types as $attribute => $oldType) { |
|
58
|
|
|
$metadata->arTypes[$this->fieldName($attribute)] = $oldType; |
|
59
|
|
|
} |
|
60
|
|
|
$metadata->arSelect = $arEntity::$_sql_select; |
|
61
|
|
|
$metadata->arMap = $arEntity::$_map; |
|
62
|
|
|
$metadata->arFrom = $arEntity::$_sql_from; |
|
63
|
|
|
$metadata->arGroup = $arEntity::$_own_group; |
|
64
|
|
|
$metadata->arRelationAlias = $arEntity::$_relation_alias; |
|
65
|
|
|
$metadata->arRelationType = $arEntity::$_relation_type; |
|
66
|
|
|
$metadata->arRelations = $arEntity::$_relations; |
|
67
|
|
|
} |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
// Generate correct PSR-2 field name |
|
71
|
|
|
$fieldName = $this->fieldName($columnRow['Field']); |
|
72
|
|
|
if (!in_array($fieldName, $metadata->fields)) { |
|
73
|
|
|
$metadata->fieldNames[$fieldName] = $columnRow['Field']; |
|
74
|
|
|
$metadata->fields[$columnRow['Field']] = $fieldName; |
|
75
|
|
|
$metadata->types[$columnRow['Field']] = $this->databaseTypeToPHP($columnRow['Type']); |
|
76
|
|
|
$metadata->internalTypes[$columnRow['Field']] = $columnRow['Type']; |
|
77
|
|
|
$metadata->defaults[$columnRow['Field']] = $columnRow['Default']; |
|
78
|
|
|
|
|
79
|
|
|
// Store entity primary field |
|
80
|
|
|
if (strtolower($columnRow['Key']) === 'pri') { |
|
81
|
|
|
$metadata->primaryField = $columnRow['Field']; |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
return $metadataCollection; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* Get real entities from database. |
|
91
|
|
|
* |
|
92
|
|
|
* @return array Get collection of database entities metadata |
|
93
|
|
|
*/ |
|
94
|
|
|
public function getEntities() |
|
95
|
|
|
{ |
|
96
|
|
|
// Get tables data |
|
97
|
|
|
return $this->database->fetch( |
|
98
|
|
|
'SELECT |
|
99
|
|
|
`TABLES`.`TABLE_NAME` as `Table`, |
|
100
|
|
|
`COLUMNS`.`COLUMN_NAME` as `Field`, |
|
101
|
|
|
`COLUMNS`.`DATA_TYPE` as `Type`, |
|
102
|
|
|
`COLUMNS`.`IS_NULLABLE` as `Null`, |
|
103
|
|
|
`COLUMNS`.`COLUMN_KEY` as `Key`, |
|
104
|
|
|
`COLUMNS`.`COLUMN_DEFAULT` as `Default`, |
|
105
|
|
|
`COLUMNS`.`EXTRA` as `Extra` |
|
106
|
|
|
FROM `information_schema`.`TABLES` as `TABLES` |
|
107
|
|
|
LEFT JOIN `information_schema`.`COLUMNS` as `COLUMNS` |
|
108
|
|
|
ON `TABLES`.`TABLE_NAME`=`COLUMNS`.`TABLE_NAME` |
|
109
|
|
|
WHERE `TABLES`.`TABLE_SCHEMA`="' . $this->database->database() . '" |
|
110
|
|
|
AND `COLUMNS`.`TABLE_SCHEMA`="' . $this->database->database() . '" |
|
111
|
|
|
AND `TABLES`.`TABLE_NAME` != "cms_version" |
|
112
|
|
|
'); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* Get PHP data type from database column type. |
|
117
|
|
|
* |
|
118
|
|
|
* @param string $type Database column type |
|
119
|
|
|
* |
|
120
|
|
|
* @return string PHP data type |
|
121
|
|
|
*/ |
|
122
|
|
|
protected function databaseTypeToPHP($type) |
|
123
|
|
|
{ |
|
124
|
|
|
switch (strtoupper($type)) { |
|
125
|
|
|
case 'DECIMAL': |
|
126
|
|
|
case 'TINY': |
|
127
|
|
|
case 'TINYINT': |
|
128
|
|
|
case 'BIT': |
|
129
|
|
|
case 'INT': |
|
130
|
|
|
case 'SMALLINT': |
|
131
|
|
|
case 'MEDIUMINT': |
|
132
|
|
|
case 'INTEGER': |
|
133
|
|
|
case 'BIGINT': |
|
134
|
|
|
case 'SHORT': |
|
135
|
|
|
case 'LONG': |
|
136
|
|
|
case 'LONGLONG': |
|
137
|
|
|
case 'INT24': |
|
138
|
|
|
return 'int'; |
|
139
|
|
|
case 'FLOAT': |
|
140
|
|
|
return 'float'; |
|
141
|
|
|
case 'DOUBLE': |
|
142
|
|
|
case 'DOUBLE PRECISION': |
|
143
|
|
|
return 'double'; |
|
144
|
|
|
case 'DATETIME': |
|
145
|
|
|
case 'DATE': |
|
146
|
|
|
case 'TIMESTAMP': |
|
147
|
|
|
return 'int'; |
|
148
|
|
|
case 'BOOL': |
|
149
|
|
|
case 'BOOLEAN': |
|
150
|
|
|
return 'bool'; |
|
151
|
|
|
case 'CHAR': |
|
152
|
|
|
case 'VARCHAR': |
|
153
|
|
|
case 'TEXT': |
|
154
|
|
|
return 'string'; |
|
155
|
|
|
default: |
|
156
|
|
|
return 'mixed'; |
|
157
|
|
|
} |
|
158
|
|
|
} |
|
159
|
|
|
} |
|
160
|
|
|
//[PHPCOMPRESSOR(remove,end)] |