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
|
|
|
$entity = $this->entityName($table); |
44
|
|
|
$metadata = new $this->metadataClass($this->fullEntityName($entity)); |
45
|
|
|
$metadata->entity = $entity; |
46
|
|
|
$metadata->entityName = $table; |
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[$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
|
|
|
$metadata->nullable[$columnRow['Field']] = $columnRow['Null']; |
79
|
|
|
|
80
|
|
|
// Store entity primary field |
81
|
|
|
if (strtolower($columnRow['Key']) === 'pri') { |
82
|
|
|
$metadata->primaryField = $columnRow['Field']; |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
return $metadataCollection; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Get real entities from database. |
92
|
|
|
* |
93
|
|
|
* @return array Get collection of database entities metadata |
94
|
|
|
*/ |
95
|
|
|
public function getEntities() |
96
|
|
|
{ |
97
|
|
|
// Get tables data |
98
|
|
|
return $this->database->fetch( |
99
|
|
|
'SELECT |
100
|
|
|
`TABLES`.`TABLE_NAME` as `Table`, |
101
|
|
|
`COLUMNS`.`COLUMN_NAME` as `Field`, |
102
|
|
|
`COLUMNS`.`DATA_TYPE` as `Type`, |
103
|
|
|
`COLUMNS`.`IS_NULLABLE` as `Null`, |
104
|
|
|
`COLUMNS`.`COLUMN_KEY` as `Key`, |
105
|
|
|
`COLUMNS`.`COLUMN_DEFAULT` as `Default`, |
106
|
|
|
`COLUMNS`.`EXTRA` as `Extra` |
107
|
|
|
FROM `information_schema`.`TABLES` as `TABLES` |
108
|
|
|
LEFT JOIN `information_schema`.`COLUMNS` as `COLUMNS` |
109
|
|
|
ON `TABLES`.`TABLE_NAME`=`COLUMNS`.`TABLE_NAME` |
110
|
|
|
WHERE `TABLES`.`TABLE_SCHEMA`="' . $this->database->database() . '" |
111
|
|
|
AND `COLUMNS`.`TABLE_SCHEMA`="' . $this->database->database() . '" |
112
|
|
|
AND `TABLES`.`TABLE_NAME` != "cms_version" |
113
|
|
|
'); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Get PHP data type from database column type. |
118
|
|
|
* |
119
|
|
|
* @param string $type Database column type |
120
|
|
|
* |
121
|
|
|
* @return string PHP data type |
122
|
|
|
*/ |
123
|
|
|
protected function databaseTypeToPHP($type) |
124
|
|
|
{ |
125
|
|
|
switch (strtoupper($type)) { |
126
|
|
|
case 'DECIMAL': |
127
|
|
|
case 'TINY': |
128
|
|
|
case 'TINYINT': |
129
|
|
|
case 'BIT': |
130
|
|
|
case 'INT': |
131
|
|
|
case 'SMALLINT': |
132
|
|
|
case 'MEDIUMINT': |
133
|
|
|
case 'INTEGER': |
134
|
|
|
case 'BIGINT': |
135
|
|
|
case 'SHORT': |
136
|
|
|
case 'LONG': |
137
|
|
|
case 'LONGLONG': |
138
|
|
|
case 'INT24': |
139
|
|
|
return 'int'; |
140
|
|
|
case 'FLOAT': |
141
|
|
|
return 'float'; |
142
|
|
|
case 'DOUBLE': |
143
|
|
|
case 'DOUBLE PRECISION': |
144
|
|
|
return 'double'; |
145
|
|
|
case 'DATETIME': |
146
|
|
|
case 'DATE': |
147
|
|
|
case 'TIMESTAMP': |
148
|
|
|
return 'int'; |
149
|
|
|
case 'BOOL': |
150
|
|
|
case 'BOOLEAN': |
151
|
|
|
return 'bool'; |
152
|
|
|
case 'CHAR': |
153
|
|
|
case 'VARCHAR': |
154
|
|
|
case 'TEXT': |
155
|
|
|
return 'string'; |
156
|
|
|
default: |
157
|
|
|
return 'mixed'; |
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
//[PHPCOMPRESSOR(remove,end)] |