|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Dami\Migration\Api; |
|
4
|
|
|
|
|
5
|
|
|
use Rentgen\Database\Column\DateTimeColumn; |
|
6
|
|
|
use Rentgen\Database\Column\TextColumn; |
|
7
|
|
|
use Rentgen\Database\Constraint\ForeignKey; |
|
|
|
|
|
|
8
|
|
|
use Rentgen\Database\Constraint\Unique; |
|
9
|
|
|
use Rentgen\Database\Index; |
|
10
|
|
|
use Rentgen\Database\Schema; |
|
11
|
|
|
use Rentgen\Database\Table; |
|
12
|
|
|
use Rentgen\Schema\Manipulation; |
|
13
|
|
|
|
|
14
|
|
|
class TableApi extends Table |
|
15
|
|
|
{ |
|
16
|
|
|
private $alterTable = false; |
|
|
|
|
|
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Constructor. |
|
20
|
|
|
* |
|
21
|
|
|
* @param string $table Table name. |
|
|
|
|
|
|
22
|
|
|
* @param Schema $schema |
|
23
|
|
|
* @param Manipulation $manipulation |
|
24
|
|
|
* @param array $actions |
|
25
|
|
|
*/ |
|
26
|
|
|
public function __construct($name, Schema $schema = null, Manipulation $manipulation, &$actions) |
|
27
|
|
|
{ |
|
28
|
|
|
parent::__construct($name, $schema); |
|
29
|
|
|
$this->manipulation = $manipulation; |
|
|
|
|
|
|
30
|
|
|
$this->actions = &$actions; |
|
|
|
|
|
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @param string $method A method name. |
|
35
|
|
|
* @param array $params Parameters. |
|
36
|
|
|
* |
|
37
|
|
|
* @return TableApi Self. |
|
38
|
|
|
*/ |
|
39
|
|
|
public function __call($method, $params) |
|
40
|
|
|
{ |
|
41
|
|
|
if ($method === 'addTimestamps') { |
|
42
|
|
|
$this->columns[] = new DateTimeColumn('created_at', array('not_null' => true)); |
|
43
|
|
|
$this->columns[] = new DateTimeColumn('updated_at', array('not_null' => true)); |
|
44
|
|
|
} elseif ($method === 'addUserstamps') { |
|
45
|
|
|
$this->columns[] = new IntegerColumn('created_by', array('not_null' => true, 'default' => 0)); |
|
46
|
|
|
$this->columns[] = new IntegerColumn('updated_by', array('not_null' => true, 'default' => 0)); |
|
47
|
|
|
} else { |
|
48
|
|
|
$this->columns[] = (new ColumnFactory($method, $params))->createInstance(); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
return $this; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Drop a column. |
|
56
|
|
|
* |
|
57
|
|
|
* @param string $name Column name. |
|
58
|
|
|
* |
|
59
|
|
|
* @return TableApi Self. |
|
60
|
|
|
*/ |
|
61
|
|
|
public function dropColumn($name) |
|
62
|
|
|
{ |
|
63
|
|
|
$column = new TextColumn($name); |
|
64
|
|
|
$column->setTable($this); |
|
65
|
|
|
|
|
66
|
|
|
$this->actions[] = function () use ($column) { |
|
67
|
|
|
return $this->manipulation->drop($column); |
|
68
|
|
|
}; |
|
69
|
|
|
|
|
70
|
|
|
return $this; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Add a foreign key. |
|
75
|
|
|
* |
|
76
|
|
|
* @param string $referenceTable Referenced table name. |
|
77
|
|
|
* @param string $referenceColumns Columns of referenced table. |
|
78
|
|
|
* @param array $options Optional options. |
|
79
|
|
|
* |
|
80
|
|
|
* @return TableApi Self. |
|
81
|
|
|
*/ |
|
82
|
|
|
public function addForeignKey($referenceTable, $referenceColumns, array $options = []) |
|
83
|
|
|
{ |
|
84
|
|
|
$schema = isset($options['schema']) ? new Schema($options['schema']) : null; |
|
85
|
|
|
|
|
86
|
|
|
$foreignKey = new ForeignKey($this, new Table($referenceTable, $schema)); |
|
87
|
|
|
$foreignKey->setReferencedColumns($referenceColumns); |
|
|
|
|
|
|
88
|
|
|
|
|
89
|
|
|
if (isset($options['column'])) { |
|
90
|
|
|
$foreignKey->setColumns($options['column']); |
|
91
|
|
|
} else { |
|
92
|
|
|
$foreignKey->setColumns($referenceColumns); |
|
|
|
|
|
|
93
|
|
|
} |
|
94
|
|
|
if (isset($options['update'])) { |
|
95
|
|
|
$foreignKey->setUpdateAction($options['update']); |
|
96
|
|
|
} |
|
97
|
|
|
if (isset($options['delete'])) { |
|
98
|
|
|
$foreignKey->setDeleteAction($options['delete']); |
|
99
|
|
|
} |
|
100
|
|
|
$this->actions[] = function () use ($foreignKey) { |
|
101
|
|
|
return $this->manipulation->create($foreignKey); |
|
102
|
|
|
}; |
|
103
|
|
|
|
|
104
|
|
|
return $this; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* Drop a foreign key. |
|
109
|
|
|
* |
|
110
|
|
|
* @param string $referenceTable Referenced table name. |
|
111
|
|
|
* @param string $referenceColumns Columns of referenced table. |
|
112
|
|
|
* @param array $options Optional options. |
|
113
|
|
|
* |
|
114
|
|
|
* @return TableApi Self. |
|
115
|
|
|
*/ |
|
116
|
|
|
public function dropForeignKey($referenceTable, $referenceColumns, array $options = []) |
|
117
|
|
|
{ |
|
118
|
|
|
$schema = isset($options['schema']) ? new Schema($options['schema']) : null; |
|
119
|
|
|
|
|
120
|
|
|
$foreignKey = new ForeignKey($this, new Table($referenceTable, $schema)); |
|
121
|
|
|
$foreignKey->setColumns($referenceColumns); |
|
|
|
|
|
|
122
|
|
|
$foreignKey->setReferencedColumns($referenceColumns); |
|
|
|
|
|
|
123
|
|
|
|
|
124
|
|
|
$this->actions[] = function () use ($foreignKey) { |
|
125
|
|
|
return $this->manipulation->drop($foreignKey); |
|
126
|
|
|
}; |
|
127
|
|
|
|
|
128
|
|
|
return $this; |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* Add a unique constraint. |
|
133
|
|
|
* |
|
134
|
|
|
* @param array $columns Unique columns. |
|
135
|
|
|
* |
|
136
|
|
|
* @return TableApi Self. |
|
137
|
|
|
*/ |
|
138
|
|
View Code Duplication |
public function addUnique($columns) |
|
|
|
|
|
|
139
|
|
|
{ |
|
140
|
|
|
$unique = new Unique($columns, $this); |
|
141
|
|
|
|
|
142
|
|
|
$this->actions[] = function () use ($unique) { |
|
143
|
|
|
return $this->manipulation->create($unique); |
|
144
|
|
|
}; |
|
145
|
|
|
|
|
146
|
|
|
return $this; |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* Drop a unique constraint. |
|
151
|
|
|
* |
|
152
|
|
|
* @param array $columns Unique columns. |
|
153
|
|
|
* |
|
154
|
|
|
* @return TableApi Self. |
|
155
|
|
|
*/ |
|
156
|
|
View Code Duplication |
public function dropUnique($columns) |
|
|
|
|
|
|
157
|
|
|
{ |
|
158
|
|
|
$unique = new Unique($columns, $this); |
|
159
|
|
|
|
|
160
|
|
|
$this->actions[] = function () use ($unique) { |
|
161
|
|
|
return $this->manipulation->drop($unique); |
|
162
|
|
|
}; |
|
163
|
|
|
|
|
164
|
|
|
return $this; |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
/** |
|
168
|
|
|
* Add a index on columns. |
|
169
|
|
|
* |
|
170
|
|
|
* @param array $columns Index columns. |
|
171
|
|
|
* |
|
172
|
|
|
* @return TableApi Self. |
|
173
|
|
|
*/ |
|
174
|
|
View Code Duplication |
public function addIndex($columns) |
|
|
|
|
|
|
175
|
|
|
{ |
|
176
|
|
|
$index = new Index($columns, $this); |
|
177
|
|
|
|
|
178
|
|
|
$this->actions[] = function () use ($index) { |
|
179
|
|
|
return $this->manipulation->create($index); |
|
180
|
|
|
}; |
|
181
|
|
|
|
|
182
|
|
|
return $this; |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
/** |
|
186
|
|
|
* Drop a index on columns. |
|
187
|
|
|
* |
|
188
|
|
|
* @param array $columns Index columns. |
|
189
|
|
|
* |
|
190
|
|
|
* @return TableApi Self. |
|
191
|
|
|
*/ |
|
192
|
|
View Code Duplication |
public function dropIndex($columns) |
|
|
|
|
|
|
193
|
|
|
{ |
|
194
|
|
|
$index = new Index($columns, $this); |
|
195
|
|
|
|
|
196
|
|
|
$this->actions[] = function () use ($index) { |
|
197
|
|
|
return $this->manipulation->drop($index); |
|
198
|
|
|
}; |
|
199
|
|
|
|
|
200
|
|
|
return $this; |
|
201
|
|
|
} |
|
202
|
|
|
} |
|
203
|
|
|
|
Let’s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let’s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: