Completed
Push — master ( ead074...fd19fe )
by Jonathan
12s
created

GenerateCommand::procOpen()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 2
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\Migrations\Tools\Console\Command;
6
7
use Symfony\Component\Console\Input\InputInterface;
8
use Symfony\Component\Console\Input\InputOption;
9
use Symfony\Component\Console\Output\OutputInterface;
10
use function escapeshellarg;
11
use function proc_open;
12
use function sprintf;
13
14
class GenerateCommand extends AbstractCommand
15
{
16 9
    protected function configure() : void
17
    {
18
        $this
19 9
            ->setName('migrations:generate')
20 9
            ->setAliases(['generate'])
21 9
            ->setDescription('Generate a blank migration class.')
22 9
            ->addOption(
23 9
                'editor-cmd',
24 9
                null,
25 9
                InputOption::VALUE_OPTIONAL,
26 9
                'Open file with this command upon creation.'
27
            )
28 9
            ->setHelp(<<<EOT
29 9
The <info>%command.name%</info> command generates a blank migration class:
30
31
    <info>%command.full_name%</info>
32
33
You can optionally specify a <comment>--editor-cmd</comment> option to open the generated file in your favorite editor:
34
35
    <info>%command.full_name% --editor-cmd=mate</info>
36
EOT
37
        );
38
39 9
        parent::configure();
40 9
    }
41
42 3
    public function execute(InputInterface $input, OutputInterface $output) : void
43
    {
44 3
        $versionNumber = $this->configuration->generateVersionNumber();
45
46 3
        $migrationGenerator = $this->dependencyFactory->getMigrationGenerator();
47
48 3
        $path = $migrationGenerator->generateMigration($versionNumber);
49
50 3
        $editorCommand = $input->getOption('editor-cmd');
51
52 3
        if ($editorCommand !== null) {
53 1
            $this->procOpen($editorCommand, $path);
54
        }
55
56 3
        $output->writeln([
57 3
            sprintf('Generated new migration class to "<info>%s</info>"', $path),
58 3
            '',
59 3
            sprintf(
60 3
                'To run just this migration for testing purposes, you can use <info>migrations:execute --up %s</info>',
61 3
                $versionNumber
62
            ),
63 3
            '',
64 3
            sprintf(
65 3
                'To revert the migration you can use <info>migrations:execute --down %s</info>',
66 3
                $versionNumber
67
            ),
68
        ]);
69 3
    }
70
71
    protected function procOpen(string $editorCommand, string $path) : void
72
    {
73
        proc_open($editorCommand . ' ' . escapeshellarg($path), [], $pipes);
74
    }
75
}
76