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\OptionsInterface; |
24
|
|
|
use Baleen\Migrations\Service\Runner\Event\Migration\MigrateAfterEvent; |
25
|
|
|
use Baleen\Migrations\Service\Runner\Event\Migration\MigrateBeforeEvent; |
26
|
|
|
use Baleen\Migrations\Shared\Event\Context\CollectionContext; |
27
|
|
|
use Baleen\Migrations\Shared\Event\Context\CollectionContextInterface; |
28
|
|
|
use Baleen\Migrations\Shared\Event\Context\ContextInterface; |
29
|
|
|
use Baleen\Migrations\Shared\Event\Context\HasContextTrait; |
30
|
|
|
use Baleen\Migrations\Shared\Event\Publisher\HasInternalPublisherTrait; |
31
|
|
|
use Baleen\Migrations\Shared\Event\PublisherInterface; |
32
|
|
|
use Baleen\Migrations\Version\VersionInterface; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Class MigrationRunner |
36
|
|
|
* |
37
|
|
|
* A Runner that emits domain events using an Emitter |
38
|
|
|
* |
39
|
|
|
* @author Gabriel Somoza <[email protected]> |
40
|
|
|
*/ |
41
|
|
|
final class MigrationRunner implements MigrationRunnerInterface |
42
|
|
|
{ |
43
|
|
|
use HasInternalPublisherTrait; |
44
|
|
|
use HasContextTrait; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* MigrationRunner constructor. |
48
|
|
|
* |
49
|
|
|
* @param PublisherInterface $publisher |
|
|
|
|
50
|
|
|
* @param CollectionContextInterface $context |
|
|
|
|
51
|
|
|
*/ |
52
|
29 |
|
public function __construct( |
53
|
|
|
PublisherInterface $publisher = null, |
54
|
|
|
CollectionContextInterface $context = null |
55
|
|
|
) { |
56
|
29 |
|
if (null === $context) { |
57
|
23 |
|
$context = CollectionContext::createWithProgress(1, 1); |
58
|
23 |
|
} |
59
|
29 |
|
$this->setContext($context); |
60
|
|
|
|
61
|
29 |
|
$this->setPublisher($publisher); |
62
|
29 |
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Runs a single version using the specified options |
66
|
|
|
* |
67
|
|
|
* @param VersionInterface $version |
68
|
|
|
* @param OptionsInterface $options |
69
|
|
|
* |
70
|
|
|
* @return false|MigrateAfterEvent |
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 |
|
$version->migrate($options); // state will be changed |
93
|
|
|
|
94
|
|
|
// Dispatch MIGRATE_AFTER |
95
|
13 |
|
$event = new MigrateAfterEvent($version, $options, $this->getContext()); |
|
|
|
|
96
|
13 |
|
$this->getPublisher()->publish($event); |
97
|
|
|
|
98
|
13 |
|
return $event; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Returns true if the operation is forced, or if the direction is the opposite to the state of the migration. |
103
|
|
|
* |
104
|
|
|
* @param VersionInterface $version |
105
|
|
|
* @param OptionsInterface $options |
106
|
|
|
* |
107
|
|
|
* @return bool |
108
|
|
|
*/ |
109
|
23 |
|
protected function shouldMigrate(VersionInterface $version, OptionsInterface $options) |
110
|
|
|
{ |
111
|
23 |
|
return $options->isForced() |
112
|
23 |
|
|| ($options->getDirection()->isUp() ^ $version->isMigrated()); // direction is opposite to state |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @inheritdoc |
117
|
|
|
*/ |
118
|
18 |
|
final public function withContext(ContextInterface $context) { |
|
|
|
|
119
|
18 |
|
return new static($this->getPublisher(), $context); |
|
|
|
|
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|
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.