1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of Biurad opensource projects. |
7
|
|
|
* |
8
|
|
|
* PHP version 7.2 and above required |
9
|
|
|
* |
10
|
|
|
* @author Divine Niiquaye Ibok <[email protected]> |
11
|
|
|
* @copyright 2019 Biurad Group (https://biurad.com/) |
12
|
|
|
* @license https://opensource.org/licenses/BSD-3-Clause License |
13
|
|
|
* |
14
|
|
|
* For the full copyright and license information, please view the LICENSE |
15
|
|
|
* file that was distributed with this source code. |
16
|
|
|
*/ |
17
|
|
|
|
18
|
|
|
namespace Biurad\Cycle\Commands\Migrations; |
19
|
|
|
|
20
|
|
|
use Biurad\Cycle\Generators\ShowChanges; |
21
|
|
|
use Biurad\Cycle\Migrator; |
22
|
|
|
use Cycle\Migrations\GenerateMigrations; |
23
|
|
|
use Cycle\Schema\Compiler; |
24
|
|
|
use Cycle\Schema\Registry; |
25
|
|
|
use Spiral\Migrations\Config\MigrationConfig; |
26
|
|
|
use Spiral\Migrations\State; |
27
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
28
|
|
|
use Symfony\Component\Console\Input\InputOption; |
29
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
30
|
|
|
|
31
|
|
|
final class CycleCommand extends AbstractCommand |
32
|
|
|
{ |
33
|
|
|
protected static $defaultName = 'migrations:cycle'; |
34
|
|
|
|
35
|
|
|
/** @var Registry */ |
36
|
|
|
private $registry; |
37
|
|
|
|
38
|
|
|
/** @var GenerateMigrations */ |
39
|
|
|
private $migrations; |
40
|
|
|
|
41
|
|
|
/** @var GeneratorInterface[] */ |
42
|
|
|
private $generators; |
43
|
|
|
|
44
|
|
|
public function __construct(Migrator $migrator, MigrationConfig $config, Registry $registry, GenerateMigrations $migrations, array $generators) |
45
|
|
|
{ |
46
|
|
|
$this->registry = $registry; |
47
|
|
|
$this->migrations = $migrations; |
48
|
|
|
$this->generators = $generators; |
49
|
|
|
|
50
|
|
|
parent::__construct($migrator, $config); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* {@inheritdoc} |
55
|
|
|
*/ |
56
|
|
|
protected function defineDescription(): string |
57
|
|
|
{ |
58
|
|
|
return 'Generate ORM schema migrations'; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* {@inheritdoc} |
63
|
|
|
*/ |
64
|
|
|
protected function defineOption(): array |
65
|
|
|
{ |
66
|
|
|
return [new InputOption('run', 'r', InputOption::VALUE_NONE, 'Automatically run generated migration.')]; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* {@inheritdoc} |
71
|
|
|
*/ |
72
|
|
|
protected function execute(InputInterface $input, OutputInterface $output): int |
73
|
|
|
{ |
74
|
|
|
if (!$this->verifyConfigured($output)) { |
75
|
|
|
return 1; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
foreach ($this->migrator->getMigrations() as $migration) { |
79
|
|
|
if ($migration->getState()->getStatus() !== State::STATUS_EXECUTED) { |
80
|
|
|
$output->writeln('<fg=red>Outstanding migrations found, run `migrate` first.</fg=red>'); |
81
|
|
|
|
82
|
|
|
return 1; |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
$show = new ShowChanges($output); |
87
|
|
|
(new Compiler())->compile($this->registry, \array_merge($this->generators, [$show])); |
88
|
|
|
|
89
|
|
|
if ($show->hasChanges()) { |
90
|
|
|
(new Compiler())->compile($this->registry, [$this->migrations]); |
91
|
|
|
|
92
|
|
|
if ($input->getOption('run')) { |
93
|
|
|
return $this->getApplication()->find('migrations:start') |
94
|
|
|
->run($input, $output); |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
return 0; |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|