Passed
Pull Request — master (#914)
by Asmir
02:33
created

ConsoleRunner::createApplication()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 2
dl 0
loc 8
ccs 6
cts 6
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\Migrations\Tools\Console;
6
7
use Doctrine\Migrations\DependencyFactory;
8
use Doctrine\Migrations\Tools\Console\Command\DiffCommand;
9
use Doctrine\Migrations\Tools\Console\Command\DoctrineCommand;
10
use Doctrine\Migrations\Tools\Console\Command\DumpSchemaCommand;
11
use Doctrine\Migrations\Tools\Console\Command\ExecuteCommand;
12
use Doctrine\Migrations\Tools\Console\Command\GenerateCommand;
13
use Doctrine\Migrations\Tools\Console\Command\LatestCommand;
14
use Doctrine\Migrations\Tools\Console\Command\ListCommand;
15
use Doctrine\Migrations\Tools\Console\Command\MigrateCommand;
16
use Doctrine\Migrations\Tools\Console\Command\RollupCommand;
17
use Doctrine\Migrations\Tools\Console\Command\StatusCommand;
18
use Doctrine\Migrations\Tools\Console\Command\SyncMetadataCommand;
19
use Doctrine\Migrations\Tools\Console\Command\UpToDateCommand;
20
use Doctrine\Migrations\Tools\Console\Command\VersionCommand;
21
use PackageVersions\Versions;
22
use RuntimeException;
23
use Symfony\Component\Console\Application;
24
use function file_exists;
25
use function getcwd;
26
use function is_readable;
27
use function sprintf;
28
use const DIRECTORY_SEPARATOR;
29
30
/**
31
 * The ConsoleRunner class is used to create the Symfony Console application for the Doctrine Migrations console.
32
 *
33
 * @internal
34
 *
35
 * @see bin/doctrine-migrations.php
36
 */
37
class ConsoleRunner
38
{
39 4
    public static function findDependencyFactory() : ?DependencyFactory
40
    {
41
        // Support for using the Doctrine ORM convention of providing a `cli-config.php` file.
42
        $configurationDirectories = [
43 4
            getcwd(),
44 4
            getcwd() . DIRECTORY_SEPARATOR . 'config',
45
        ];
46
47 4
        $configurationFile = null;
48 4
        foreach ($configurationDirectories as $configurationDirectory) {
49 4
            $configurationFilePath = $configurationDirectory . DIRECTORY_SEPARATOR . 'cli-config.php';
50
51 4
            if (! file_exists($configurationFilePath)) {
52 2
                continue;
53
            }
54
55 3
            $configurationFile = $configurationFilePath;
56 3
            break;
57
        }
58
59 4
        $dependencyFactory = null;
60 4
        if ($configurationFile !== null) {
61 3
            if (! is_readable($configurationFile)) {
62
                throw new RuntimeException(sprintf(
63
                    'Configuration file "%s" cannot be read.',
64
                    $configurationFile
65
                ));
66
            }
67
68 3
            $dependencyFactory = require $configurationFile;
69
        }
70
71 4
        if ($dependencyFactory !== null && ! ($dependencyFactory instanceof DependencyFactory)) {
72 1
            throw new RuntimeException(sprintf(
73 1
                'Configuration file "%s" must return an instance of "%s"',
74 1
                $configurationFile,
75 1
                DependencyFactory::class
76
            ));
77
        }
78
79 3
        return $dependencyFactory;
80
    }
81
82
    /** @param DoctrineCommand[] $commands */
83 1
    public static function run(array $commands = [], ?DependencyFactory $dependencyFactory = null) : void
84
    {
85 1
        $cli = static::createApplication($commands, $dependencyFactory);
86 1
        $cli->run();
87 1
    }
88
89
    /** @param DoctrineCommand[] $commands */
90 2
    public static function createApplication(array $commands = [], ?DependencyFactory $dependencyFactory = null) : Application
91
    {
92 2
        $cli = new Application('Doctrine Migrations', Versions::getVersion('doctrine/migrations'));
93 2
        $cli->setCatchExceptions(true);
94 2
        self::addCommands($cli, $dependencyFactory);
95 2
        $cli->addCommands($commands);
96
97 2
        return $cli;
98
    }
99
100 11
    public static function addCommands(Application $cli, ?DependencyFactory $dependencyFactory = null) : void
101
    {
102 11
        $cli->addCommands([
103 11
            new DumpSchemaCommand($dependencyFactory),
104 11
            new ExecuteCommand($dependencyFactory),
105 11
            new GenerateCommand($dependencyFactory),
106 11
            new LatestCommand($dependencyFactory),
107 11
            new MigrateCommand($dependencyFactory),
108 11
            new RollupCommand($dependencyFactory),
109 11
            new StatusCommand($dependencyFactory),
110 11
            new VersionCommand($dependencyFactory),
111 11
            new UpToDateCommand($dependencyFactory),
112 11
            new SyncMetadataCommand($dependencyFactory),
113 11
            new ListCommand($dependencyFactory),
114
        ]);
115
116 11
        if ($dependencyFactory === null || ! $dependencyFactory->hasEntityManager()) {
117 9
            return;
118
        }
119
120 2
        $cli->add(new DiffCommand($dependencyFactory));
121 2
    }
122
}
123