1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yawik\Migration\Controller; |
6
|
|
|
|
7
|
|
|
|
8
|
|
|
use Laminas\Mvc\Console\Controller\AbstractConsoleController; |
9
|
|
|
use Psr\Container\ContainerInterface; |
10
|
|
|
use Yawik\Migration\Contracts\MigratorInterface; |
11
|
|
|
use Yawik\Migration\Handler\MigrationHandler; |
12
|
|
|
use Yawik\Migration\Migrator\Version36; |
13
|
|
|
|
14
|
|
|
class ConsoleController extends AbstractConsoleController |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var iterable|MigratorInterface[] |
18
|
|
|
*/ |
19
|
|
|
private iterable $migrators; |
20
|
|
|
/** |
21
|
|
|
* @var MigrationHandler |
22
|
|
|
*/ |
23
|
|
|
private MigrationHandler $handler; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* MigrationController constructor. |
27
|
|
|
* |
28
|
|
|
* @param MigrationHandler $handler |
29
|
|
|
* @param iterable $migrators |
30
|
|
|
*/ |
31
|
|
|
public function __construct( |
32
|
|
|
MigrationHandler $handler, |
33
|
|
|
iterable $migrators |
34
|
|
|
) |
35
|
|
|
{ |
36
|
|
|
$this->migrators = $migrators; |
37
|
|
|
$this->handler = $handler; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public static function factory(ContainerInterface $container) |
41
|
|
|
{ |
42
|
|
|
$handler = $container->get(MigrationHandler::class); |
43
|
|
|
$migrators = []; |
44
|
|
|
$migrators[] = $container->get(Version36::class); |
45
|
|
|
|
46
|
|
|
return new self($handler, $migrators); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function migrateAction() |
50
|
|
|
{ |
51
|
|
|
$handler = $this->handler; |
52
|
|
|
foreach($this->migrators as $migrator){ |
53
|
|
|
$status = $handler->findOrCreate($migrator, true); |
54
|
|
|
if(!$status->isMigrated()){ |
55
|
|
|
$success = $migrator->migrate(); |
56
|
|
|
if($success){ |
57
|
|
|
$handler->migrated($migrator); |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
} |