1 | <?php |
||
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[$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() |
||
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) |
||
159 | } |
||
160 | //[PHPCOMPRESSOR(remove,end)] |