1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace Limoncello\Application\Data; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Copyright 2015-2020 [email protected] |
7
|
|
|
* |
8
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
9
|
|
|
* you may not use this file except in compliance with the License. |
10
|
|
|
* You may obtain a copy of the License at |
11
|
|
|
* |
12
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
13
|
|
|
* |
14
|
|
|
* Unless required by applicable law or agreed to in writing, software |
15
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
16
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
17
|
|
|
* See the License for the specific language governing permissions and |
18
|
|
|
* limitations under the License. |
19
|
|
|
*/ |
20
|
|
|
|
21
|
|
|
use Limoncello\Contracts\Commands\IoInterface; |
22
|
|
|
use Limoncello\Contracts\FileSystem\FileSystemInterface; |
23
|
|
|
use Psr\Container\ContainerInterface; |
24
|
|
|
use function assert; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @package Limoncello\Application |
28
|
|
|
*/ |
29
|
|
|
class FileMigrationRunner extends BaseMigrationRunner |
30
|
|
|
{ |
31
|
|
|
/** |
32
|
|
|
* @var string |
33
|
|
|
*/ |
34
|
|
|
private $migrationsPath; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var string[] |
38
|
|
|
*/ |
39
|
|
|
private $migrationClasses; |
40
|
|
|
|
41
|
|
|
/** |
42
|
2 |
|
* @param IoInterface $inOut |
43
|
|
|
* @param string $migrationsPath |
44
|
2 |
|
*/ |
45
|
|
|
public function __construct(IoInterface $inOut, string $migrationsPath) |
46
|
2 |
|
{ |
47
|
|
|
parent::__construct($inOut); |
48
|
|
|
|
49
|
|
|
$this->setMigrationsPath($migrationsPath); |
50
|
|
|
} |
51
|
|
|
|
52
|
1 |
|
/** |
53
|
|
|
* @inheritdoc |
54
|
|
|
*/ |
55
|
1 |
View Code Duplication |
public function migrate(ContainerInterface $container): void |
|
|
|
|
56
|
|
|
{ |
57
|
1 |
|
// read & remember classes to migrate... |
58
|
|
|
assert($container->has(FileSystemInterface::class) === true); |
59
|
1 |
|
/** @var FileSystemInterface $fileSystem */ |
60
|
1 |
|
$fileSystem = $container->get(FileSystemInterface::class); |
61
|
|
|
|
62
|
1 |
|
$path = $this->getMigrationsPath(); |
63
|
|
|
assert($fileSystem->exists($path) === true); |
64
|
1 |
|
|
65
|
1 |
|
$this->getIO()->writeInfo("Migrations `$path` started." . PHP_EOL, IoInterface::VERBOSITY_VERBOSE); |
66
|
|
|
|
67
|
|
|
$migrationClasses = $fileSystem->requireFile($path); |
68
|
1 |
|
$this->setMigrationClasses($migrationClasses); |
69
|
|
|
|
70
|
|
|
// ... and run actual migration |
71
|
|
|
parent::migrate($container); |
72
|
|
|
} |
73
|
|
|
|
74
|
1 |
|
/** |
75
|
|
|
* @inheritdoc |
76
|
1 |
|
*/ |
77
|
|
|
protected function getMigrationClasses(): array |
78
|
|
|
{ |
79
|
|
|
return $this->migrationClasses; |
80
|
|
|
} |
81
|
|
|
|
82
|
1 |
|
/** |
83
|
|
|
* @return string |
84
|
1 |
|
*/ |
85
|
|
|
protected function getMigrationsPath(): string |
86
|
|
|
{ |
87
|
|
|
return $this->migrationsPath; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @param string $migrationsPath |
92
|
2 |
|
* |
93
|
|
|
* @return self |
94
|
2 |
|
*/ |
95
|
|
|
protected function setMigrationsPath(string $migrationsPath): self |
96
|
2 |
|
{ |
97
|
|
|
$this->migrationsPath = $migrationsPath; |
98
|
|
|
|
99
|
|
|
return $this; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @param string[] $migrationClasses |
104
|
1 |
|
* |
105
|
|
|
* @return self |
106
|
1 |
|
*/ |
107
|
|
|
private function setMigrationClasses(array $migrationClasses): self |
108
|
1 |
|
{ |
109
|
|
|
$this->migrationClasses = $migrationClasses; |
110
|
|
|
|
111
|
|
|
return $this; |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|
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.