|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace ntentan\nibii; |
|
4
|
|
|
|
|
5
|
|
|
class ModelDescription { |
|
6
|
|
|
|
|
7
|
|
|
private $fields = []; |
|
8
|
|
|
private $primaryKey = []; |
|
9
|
|
|
private $uniqueKeys = []; |
|
10
|
|
|
private $autoPrimaryKey = false; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* |
|
14
|
|
|
* @var array<Relationship> |
|
15
|
|
|
*/ |
|
16
|
|
|
private $relationships = []; |
|
17
|
|
|
private $table; |
|
18
|
|
|
private $name; |
|
19
|
|
|
private $container; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* |
|
23
|
|
|
* @param RecordWrapper $model |
|
24
|
|
|
*/ |
|
25
|
28 |
|
public function __construct(ORMContext $context, $model) { |
|
26
|
28 |
|
$this->table = $model->getDBStoreInformation()['unquoted_table']; |
|
27
|
28 |
|
$this->name = $context->getModelName((new \ReflectionClass($model))->getName()); |
|
28
|
28 |
|
$this->container = $context->getContainer(); |
|
29
|
28 |
|
$relationships = $model->getRelationships(); |
|
30
|
28 |
|
$adapter = $context->getContainer()->resolve(DriverAdapter::class); |
|
31
|
28 |
|
$schema = array_values($context->getDbContext()->getDriver()->describeTable($this->table))[0]; |
|
32
|
28 |
|
$this->autoPrimaryKey = $schema['auto_increment']; |
|
33
|
|
|
|
|
34
|
28 |
|
foreach ($schema['columns'] as $field => $details) { |
|
35
|
28 |
|
$this->fields[$field] = [ |
|
36
|
28 |
|
'type' => $adapter->mapDataTypes($details['type']), |
|
37
|
28 |
|
'required' => !$details['nulls'], |
|
38
|
28 |
|
'default' => $details['default'], |
|
39
|
28 |
|
'name' => $field |
|
40
|
|
|
]; |
|
41
|
28 |
|
if (isset($details['default'])) { |
|
42
|
20 |
|
$this->fields[$field]['default'] = $details['default']; |
|
43
|
|
|
} |
|
44
|
28 |
|
if (isset($details['length'])) { |
|
45
|
28 |
|
$this->fields[$field]['length'] = $details['length']; |
|
46
|
|
|
} |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
28 |
|
$this->appendConstraints($schema['primary_key'], $this->primaryKey, true); |
|
50
|
28 |
|
$this->appendConstraints($schema['unique_keys'], $this->uniqueKeys); |
|
51
|
|
|
|
|
52
|
28 |
|
foreach ($relationships as $type => $relations) { |
|
53
|
28 |
|
$this->createRelationships($type, $relations); |
|
54
|
|
|
} |
|
55
|
28 |
|
} |
|
56
|
|
|
|
|
57
|
28 |
|
private function appendConstraints($constraints, &$key, $flat = false) { |
|
58
|
28 |
|
foreach ($constraints as $constraint) { |
|
59
|
28 |
|
if ($flat) { |
|
60
|
28 |
|
$key = $constraint['columns']; |
|
61
|
28 |
|
break; |
|
62
|
|
|
} else { |
|
63
|
16 |
|
$key[] = array( |
|
64
|
16 |
|
'fields' => $constraint['columns'] |
|
65
|
|
|
); |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
28 |
|
} |
|
69
|
|
|
|
|
70
|
28 |
|
private function getRelationshipDetails($relationship) { |
|
71
|
28 |
|
$relationshipDetails = []; |
|
|
|
|
|
|
72
|
28 |
|
if (is_string($relationship)) { |
|
73
|
|
|
$relationshipDetails = [ |
|
74
|
22 |
|
'model' => $relationship, |
|
75
|
22 |
|
'name' => $relationship, |
|
76
|
22 |
|
'foreign_key' => '', |
|
77
|
22 |
|
'local_key' => '' |
|
78
|
|
|
]; |
|
79
|
6 |
|
} else if (is_array($relationship)) { |
|
80
|
|
|
$relationshipDetails = [ |
|
81
|
6 |
|
'model' => $relationship[0], |
|
82
|
6 |
|
'name' => isset($relationship['as']) ? $relationship['as'] : $relationship[0], |
|
83
|
6 |
|
'foreign_key' => isset($relationship['foreign_key']) ? $relationship['foreign_key'] : '', |
|
84
|
6 |
|
'local_key' => isset($relationship['local_key']) ? $relationship['local_key'] : '', |
|
85
|
|
|
]; |
|
86
|
|
|
} else { |
|
87
|
|
|
return null; |
|
88
|
|
|
} |
|
89
|
28 |
|
$relationshipDetails['local_table'] = $this->table; |
|
90
|
28 |
|
if(isset($relationship['through'])) { |
|
91
|
|
|
$relationshipDetails['through'] = $relationship['through']; |
|
92
|
|
|
} |
|
93
|
28 |
|
return $relationshipDetails; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
28 |
|
private function createRelationships($type, $relationships) { |
|
97
|
28 |
|
foreach ($relationships as $relationship) { |
|
98
|
28 |
|
$relationship = $this->getRelationshipDetails($relationship); |
|
99
|
28 |
|
$class = "\\ntentan\\nibii\\relationships\\{$type}Relationship"; |
|
100
|
28 |
|
$relationshipObject = $this->container->resolve($class); |
|
101
|
28 |
|
$relationshipObject->setOptions($relationship); |
|
102
|
28 |
|
$relationshipObject->setup($this->name, $this->table, $this->primaryKey); |
|
103
|
28 |
|
$this->relationships[$relationship['name']] = $relationshipObject; |
|
104
|
|
|
} |
|
105
|
28 |
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* |
|
109
|
|
|
* @return array<Relationship> |
|
110
|
|
|
*/ |
|
111
|
18 |
|
public function getRelationships() { |
|
112
|
18 |
|
return $this->relationships; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
24 |
|
public function getPrimaryKey() { |
|
116
|
24 |
|
return $this->primaryKey; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
10 |
|
public function getAutoPrimaryKey() { |
|
120
|
10 |
|
return $this->autoPrimaryKey; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
10 |
|
public function getFields() { |
|
124
|
10 |
|
return $this->fields; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
10 |
|
public function getUniqueKeys() { |
|
128
|
10 |
|
return $this->uniqueKeys; |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
} |
|
132
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.