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\Migrator; |
21
|
|
|
use Spiral\Migrations\Config\MigrationConfig; |
22
|
|
|
use Symfony\Component\Console\Command\Command; |
23
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
24
|
|
|
use Symfony\Component\Console\Input\InputOption; |
25
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
26
|
|
|
use Symfony\Component\Console\Question\ConfirmationQuestion; |
27
|
|
|
use Symfony\Component\Console\Style\SymfonyStyle; |
28
|
|
|
|
29
|
|
|
abstract class AbstractCommand extends Command |
30
|
|
|
{ |
31
|
|
|
/** @var Migrator */ |
32
|
|
|
protected $migrator; |
33
|
|
|
|
34
|
|
|
/** @var MigrationConfig */ |
35
|
|
|
protected $config; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param Migrator $migrator |
39
|
|
|
* @param MigrationConfig $config |
40
|
|
|
*/ |
41
|
|
|
public function __construct(Migrator $migrator, MigrationConfig $config) |
42
|
|
|
{ |
43
|
|
|
$this->migrator = $migrator; |
44
|
|
|
$this->config = $config; |
45
|
|
|
|
46
|
|
|
parent::__construct(); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Return's default Description. |
51
|
|
|
* |
52
|
|
|
* @return string |
53
|
|
|
*/ |
54
|
|
|
abstract protected function defineDescription(): string; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Return's default Option for command. |
58
|
|
|
* |
59
|
|
|
* @return array |
60
|
|
|
*/ |
61
|
|
|
protected function defineOption(): array |
62
|
|
|
{ |
63
|
|
|
return []; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Return default Options. |
68
|
|
|
* |
69
|
|
|
* @return array |
70
|
|
|
*/ |
71
|
|
|
protected function defineOptions(): array |
72
|
|
|
{ |
73
|
|
|
return \array_merge($this->defineOption(), [ |
74
|
|
|
new InputOption('force', 's', InputOption::VALUE_NONE, 'Skip safe environment check'), |
75
|
|
|
]); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* {@inheritdoc} |
80
|
|
|
*/ |
81
|
|
|
protected function configure(): void |
82
|
|
|
{ |
83
|
|
|
$this |
84
|
|
|
->setDefinition($this->defineOptions()) |
85
|
|
|
->setDescription($this->defineDescription()) |
86
|
|
|
; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @param OutputInterface $output |
91
|
|
|
* |
92
|
|
|
* @return bool |
93
|
|
|
*/ |
94
|
|
|
protected function verifyConfigured(OutputInterface $output): bool |
95
|
|
|
{ |
96
|
|
|
if (!$this->migrator->isConfigured()) { |
97
|
|
|
$output->writeln(''); |
98
|
|
|
$output->writeln( |
99
|
|
|
"<fg=red>Migrations are not configured yet, run '<info>migrations:init</info>' first.</fg=red>" |
100
|
|
|
); |
101
|
|
|
|
102
|
|
|
return false; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
return true; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Check if current environment is safe to run migration. |
110
|
|
|
* |
111
|
|
|
* @param InputInterface $input |
112
|
|
|
* @param SymfonyStyle $io |
113
|
|
|
* |
114
|
|
|
* @return bool |
115
|
|
|
*/ |
116
|
|
|
protected function verifyEnvironment(InputInterface $input, SymfonyStyle $io): bool |
117
|
|
|
{ |
118
|
|
|
if ($input->getOption('force') || $this->config->isSafe()) { |
119
|
|
|
//Safe to run |
120
|
|
|
return true; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
$io->newLine(); |
124
|
|
|
$io->writeln('<fg=red>Confirmation is required to run migrations!</fg=red>'); |
125
|
|
|
|
126
|
|
|
if (!$this->askConfirmation($io)) { |
127
|
|
|
$io->writeln('<comment>Cancelling operation...</comment>'); |
128
|
|
|
|
129
|
|
|
return false; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
return true; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* @param SymfonyStyle $io |
137
|
|
|
* |
138
|
|
|
* @return bool |
139
|
|
|
*/ |
140
|
|
|
protected function askConfirmation(SymfonyStyle $io): bool |
141
|
|
|
{ |
142
|
|
|
$confirmation = $io->askQuestion( |
143
|
|
|
new ConfirmationQuestion('<question>Would you like to continue?</question> ') |
144
|
|
|
); |
145
|
|
|
|
146
|
|
|
return $confirmation; |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
|