1
|
|
|
<?php namespace Anomaly\Streams\Platform\Database\Migration; |
2
|
|
|
|
3
|
|
|
use Anomaly\Streams\Platform\Addon\Addon; |
4
|
|
|
use Anomaly\Streams\Platform\Database\Migration\Command\Migrate; |
5
|
|
|
use Anomaly\Streams\Platform\Database\Migration\Command\Reset; |
6
|
|
|
use Illuminate\Foundation\Bus\DispatchesJobs; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class Migrator |
10
|
|
|
* |
11
|
|
|
* @link http://pyrocms.com/ |
12
|
|
|
* @author PyroCMS, Inc. <[email protected]> |
13
|
|
|
* @author Ryan Thompson <[email protected]> |
14
|
|
|
*/ |
15
|
|
|
class Migrator extends \Illuminate\Database\Migrations\Migrator |
16
|
|
|
{ |
17
|
|
|
|
18
|
|
|
use DispatchesJobs; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* The addon instance. |
22
|
|
|
* |
23
|
|
|
* @var Addon |
24
|
|
|
*/ |
25
|
|
|
protected $addon = null; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* The migration repository. |
29
|
|
|
* |
30
|
|
|
* @var MigrationRepository |
31
|
|
|
*/ |
32
|
|
|
protected $repository; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Rolls all of the currently applied migrations back. |
36
|
|
|
* |
37
|
|
|
* @param array|string $paths |
38
|
|
|
* @param bool $pretend |
39
|
|
|
* @return array |
40
|
|
|
*/ |
41
|
|
|
public function reset($paths = [], $pretend = false) |
42
|
|
|
{ |
43
|
|
|
$this->repository->setAddon($this->getAddon()); |
44
|
|
|
|
45
|
|
|
return parent::reset($paths, $pretend); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Rollback the last migration operation. |
50
|
|
|
* |
51
|
|
|
* @param array|string $paths |
52
|
|
|
* @param array $options |
53
|
|
|
* @return array |
54
|
|
|
*/ |
55
|
|
|
public function rollback($paths = [], array $options = []) |
56
|
|
|
{ |
57
|
|
|
$this->repository->setAddon($this->getAddon()); |
58
|
|
|
|
59
|
|
|
return parent::rollback($paths, $options); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Run "up" a migration instance. |
64
|
|
|
* |
65
|
|
|
* @param string $file |
66
|
|
|
* @param int $batch |
67
|
|
|
* @param bool $pretend |
68
|
|
|
* @return void |
69
|
|
|
*/ |
70
|
|
View Code Duplication |
protected function runUp($file, $batch, $pretend) |
|
|
|
|
71
|
|
|
{ |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Run our migrations first. |
75
|
|
|
* |
76
|
|
|
* @var Migration $migration |
77
|
|
|
*/ |
78
|
|
|
$migration = $this->resolve($file); |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Set the addon if there is |
82
|
|
|
* one contextually available. |
83
|
|
|
* |
84
|
|
|
* @var Addon $addon |
85
|
|
|
*/ |
86
|
|
|
if ($addon = $this->getAddon()) { |
87
|
|
|
$migration->setAddon($addon); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
if ($migration instanceof Migration) { |
91
|
|
|
$this->dispatch(new Migrate($migration)); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
parent::runUp($file, $batch, $pretend); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Run "down" a migration instance. |
99
|
|
|
* |
100
|
|
|
* @param string $file |
101
|
|
|
* @param object $migration |
102
|
|
|
* @param bool $pretend |
103
|
|
|
* @return void |
104
|
|
|
*/ |
105
|
|
View Code Duplication |
protected function runDown($file, $migration, $pretend) |
|
|
|
|
106
|
|
|
{ |
107
|
|
|
/** |
108
|
|
|
* Run our migrations first. |
109
|
|
|
* |
110
|
|
|
* @var Migration $migration |
111
|
|
|
*/ |
112
|
|
|
$migration = $this->resolve($file); |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Set the addon if there is |
116
|
|
|
* one contextually available. |
117
|
|
|
* |
118
|
|
|
* @var Addon $addon |
119
|
|
|
*/ |
120
|
|
|
if ($addon = $this->getAddon()) { |
121
|
|
|
$migration->setAddon($addon); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
if ($migration instanceof Migration) { |
125
|
|
|
$this->dispatch(new Reset($migration)); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
parent::runDown($file, $migration, $pretend); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Resolve a migration instance from a file. |
133
|
|
|
* |
134
|
|
|
* @param string $file |
135
|
|
|
* @return object |
136
|
|
|
*/ |
137
|
|
|
public function resolve($file) |
138
|
|
|
{ |
139
|
|
|
$migration = app((new MigrationName($file))->className()); |
140
|
|
|
|
141
|
|
|
$migration->migration = (new MigrationName($file))->migration(); |
142
|
|
|
|
143
|
|
|
return $migration; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Set the addon. |
148
|
|
|
* |
149
|
|
|
* @param Addon $addon |
150
|
|
|
*/ |
151
|
|
|
public function setAddon(Addon $addon) |
152
|
|
|
{ |
153
|
|
|
$this->addon = $addon; |
154
|
|
|
|
155
|
|
|
return $this; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* Get the addon. |
160
|
|
|
* |
161
|
|
|
* @return Addon |
162
|
|
|
*/ |
163
|
|
|
public function getAddon() |
164
|
|
|
{ |
165
|
|
|
return $this->addon; |
166
|
|
|
} |
167
|
|
|
} |
168
|
|
|
|
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.