|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|
4
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
|
5
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
|
6
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
|
7
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
|
8
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
|
9
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
|
10
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
|
11
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
|
12
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
|
13
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
14
|
|
|
* |
|
15
|
|
|
* This software consists of voluntary contributions made by many individuals |
|
16
|
|
|
* and is licensed under the MIT license. For more information, see |
|
17
|
|
|
* <http://www.doctrine-project.org>. |
|
18
|
|
|
*/ |
|
19
|
|
|
|
|
20
|
|
|
namespace Baleen\Migrations\Service\Runner; |
|
21
|
|
|
|
|
22
|
|
|
use Baleen\Migrations\Exception\Service\Runner\RunnerException; |
|
23
|
|
|
use Baleen\Migrations\Migration\Command\MigrationBus; |
|
24
|
|
|
use Baleen\Migrations\Migration\Command\MigrationBusInterface; |
|
25
|
|
|
use Baleen\Migrations\Migration\OptionsInterface; |
|
26
|
|
|
use Baleen\Migrations\Service\Runner\Event\Migration\MigrateAfterEvent; |
|
27
|
|
|
use Baleen\Migrations\Service\Runner\Event\Migration\MigrateBeforeEvent; |
|
28
|
|
|
use Baleen\Migrations\Shared\Event\Context\CollectionContext; |
|
29
|
|
|
use Baleen\Migrations\Shared\Event\Context\CollectionContextInterface; |
|
30
|
|
|
use Baleen\Migrations\Shared\Event\Context\ContextInterface; |
|
31
|
|
|
use Baleen\Migrations\Shared\Event\Context\HasContextTrait; |
|
32
|
|
|
use Baleen\Migrations\Shared\Event\Publisher\HasInternalPublisherTrait; |
|
33
|
|
|
use Baleen\Migrations\Shared\Event\PublisherInterface; |
|
34
|
|
|
use Baleen\Migrations\Version\VersionInterface; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Class MigrationRunner |
|
38
|
|
|
* |
|
39
|
|
|
* A Runner that emits domain events using an Emitter |
|
40
|
|
|
* |
|
41
|
|
|
* @author Gabriel Somoza <[email protected]> |
|
42
|
|
|
*/ |
|
43
|
|
|
final class MigrationRunner implements ContextualRunnerInterface |
|
44
|
|
|
{ |
|
45
|
|
|
use HasInternalPublisherTrait; |
|
46
|
|
|
use HasContextTrait; |
|
47
|
|
|
|
|
48
|
|
|
/** @var MigrationBusInterface */ |
|
49
|
|
|
private $migrationBus; |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* MigrationRunner constructor. |
|
53
|
|
|
* |
|
54
|
|
|
* @param MigrationBusInterface $migrationBus |
|
|
|
|
|
|
55
|
|
|
* @param PublisherInterface $publisher |
|
|
|
|
|
|
56
|
|
|
* @param CollectionContextInterface $context |
|
|
|
|
|
|
57
|
|
|
*/ |
|
58
|
27 |
|
public function __construct( |
|
59
|
|
|
MigrationBusInterface $migrationBus = null, |
|
60
|
|
|
PublisherInterface $publisher = null, |
|
61
|
|
|
CollectionContextInterface $context = null |
|
62
|
|
|
) { |
|
63
|
27 |
|
if (null === $migrationBus) { |
|
64
|
20 |
|
$migrationBus = MigrationBus::createDefaultBus(); |
|
65
|
20 |
|
} |
|
66
|
27 |
|
$this->migrationBus = $migrationBus; |
|
67
|
|
|
|
|
68
|
27 |
|
if (null === $context) { |
|
69
|
22 |
|
$context = CollectionContext::createWithProgress(1, 1); |
|
70
|
22 |
|
} |
|
71
|
27 |
|
$this->setContext($context); |
|
72
|
|
|
|
|
73
|
27 |
|
$this->setPublisher($publisher); |
|
74
|
27 |
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Runs a single version using the specified options |
|
78
|
|
|
* |
|
79
|
|
|
* @param VersionInterface $version |
|
80
|
|
|
* @param OptionsInterface $options |
|
81
|
|
|
* |
|
82
|
|
|
* @return VersionInterface |
|
83
|
|
|
* |
|
84
|
|
|
* @throws RunnerException |
|
85
|
|
|
*/ |
|
86
|
23 |
|
public function run(VersionInterface $version, OptionsInterface $options) |
|
87
|
|
|
{ |
|
88
|
23 |
|
if (!$this->shouldMigrate($version, $options)) { |
|
89
|
14 |
|
if ($options->isExceptionOnSkip()) { |
|
90
|
2 |
|
throw new RunnerException(sprintf( |
|
91
|
2 |
|
'Cowardly refusing to run %s() on a version that is already "%s" (ID: %s).', |
|
92
|
2 |
|
$options->getDirection(), |
|
93
|
2 |
|
$options->getDirection(), |
|
94
|
2 |
|
$version->getId() |
|
95
|
2 |
|
)); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
12 |
|
return false; // skip |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
// Dispatch MIGRATE_BEFORE |
|
102
|
13 |
|
$this->getPublisher()->publish(new MigrateBeforeEvent($version, $options, $this->getContext())); |
|
|
|
|
|
|
103
|
|
|
|
|
104
|
13 |
|
$this->doRun($version, $options); |
|
105
|
|
|
|
|
106
|
|
|
// this is safe because it won't get executed if an exception is thrown during migration |
|
107
|
13 |
|
$version->setMigrated($options->getDirection()->isUp()); |
|
108
|
|
|
|
|
109
|
|
|
// Dispatch MIGRATE_AFTER |
|
110
|
13 |
|
$this->getPublisher()->publish(new MigrateAfterEvent($version, $options, $this->getContext())); |
|
|
|
|
|
|
111
|
|
|
|
|
112
|
13 |
|
return $version; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* Returns true if the operation is forced, or if the direction is the opposite to the state of the migration. |
|
117
|
|
|
* |
|
118
|
|
|
* @param VersionInterface $version |
|
119
|
|
|
* @param OptionsInterface $options |
|
120
|
|
|
* |
|
121
|
|
|
* @return bool |
|
122
|
|
|
*/ |
|
123
|
23 |
|
protected function shouldMigrate(VersionInterface $version, OptionsInterface $options) |
|
124
|
|
|
{ |
|
125
|
23 |
|
return $options->isForced() |
|
126
|
23 |
|
|| ($options->getDirection()->isUp() ^ $version->isMigrated()); // direction is opposite to state |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* @param VersionInterface $version |
|
131
|
|
|
* @param OptionsInterface $options |
|
132
|
|
|
* @return bool |
|
|
|
|
|
|
133
|
|
|
*/ |
|
134
|
13 |
|
protected function doRun(VersionInterface $version, OptionsInterface $options) |
|
135
|
|
|
{ |
|
136
|
13 |
|
$command = $version->getMigrateCommand($options); |
|
137
|
13 |
|
$this->migrationBus->handle($command); |
|
138
|
13 |
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* @inheritdoc |
|
142
|
|
|
*/ |
|
143
|
18 |
|
final public function withContext(ContextInterface $context) { |
|
|
|
|
|
|
144
|
18 |
|
return new static($this->migrationBus, $this->getPublisher(), $context); |
|
|
|
|
|
|
145
|
|
|
} |
|
146
|
|
|
} |
|
147
|
|
|
|
This check looks for
@paramannotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.