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

DiffCommand   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 130
Duplicated Lines 0 %

Test Coverage

Coverage 93.51%

Importance

Changes 0
Metric Value
dl 0
loc 130
ccs 72
cts 77
cp 0.9351
rs 10
c 0
b 0
f 0
wmc 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
B execute() 0 42 4
A procOpen() 0 3 1
B configure() 0 42 1
A createMigrationDiffGenerator() 0 9 1
A getSchemaProvider() 0 9 2
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
            ->setAliases(['diff'])
38 6
            ->setDescription('Generate a migration by comparing your current database to your mapping information.')
39 6
            ->setHelp(<<<EOT
40 6
The <info>%command.name%</info> command generates a migration by comparing your current database to your mapping information:
41
42
    <info>%command.full_name%</info>
43
44
You can optionally specify a <comment>--editor-cmd</comment> option to open the generated file in your favorite editor:
45
46
    <info>%command.full_name% --editor-cmd=mate</info>
47
EOT
48
            )
49 6
            ->addOption(
50 6
                'editor-cmd',
51 6
                null,
52 6
                InputOption::VALUE_OPTIONAL,
53 6
                'Open file with this command upon creation.'
54
            )
55 6
            ->addOption(
56 6
                'filter-expression',
57 6
                null,
58 6
                InputOption::VALUE_OPTIONAL,
59 6
                'Tables which are filtered by Regular Expression.'
60
            )
61 6
            ->addOption(
62 6
                'formatted',
63 6
                null,
64 6
                InputOption::VALUE_NONE,
65 6
                'Format the generated SQL.'
66
            )
67 6
            ->addOption(
68 6
                'line-length',
69 6
                null,
70 6
                InputOption::VALUE_OPTIONAL,
71 6
                'Max line length of unformatted lines.',
72 6
                120
73
            )
74
        ;
75 6
    }
76
77 6
    public function execute(
78
        InputInterface $input,
79
        OutputInterface $output
80
    ) : void {
81 6
        $filterExpression = $input->getOption('filter-expression') ?? null;
82 6
        $formatted        = (bool) $input->getOption('formatted');
83 6
        $lineLength       = (int) $input->getOption('line-length');
84
85 6
        if ($formatted) {
86 2
            if (! class_exists('SqlFormatter')) {
87
                throw new InvalidArgumentException(
88
                    'The "--formatted" option can only be used if the sql formatter is installed. Please run "composer require jdorn/sql-formatter".'
89
                );
90
            }
91
        }
92
93 6
        $versionNumber = $this->configuration->generateVersionNumber();
94
95 6
        $path = $this->createMigrationDiffGenerator()->generate(
96 6
            $versionNumber,
97 6
            $filterExpression,
98 6
            $formatted,
99 6
            $lineLength
100
        );
101
102 6
        $editorCommand = $input->getOption('editor-cmd');
103
104 6
        if ($editorCommand !== null) {
105 1
            $this->procOpen($editorCommand, $path);
106
        }
107
108 6
        $output->writeln([
109 6
            sprintf('Generated new migration class to "<info>%s</info>"', $path),
110 6
            '',
111 6
            sprintf(
112 6
                'To run just this migration for testing purposes, you can use <info>migrations:execute --up %s</info>',
113 6
                $versionNumber
114
            ),
115 6
            '',
116 6
            sprintf(
117 6
                'To revert the migration you can use <info>migrations:execute --down %s</info>',
118 6
                $versionNumber
119
            ),
120
        ]);
121 6
    }
122
123 5
    protected function createMigrationDiffGenerator() : MigrationDiffGenerator
124
    {
125 5
        return new MigrationDiffGenerator(
126 5
            $this->connection->getConfiguration(),
127 5
            $this->connection->getSchemaManager(),
128 5
            $this->getSchemaProvider(),
129 5
            $this->connection->getDatabasePlatform(),
130 5
            $this->dependencyFactory->getMigrationGenerator(),
131 5
            $this->dependencyFactory->getMigrationSqlGenerator()
132
        );
133
    }
134
135
    protected function procOpen(string $editorCommand, string $path) : void
136
    {
137
        proc_open($editorCommand . ' ' . escapeshellarg($path), [], $pipes);
138
    }
139
140 5
    private function getSchemaProvider() : SchemaProviderInterface
141
    {
142 5
        if ($this->schemaProvider === null) {
143 1
            $this->schemaProvider = new OrmSchemaProvider(
144 1
                $this->getHelper('entityManager')->getEntityManager()
145
            );
146
        }
147
148 5
        return $this->schemaProvider;
149
    }
150
}
151