|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Ubiquity\orm\reverse; |
|
4
|
|
|
|
|
5
|
|
|
use Ubiquity\orm\OrmUtils; |
|
6
|
|
|
use Ubiquity\db\reverse\DbGenerator; |
|
7
|
|
|
|
|
8
|
|
|
class TableReversor { |
|
9
|
|
|
private $model; |
|
10
|
|
|
private $fkFieldsToAdd=[]; |
|
11
|
|
|
private $fkFieldTypesToAdd=[]; |
|
12
|
|
|
private $metas; |
|
13
|
|
|
|
|
14
|
|
|
public function __construct($model=null){ |
|
15
|
|
|
$this->model=$model; |
|
16
|
|
|
} |
|
17
|
|
|
|
|
18
|
|
|
public function initFromClass(){ |
|
19
|
|
|
if(isset($this->model)) |
|
20
|
|
|
$this->metas=OrmUtils::getModelMetadata($this->model); |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
public function init($metas){ |
|
24
|
|
|
$this->metas=$metas; |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
public function generateSQL(DbGenerator $generator){ |
|
28
|
|
|
$table=$this->metas["#tableName"]; |
|
29
|
|
|
$primaryKeys=$this->metas["#primaryKeys"]; |
|
30
|
|
|
$serializables=$this->getSerializableFields(); |
|
31
|
|
|
$nullables=$this->metas["#nullable"]; |
|
32
|
|
|
$fieldTypes=$this->metas["#fieldTypes"]; |
|
33
|
|
|
$manyToOnes=$this->metas["#manyToOne"]; |
|
34
|
|
|
$manyToManys=[]; |
|
35
|
|
|
if(isset($this->metas["#manyToMany"])) |
|
36
|
|
|
$manyToManys=$this->metas["#manyToMany"]; |
|
37
|
|
|
$this->scanManyToManys($generator, $manyToManys,$table); |
|
|
|
|
|
|
38
|
|
|
$this->generatePks($generator, $primaryKeys, $table, $fieldTypes,$nullables); |
|
39
|
|
|
$this->generateForeignKeys($generator, $manyToOnes, $table); |
|
40
|
|
|
$serializables=\array_unique(\array_merge($serializables,$this->fkFieldsToAdd)); |
|
41
|
|
|
$fieldTypes=\array_merge($fieldTypes,$this->fkFieldTypesToAdd); |
|
42
|
|
|
$fieldsAttributes=$this->generateFieldsAttributes($serializables, $fieldTypes, $nullables); |
|
43
|
|
|
$generator->createTable($table, $fieldsAttributes); |
|
44
|
|
|
foreach ($this->fkFieldsToAdd as $fkField){ |
|
45
|
|
|
$generator->addKey($table, [$fkField],""); |
|
46
|
|
|
} |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
protected function getSerializableFields() { |
|
50
|
|
|
$notSerializable=$this->metas["#notSerializable"]; |
|
51
|
|
|
$fieldNames=$this->metas["#fieldNames"]; |
|
52
|
|
|
return \array_diff($fieldNames, $notSerializable); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
protected function scanManyToManys(DbGenerator $generator,$manyToManys){ |
|
56
|
|
|
foreach ($manyToManys as $member=>$manyToMany){ |
|
57
|
|
|
if(isset($this->metas["#joinTable"][$member])){ |
|
58
|
|
|
$annotJoinTable=$this->metas["#joinTable"][$member]; |
|
59
|
|
|
$generator->addManyToMany($annotJoinTable["name"], $manyToMany["targetEntity"]); |
|
60
|
|
|
} |
|
61
|
|
|
} |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
protected function generatePks(DbGenerator $generator,$primaryKeys,$table,$fieldTypes,$nullables){ |
|
65
|
|
|
$generator->addKey($table, $primaryKeys); |
|
66
|
|
|
if(\sizeof($primaryKeys)===1 && $generator->isInt($fieldTypes[$primaryKeys[0]])){ |
|
67
|
|
|
$generator->addAutoInc($table, $this->getFieldAttributes($generator, $primaryKeys[0], $nullables, $fieldTypes)); |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
protected function generateFieldsAttributes($serializables,$fieldTypes,$nullables){ |
|
72
|
|
|
$fieldsAttributes=[]; |
|
73
|
|
|
foreach ($serializables as $field){ |
|
74
|
|
|
$fieldsAttributes[]=$this->_generateFieldAttributes($field, $nullables, $fieldTypes); |
|
75
|
|
|
} |
|
76
|
|
|
return $fieldsAttributes; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
public function getFieldAttributes(DbGenerator $generator,$field,$nullables,$fieldTypes){ |
|
80
|
|
|
return $generator->generateField($this->_generateFieldAttributes($field, $nullables, $fieldTypes)); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
protected function _generateFieldAttributes($field,$nullables,$fieldTypes){ |
|
84
|
|
|
$nullable="NOT NULL"; |
|
85
|
|
|
if(\array_search($field, $nullables)!==false){ |
|
86
|
|
|
$nullable=""; |
|
87
|
|
|
} |
|
88
|
|
|
return ["name"=>$field,"type"=>$fieldTypes[$field],"extra"=>$nullable]; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
protected function generateForeignKey(DbGenerator $generator,$tableName,$member){ |
|
92
|
|
|
$fieldAnnot=OrmUtils::getMemberJoinColumns("", $member,$this->metas); |
|
93
|
|
|
if($fieldAnnot!==null){ |
|
94
|
|
|
$annotationArray=$fieldAnnot[1]; |
|
95
|
|
|
$referencesTableName=OrmUtils::getTableName($annotationArray["className"]); |
|
96
|
|
|
$referencesFieldName=OrmUtils::getFirstKey($annotationArray["className"]); |
|
97
|
|
|
$fkFieldName=$fieldAnnot[0]; |
|
98
|
|
|
$this->fkFieldsToAdd[]=$fkFieldName; |
|
99
|
|
|
$this->fkFieldTypesToAdd[$fkFieldName]=OrmUtils::getFieldType($annotationArray["className"], $referencesFieldName); |
|
100
|
|
|
$generator->addForeignKey($tableName, $fkFieldName, $referencesTableName, $referencesFieldName); |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
protected function generateForeignKeys(DbGenerator $generator,$manyToOnes,$tableName){ |
|
105
|
|
|
foreach ($manyToOnes as $member){ |
|
106
|
|
|
$this->generateForeignKey($generator, $tableName, $member); |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
|
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.
In this case you can add the
@ignorePhpDoc annotation to the duplicate definition and it will be ignored.