Completed
Pull Request — master (#665)
by Jonathan
02:09
created

DiffCommand::configure()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 41
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 27
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 34
nc 1
nop 0
dl 0
loc 41
ccs 27
cts 27
cp 1
crap 1
rs 8.8571
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 Doctrine\Migrations\MigrationDiffGenerator;
8
use Doctrine\Migrations\Provider\OrmSchemaProvider;
9
use Doctrine\Migrations\Provider\SchemaProviderInterface;
10
use InvalidArgumentException;
11
use Symfony\Component\Console\Input\InputInterface;
12
use Symfony\Component\Console\Input\InputOption;
13
use Symfony\Component\Console\Output\OutputInterface;
14
use function class_exists;
15
use function escapeshellarg;
16
use function proc_open;
17
use function sprintf;
18
19
class DiffCommand extends AbstractCommand
20
{
21
    /** @var null|SchemaProviderInterface */
22
    protected $schemaProvider;
23
24 6
    public function __construct(?SchemaProviderInterface $schemaProvider = null)
25
    {
26 6
        $this->schemaProvider = $schemaProvider;
27
28 6
        parent::__construct();
29 6
    }
30
31 6
    protected function configure() : void
32
    {
33 6
        parent::configure();
34
35
        $this
36 6
            ->setName('migrations:diff')
37 6
            ->setDescription('Generate a migration by comparing your current database to your mapping information.')
38 6
            ->setHelp(<<<EOT
39 6
The <info>%command.name%</info> command generates a migration by comparing your current database to your mapping information:
40
41
    <info>%command.full_name%</info>
42
43
You can optionally specify a <comment>--editor-cmd</comment> option to open the generated file in your favorite editor:
44
45
    <info>%command.full_name% --editor-cmd=mate</info>
46
EOT
47
            )
48 6
            ->addOption(
49 6
                'editor-cmd',
50 6
                null,
51 6
                InputOption::VALUE_OPTIONAL,
52 6
                'Open file with this command upon creation.'
53
            )
54 6
            ->addOption(
55 6
                'filter-expression',
56 6
                null,
57 6
                InputOption::VALUE_OPTIONAL,
58 6
                'Tables which are filtered by Regular Expression.'
59
            )
60 6
            ->addOption(
61 6
                'formatted',
62 6
                null,
63 6
                InputOption::VALUE_NONE,
64 6
                'Format the generated SQL.'
65
            )
66 6
            ->addOption(
67 6
                'line-length',
68 6
                null,
69 6
                InputOption::VALUE_OPTIONAL,
70 6
                'Max line length of unformatted lines.',
71 6
                120
72
            )
73
        ;
74 6
    }
75
76 6
    public function execute(
77
        InputInterface $input,
78
        OutputInterface $output
79
    ) : void {
80 6
        $filterExpression = $input->getOption('filter-expression') ?? null;
81 6
        $formatted        = (bool) $input->getOption('formatted');
82 6
        $lineLength       = (int) $input->getOption('line-length');
83
84 6
        if ($formatted) {
85 2
            if (! class_exists('SqlFormatter')) {
86
                throw new InvalidArgumentException(
87
                    'The "--formatted" option can only be used if the sql formatter is installed. Please run "composer require jdorn/sql-formatter".'
88
                );
89
            }
90
        }
91
92 6
        $path = $this->createMigrationDiffGenerator()->generate(
93 6
            $filterExpression,
94 6
            $formatted,
95 6
            $lineLength
96
        );
97
98 6
        $editorCommand = $input->getOption('editor-cmd');
99
100 6
        if ($editorCommand !== null) {
101 1
            $this->procOpen($editorCommand, $path);
102
        }
103
104 6
        $output->writeln(sprintf('Generated new migration class to "<info>%s</info>"', $path));
105 6
    }
106
107 5
    protected function createMigrationDiffGenerator() : MigrationDiffGenerator
108
    {
109 5
        return new MigrationDiffGenerator(
110 5
            $this->configuration,
111 5
            $this->connection->getConfiguration(),
112 5
            $this->connection->getSchemaManager(),
113 5
            $this->getSchemaProvider(),
114 5
            $this->connection->getDatabasePlatform(),
115 5
            $this->dependencyFactory->getMigrationGenerator(),
116 5
            $this->dependencyFactory->getMigrationSqlGenerator()
117
        );
118
    }
119
120
    protected function procOpen(string $editorCommand, string $path) : void
121
    {
122
        proc_open($editorCommand . ' ' . escapeshellarg($path), [], $pipes);
123
    }
124
125 5
    private function getSchemaProvider() : SchemaProviderInterface
126
    {
127 5
        if ($this->schemaProvider === null) {
128 1
            $this->schemaProvider = new OrmSchemaProvider(
129 1
                $this->getHelper('entityManager')->getEntityManager()
130
            );
131
        }
132
133 5
        return $this->schemaProvider;
134
    }
135
}
136