Completed
Branch feature/pre-split (e801ec)
by Anton
03:11
created

TableBlueprint::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 3
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * components
4
 *
5
 * @author    Wolfy-J
6
 */
7
namespace Spiral\Migrations;
8
9
use Spiral\Database\Schemas\Prototypes\AbstractTable;
10
use Spiral\Migrations\Operations\Columns\AddColumn;
11
use Spiral\Migrations\Operations\Columns\AlterColumn;
12
use Spiral\Migrations\Operations\Columns\DropColumn;
13
use Spiral\Migrations\Operations\Columns\RenameColumn;
14
use Spiral\Migrations\Operations\Indexes\AddIndex;
15
use Spiral\Migrations\Operations\Indexes\AlterIndex;
16
use Spiral\Migrations\Operations\Indexes\DropIndex;
17
use Spiral\Migrations\Operations\References\AddReference;
18
use Spiral\Migrations\Operations\References\AlterReference;
19
use Spiral\Migrations\Operations\References\DropReference;
20
use Spiral\Migrations\Operations\Table\CreateTable;
21
use Spiral\Migrations\Operations\Table\DropTable;
22
use Spiral\Migrations\Operations\Table\PrimaryKeys;
23
use Spiral\Migrations\Operations\Table\RenameTable;
24
use Spiral\Migrations\Operations\Table\UpdateTable;
25
26
class TableBlueprint
27
{
28
    /**
29
     * @var CapsuleInterface
30
     */
31
    private $capsule = null;
32
33
    /**
34
     * Blueprint specific set of operations.
35
     *
36
     * @var array
37
     */
38
    private $operations = [];
39
40
    /**
41
     * @var string
42
     */
43
    private $table = '';
44
45
    /**
46
     * @var null|string
47
     */
48
    private $database = null;
49
50
    /**
51
     * @param CapsuleInterface $capsule
52
     * @param string           $table
53
     * @param string|null      $database
54
     */
55
    public function __construct(CapsuleInterface $capsule, string $table, string $database = null)
56
    {
57
        $this->capsule = $capsule;
58
        $this->table = $table;
59
        $this->database = $database;
60
    }
61
62
    /**
63
     * Get associated table schema.
64
     *
65
     * @return AbstractTable
66
     */
67
    public function getSchema(): AbstractTable
68
    {
69
        return $this->capsule->getSchema($this->table, $this->database);
70
    }
71
72
    /**
73
     * Example:
74
     * $table->addColumn('name', 'string', ['length' => 64]);
75
     * $table->addColumn('status', 'enum', [
76
     *      'values' => ['active', 'disabled']
77
     * ]);
78
     *
79
     * @param string $name
80
     * @param string $type
81
     * @param array  $options
82
     *
83
     * @return TableBlueprint
84
     */
85
    public function addColumn(string $name, string $type, array $options = []): self
86
    {
87
        return $this->addOperation(
88
            new AddColumn($this->database, $this->table, $name, $type, $options)
89
        );
90
    }
91
92
    /**
93
     * Example:
94
     * $table->alterColumn('name', 'string', ['length' => 128]);
95
     *
96
     * @param string $name
97
     * @param string $type
98
     * @param array  $options
99
     *
100
     * @return TableBlueprint
101
     */
102
    public function alterColumn(string $name, string $type, array $options = []): self
103
    {
104
        return $this->addOperation(
105
            new AlterColumn($this->database, $this->table, $name, $type, $options)
106
        );
107
    }
108
109
    /**
110
     * Example:
111
     * $table->renameColumn('column', 'new_name');
112
     *
113
     * @param string $name
114
     * @param string $newName
115
     *
116
     * @return TableBlueprint
117
     */
118
    public function renameColumn(string $name, string $newName): self
119
    {
120
        return $this->addOperation(
121
            new RenameColumn($this->database, $this->table, $name, $newName)
122
        );
123
    }
124
125
    /**
126
     * Example:
127
     * $table->dropColumn('email');
128
     *
129
     * @param string $name
130
     *
131
     * @return TableBlueprint
132
     */
133
    public function dropColumn(string $name): self
134
    {
135
        return $this->addOperation(
136
            new DropColumn($this->database, $this->table, $name)
137
        );
138
    }
139
140
    /**
141
     * Example:
142
     * $table->addIndex(['email'], ['unique' => true]);
143
     *
144
     * @param array $columns
145
     * @param array $options
146
     *
147
     * @return TableBlueprint
148
     */
149
    public function addIndex(array $columns, array $options = []): self
150
    {
151
        return $this->addOperation(
152
            new AddIndex($this->database, $this->table, $columns, $options)
153
        );
154
    }
155
156
    /**
157
     * Example:
158
     * $table->alterIndex(['email'], ['unique' => false]);
159
     *
160
     * @param array $columns
161
     * @param array $options
162
     *
163
     * @return TableBlueprint
164
     */
165
    public function alterIndex(array $columns, array $options): self
166
    {
167
        return $this->addOperation(
168
            new AlterIndex($this->database, $this->table, $columns, $options)
169
        );
170
    }
171
172
    /**
173
     * Example:
174
     * $table->dropIndex(['email']);
175
     *
176
     * @param array $columns
177
     *
178
     * @return TableBlueprint
179
     */
180
    public function dropIndex(array $columns): self
181
    {
182
        return $this->addOperation(
183
            new DropIndex($this->database, $this->table, $columns)
184
        );
185
    }
186
187
    /**
188
     * Example:
189
     * $table->addForeignKey('user_id', 'users', 'id', ['delete' => 'CASCADE']);
190
     *
191
     * @param string $column
192
     * @param string $foreignTable Database isolation prefix will be automatically added.
193
     * @param string $foreignKey
194
     * @param array  $options
195
     *
196
     * @return TableBlueprint
197
     */
198
    public function addForeignKey(
199
        string $column,
200
        string $foreignTable,
201
        string $foreignKey,
202
        array $options = []
203
    ): self {
204
        return $this->addOperation(
205
            new AddReference(
206
                $this->database,
207
                $this->table,
208
                $column,
209
                $foreignTable,
210
                $foreignKey,
211
                $options
212
            )
213
        );
214
    }
215
216
217
    /**
218
     * Example:
219
     * $table->alterForeignKey('user_id', 'users', 'id', ['delete' => 'NO ACTION']);
220
     *
221
     * @param string $column
222
     * @param string $foreignTable
223
     * @param string $foreignKey
224
     * @param array  $options
225
     *
226
     * @return TableBlueprint
227
     */
228
    public function alterForeignKey(
229
        string $column,
230
        string $foreignTable,
231
        string $foreignKey,
232
        array $options = []
233
    ): self {
234
        return $this->addOperation(
235
            new AlterReference(
236
                $this->database,
237
                $this->table,
238
                $column,
239
                $foreignTable,
240
                $foreignKey,
241
                $options
242
            )
243
        );
244
    }
245
246
    /**
247
     * Example:
248
     * $table->dropForeignKey('user_id');
249
     *
250
     * @param string $column
251
     *
252
     * @return TableBlueprint
253
     */
254
    public function dropForeignKey(string $column): self
255
    {
256
        return $this->addOperation(
257
            new DropReference($this->database, $this->table, $column)
258
        );
259
    }
260
261
    /**
262
     * Set table primary keys index. Attention, you can only call it when table being created.
263
     *
264
     * @param array $keys
265
     *
266
     * @return TableBlueprint
267
     */
268
    public function setPrimaryKeys(array $keys): self
269
    {
270
        return $this->addOperation(
271
            new PrimaryKeys($this->database, $this->table, $keys)
272
        );
273
    }
274
275
    /**
276
     * Create table schema.
277
     */
278
    public function create()
279
    {
280
        $this->addOperation(
281
            new CreateTable($this->database, $this->table)
282
        );
283
284
        $this->execute();
285
    }
286
287
    /**
288
     * Update table schema.
289
     */
290
    public function update()
291
    {
292
        $this->addOperation(
293
            new UpdateTable($this->database, $this->table)
294
        );
295
296
        $this->execute();
297
    }
298
299
    /**
300
     * Drop table.
301
     */
302
    public function drop()
303
    {
304
        $this->addOperation(
305
            new DropTable($this->database, $this->table)
306
        );
307
308
        $this->execute();
309
    }
310
311
    /**
312
     * Rename table.
313
     *
314
     * @param string $newName
315
     */
316
    public function rename(string $newName)
317
    {
318
        $this->addOperation(
319
            new RenameTable($this->database, $this->table, $newName)
320
        );
321
322
        $this->execute();
323
    }
324
325
    /**
326
     * Register new operation.
327
     *
328
     * @param OperationInterface $operation
329
     *
330
     * @return TableBlueprint
331
     */
332
    public function addOperation(OperationInterface $operation): self
333
    {
334
        $this->operations[] = $operation;
335
336
        return $this;
337
    }
338
339
    /**
340
     * Execute blueprint operations.
341
     */
342
    private function execute()
343
    {
344
        $this->capsule->execute($this->operations);
345
    }
346
}