Passed
Pull Request — master (#8)
by
unknown
01:29
created

GenerateCommand::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
c 1
b 0
f 0
nc 1
nop 3
dl 0
loc 9
ccs 0
cts 9
cp 0
crap 2
rs 10
1
<?php
2
namespace Yiisoft\Yii\Cycle\Command;
3
4
use Spiral\Migrations\State;
5
use Symfony\Component\Console\Input\InputInterface;
6
use Symfony\Component\Console\Input\StreamableInputInterface;
7
use Symfony\Component\Console\Output\OutputInterface;
8
use Yiisoft\Yii\Cycle\Generator\ShowChangesGenerator;
9
10
class GenerateCommand extends BaseMigrationCommand
11
{
12
    protected static $defaultName = 'migrate/generate';
13
14
    public function configure(): void
15
    {
16
        $this->setDescription('Generates a migration');
17
    }
18
19
    protected function execute(InputInterface $input, OutputInterface $output): void
20
    {
21
        // check existing unapplied migrations
22
        $listAfter = $this->migrator->getMigrations();
23
        foreach ($listAfter as $migration) {
24
            if ($migration->getState()->getStatus() !== State::STATUS_EXECUTED) {
25
                $output->writeln('<fg=red>Outstanding migrations found, run `migrate/up` first.</>');
26
                return;
27
            }
28
        }
29
        // run generator
30
        $this->cycleOrmHelper->generateMigrations($this->migrator, $this->config, [
31
            new ShowChangesGenerator($output),
32
        ]);
33
34
        $listBefore = $this->migrator->getMigrations();
35
        $added = count($listBefore) - count($listAfter);
36
        $output->writeln("<info>Added {$added} file(s)</info>");
37
38
        // print added migrations
39
        if ($added > 0) {
40
            foreach ($listBefore as $migration) {
41
                if ($migration->getState()->getStatus() !== State::STATUS_EXECUTED) {
42
                    $output->writeln($migration->getState()->getName());
43
                }
44
            }
45
        } else {
46
            $output->writeln('<info>If you want to create new empty migration, use <fg=yellow>migrate/create</></info>');
47
48
            if ($input->isInteractive() && $input instanceof StreamableInputInterface) {
49
                $output->write('Would you like to create empty migration right now? (Y/n): ');
50
                $answer = fgets($input->getStream() ?? STDIN);
51
                if (!empty(trim($answer)) && !in_array(strtolower(trim($answer)), ['yes', 'y'])) {
52
                    return;
53
                }
54
                // get the name for a new migration
55
                $output->write('Please enter an unique name for the new migration: ');
56
                $name = trim(fgets($input->getStream() ?? STDIN));
57
                if (empty($name)) {
58
                    $output->writeln('<fg=red>You entered an empty name. Exit</>');
59
                    return;
60
                }
61
                // create an empty migration
62
                $this->createEmptyMigration($output, $name);
63
            }
64
        }
65
    }
66
}
67