1
|
|
|
<?php namespace Anomaly\Streams\Platform\Stream; |
2
|
|
|
|
3
|
|
|
use Anomaly\Streams\Platform\Stream\Contract\StreamInterface; |
4
|
|
|
use Illuminate\Database\Schema\Blueprint; |
5
|
|
|
use Illuminate\Database\Schema\Builder; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class StreamSchema |
9
|
|
|
* |
10
|
|
|
* @link http://pyrocms.com/ |
11
|
|
|
* @author PyroCMS, Inc. <[email protected]> |
12
|
|
|
* @author Ryan Thompson <[email protected]> |
13
|
|
|
*/ |
14
|
|
|
class StreamSchema |
15
|
|
|
{ |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* The schema builder. |
19
|
|
|
* |
20
|
|
|
* @var Builder |
21
|
|
|
*/ |
22
|
|
|
protected $schema; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Create a new StreamSchema instance. |
26
|
|
|
*/ |
27
|
|
|
public function __construct() |
28
|
|
|
{ |
29
|
|
|
$this->schema = app('db')->connection()->getSchemaBuilder(); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Create a table. |
34
|
|
|
* |
35
|
|
|
* @param StreamInterface |
36
|
|
|
*/ |
37
|
|
|
public function createTable(StreamInterface $stream) |
38
|
|
|
{ |
39
|
|
|
$table = $stream->getEntryTableName(); |
40
|
|
|
|
41
|
|
|
$this->schema->dropIfExists($table); |
42
|
|
|
|
43
|
|
|
$this->schema->create( |
44
|
|
|
$table, |
45
|
|
View Code Duplication |
function (Blueprint $table) use ($stream) { |
|
|
|
|
46
|
|
|
|
47
|
|
|
$table->engine = $stream->getConfig('database.engine'); |
48
|
|
|
|
49
|
|
|
$table->increments('id'); |
50
|
|
|
$table->integer('sort_order')->nullable(); |
51
|
|
|
$table->datetime('created_at'); |
52
|
|
|
$table->integer('created_by_id')->nullable(); |
53
|
|
|
$table->datetime('updated_at')->nullable(); |
54
|
|
|
$table->integer('updated_by_id')->nullable(); |
55
|
|
|
|
56
|
|
|
if ($stream->isTrashable()) { |
57
|
|
|
$table->datetime('deleted_at')->nullable(); |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Create translations table. |
65
|
|
|
* |
66
|
|
|
* @param StreamInterface $stream |
67
|
|
|
*/ |
68
|
|
|
public function createTranslationsTable(StreamInterface $stream) |
69
|
|
|
{ |
70
|
|
|
$this->schema->dropIfExists($stream->getEntryTranslationsTableName()); |
71
|
|
|
|
72
|
|
|
$this->schema->create( |
73
|
|
|
$stream->getEntryTranslationsTableName(), |
74
|
|
View Code Duplication |
function (Blueprint $table) use ($stream) { |
|
|
|
|
75
|
|
|
|
76
|
|
|
$table->engine = $stream->getConfig('database.engine'); |
77
|
|
|
|
78
|
|
|
$table->increments('id'); |
79
|
|
|
$table->integer('entry_id'); |
80
|
|
|
$table->datetime('created_at'); |
81
|
|
|
$table->integer('created_by_id')->nullable(); |
82
|
|
|
$table->datetime('updated_at')->nullable(); |
83
|
|
|
$table->integer('updated_by_id')->nullable(); |
84
|
|
|
$table->string('locale')->index(); |
85
|
|
|
} |
86
|
|
|
); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Rename a table. |
91
|
|
|
* |
92
|
|
|
* @param StreamInterface $from |
93
|
|
|
* @param StreamInterface $to |
94
|
|
|
*/ |
95
|
|
View Code Duplication |
public function renameTable(StreamInterface $from, StreamInterface $to) |
|
|
|
|
96
|
|
|
{ |
97
|
|
|
if ($from->getEntryTableName() === $to->getEntryTableName()) { |
98
|
|
|
return; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
$this->schema->rename($from->getEntryTableName(), $to->getEntryTableName()); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Rename a translations table. |
106
|
|
|
* |
107
|
|
|
* @param StreamInterface $from |
108
|
|
|
* @param StreamInterface $to |
109
|
|
|
*/ |
110
|
|
View Code Duplication |
public function renameTranslationsTable(StreamInterface $from, StreamInterface $to) |
|
|
|
|
111
|
|
|
{ |
112
|
|
|
if ($from->getEntryTranslationsTableName() === $to->getEntryTranslationsTableName()) { |
113
|
|
|
return; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
$this->schema->rename($from->getEntryTranslationsTableName(), $to->getEntryTranslationsTableName()); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Drop a table. |
121
|
|
|
* |
122
|
|
|
* @param $table |
123
|
|
|
*/ |
124
|
|
|
public function dropTable($table) |
125
|
|
|
{ |
126
|
|
|
$this->schema->dropIfExists($table); |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.