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\CollectionContextInterface; |
29
|
|
|
use Baleen\Migrations\Shared\Event\PublisherInterface; |
30
|
|
|
use Baleen\Migrations\Version\VersionInterface; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Class MigrationRunner |
34
|
|
|
* |
35
|
|
|
* A Runner that emits domain events using an Emitter |
36
|
|
|
* |
37
|
|
|
* @author Gabriel Somoza <[email protected]> |
38
|
|
|
*/ |
39
|
|
|
final class MigrationRunner extends AbstractRunner |
40
|
|
|
{ |
41
|
|
|
/** @var MigrationBusInterface */ |
42
|
|
|
private $migrationBus; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* MigrationRunner constructor. |
46
|
|
|
* |
47
|
|
|
* @param MigrationBusInterface $migrationBus |
|
|
|
|
48
|
|
|
* @param PublisherInterface $publisher |
|
|
|
|
49
|
|
|
* @param CollectionContextInterface $context |
|
|
|
|
50
|
|
|
*/ |
51
|
27 |
|
public function __construct( |
52
|
|
|
MigrationBusInterface $migrationBus = null, |
53
|
|
|
PublisherInterface $publisher = null, |
54
|
|
|
CollectionContextInterface $context = null |
55
|
|
|
) { |
56
|
27 |
|
if (null === $migrationBus) { |
57
|
20 |
|
$migrationBus = MigrationBus::createDefaultBus(); |
58
|
20 |
|
} |
59
|
27 |
|
$this->migrationBus = $migrationBus; |
60
|
|
|
|
61
|
27 |
|
parent::__construct($publisher, $context); |
62
|
27 |
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Runs a single version using the specified options |
66
|
|
|
* |
67
|
|
|
* @param VersionInterface $version |
68
|
|
|
* @param OptionsInterface $options |
69
|
|
|
* |
70
|
|
|
* @return VersionInterface|false |
71
|
|
|
* |
72
|
|
|
* @throws RunnerException |
73
|
|
|
*/ |
74
|
23 |
|
public function run(VersionInterface $version, OptionsInterface $options) |
75
|
|
|
{ |
76
|
23 |
|
if (!$this->shouldMigrate($version, $options)) { |
77
|
14 |
|
if ($options->isExceptionOnSkip()) { |
78
|
2 |
|
throw new RunnerException(sprintf( |
79
|
2 |
|
'Cowardly refusing to run %s() on a version that is already "%s" (ID: %s).', |
80
|
2 |
|
$options->getDirection(), |
81
|
2 |
|
$options->getDirection(), |
82
|
2 |
|
$version->getId() |
83
|
2 |
|
)); |
84
|
|
|
} |
85
|
|
|
|
86
|
12 |
|
return false; // skip |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
// Dispatch MIGRATE_BEFORE |
90
|
13 |
|
$this->getPublisher()->publish(new MigrateBeforeEvent($version, $options, $this->getContext())); |
|
|
|
|
91
|
|
|
|
92
|
13 |
|
$this->doRun($version, $options); |
93
|
|
|
|
94
|
|
|
// this is safe because it won't get executed if an exception is thrown during migration |
95
|
13 |
|
$version->setMigrated($options->getDirection()->isUp()); |
96
|
|
|
|
97
|
|
|
// Dispatch MIGRATE_AFTER |
98
|
13 |
|
$this->getPublisher()->publish(new MigrateAfterEvent($version, $options, $this->getContext())); |
|
|
|
|
99
|
|
|
|
100
|
13 |
|
return $version; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Returns true if the operation is forced, or if the direction is the opposite to the state of the migration. |
105
|
|
|
* |
106
|
|
|
* @param VersionInterface $version |
107
|
|
|
* @param OptionsInterface $options |
108
|
|
|
* |
109
|
|
|
* @return bool |
110
|
|
|
*/ |
111
|
23 |
|
protected function shouldMigrate(VersionInterface $version, OptionsInterface $options) |
112
|
|
|
{ |
113
|
23 |
|
return $options->isForced() |
114
|
23 |
|
|| ($options->getDirection()->isUp() ^ $version->isMigrated()); // direction is opposite to state |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @param VersionInterface $version |
119
|
|
|
* @param OptionsInterface $options |
120
|
|
|
* @return bool |
|
|
|
|
121
|
|
|
*/ |
122
|
13 |
|
protected function doRun(VersionInterface $version, OptionsInterface $options) |
123
|
|
|
{ |
124
|
13 |
|
$command = $version->getMigrateCommand($options); |
125
|
13 |
|
$this->migrationBus->handle($command); |
126
|
13 |
|
} |
127
|
|
|
} |
128
|
|
|
|
This check looks for
@param
annotations 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.