|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace WebComplete\core\utils\migration\commands; |
|
4
|
|
|
|
|
5
|
|
|
use Symfony\Component\Console\Command\Command; |
|
6
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
|
7
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
8
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
9
|
|
|
use Symfony\Component\Console\Question\ConfirmationQuestion; |
|
10
|
|
|
use WebComplete\core\cube\CubeManager; |
|
11
|
|
|
use WebComplete\core\utils\migration\MigrationService; |
|
12
|
|
|
|
|
13
|
|
|
class MigrationUpCommand extends Command |
|
14
|
|
|
{ |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @var CubeManager |
|
18
|
|
|
*/ |
|
19
|
|
|
protected $cubeManager; |
|
20
|
|
|
/** |
|
21
|
|
|
* @var MigrationService |
|
22
|
|
|
*/ |
|
23
|
|
|
protected $migrationService; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @param CubeManager $cubeManager |
|
27
|
|
|
* @param MigrationService $migrationService |
|
28
|
|
|
* |
|
29
|
|
|
* @throws \Symfony\Component\Console\Exception\LogicException |
|
30
|
|
|
*/ |
|
31
|
|
|
public function __construct(CubeManager $cubeManager, MigrationService $migrationService) |
|
32
|
|
|
{ |
|
33
|
|
|
parent::__construct(); |
|
34
|
|
|
$this->cubeManager = $cubeManager; |
|
35
|
|
|
$this->migrationService = $migrationService; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @throws \Symfony\Component\Console\Exception\InvalidArgumentException |
|
40
|
|
|
*/ |
|
41
|
|
|
protected function configure() |
|
42
|
|
|
{ |
|
43
|
|
|
$this->setName('migrate:up') |
|
44
|
|
|
->setDescription('Execute migration[s]') |
|
45
|
|
|
->addArgument('class', InputArgument::OPTIONAL, 'Migration class'); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @param InputInterface $input |
|
50
|
|
|
* @param OutputInterface $output |
|
51
|
|
|
* |
|
52
|
|
|
* @return null|int |
|
53
|
|
|
* @throws \Symfony\Component\Console\Exception\LogicException |
|
54
|
|
|
* @throws \Symfony\Component\Console\Exception\InvalidArgumentException |
|
55
|
|
|
* @throws \Psr\Container\NotFoundExceptionInterface |
|
56
|
|
|
* @throws \Psr\Container\ContainerExceptionInterface |
|
57
|
|
|
* @throws \Exception |
|
58
|
|
|
*/ |
|
59
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
|
60
|
|
|
{ |
|
61
|
|
|
if ($class = $input->getArgument('class')) { |
|
62
|
|
|
$newMigrations = [$class]; |
|
63
|
|
|
} else { |
|
64
|
|
|
$newMigrations = $this->getNewMigrations(); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
if (!$newMigrations) { |
|
68
|
|
|
$output->writeln('New migrations not found'); |
|
69
|
|
|
return null; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
$message = "New migrations: \n"; |
|
73
|
|
|
foreach ($newMigrations as $migrationClass) { |
|
74
|
|
|
$message .= $migrationClass . "\n"; |
|
75
|
|
|
} |
|
76
|
|
|
$output->writeln($message . "\n"); |
|
77
|
|
|
|
|
78
|
|
|
$helper = $this->getHelper('question'); |
|
79
|
|
|
$question = new ConfirmationQuestion('Continue? (Y/N): ', false); |
|
80
|
|
View Code Duplication |
if ($class || $helper->ask($input, $output, $question)) { |
|
|
|
|
|
|
81
|
|
|
foreach ($newMigrations as $migrationClass) { |
|
82
|
|
|
$output->writeln('Run: ' . $migrationClass); |
|
83
|
|
|
$this->migrationService->up($migrationClass); |
|
84
|
|
|
} |
|
85
|
|
|
$output->writeln('Done!'); |
|
86
|
|
|
return null; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
$output->writeln('Cancel'); |
|
90
|
|
|
return null; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* @return array |
|
95
|
|
|
*/ |
|
96
|
|
|
protected function getNewMigrations(): array |
|
97
|
|
|
{ |
|
98
|
|
|
$cubes = $this->cubeManager->getCubes(); |
|
99
|
|
|
$migrations = [[]]; |
|
100
|
|
|
foreach ($cubes as $cube) { |
|
101
|
|
|
$migrations[] = $cube->getMigrations(); |
|
102
|
|
|
} |
|
103
|
|
|
$allMigrations = \array_merge(...$migrations); |
|
|
|
|
|
|
104
|
|
|
\ksort($allMigrations, \SORT_STRING & \SORT_ASC); |
|
105
|
|
|
|
|
106
|
|
|
$newMigrations = []; |
|
107
|
|
|
foreach ($allMigrations as $k => $migrationClass) { |
|
108
|
|
|
if (!$this->migrationService->isRegistered($migrationClass)) { |
|
109
|
|
|
$newMigrations[$k] = $migrationClass; |
|
110
|
|
|
} |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
return $newMigrations; |
|
114
|
|
|
} |
|
115
|
|
|
} |
|
116
|
|
|
|
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.