Passed
Pull Request — master (#7448)
by Ilya
14:31
created

ConsoleRunner::run()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 4
ccs 0
cts 3
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\ORM\Tools\Console;
6
7
use Doctrine\DBAL\Tools\Console as DBALConsole;
8
use Doctrine\ORM\EntityManagerInterface;
9
use Doctrine\ORM\Tools\Console\Helper\EntityManagerHelper;
10
use OutOfBoundsException;
11
use PackageVersions\Versions;
12
use Symfony\Component\Console\Application;
13
use Symfony\Component\Console\Command\Command as SymfonyCommand;
14
use Symfony\Component\Console\Helper\HelperSet;
15
16
/**
17
 * Handles running the Console Tools inside Symfony Console context.
18
 */
19
final class ConsoleRunner
20
{
21
    /**
22
     * Create a Symfony Console HelperSet
23
     */
24
    public static function createHelperSet(EntityManagerInterface $entityManager) : HelperSet
25
    {
26
        return new HelperSet(
27
            [
28
                'db' => new DBALConsole\Helper\ConnectionHelper($entityManager->getConnection()),
29
                'em' => new EntityManagerHelper($entityManager),
30
            ]
31
        );
32
    }
33
34
    /**
35
     * Runs console with the given helper set.
36
     *
37
     * @param SymfonyCommand[] $commands
38
     */
39
    public static function run(HelperSet $helperSet, array $commands = []) : void
40
    {
41
        $cli = self::createApplication($helperSet, $commands);
42
        $cli->run();
43
    }
44
45
    /**
46
     * Creates a console application with the given helperset and
47
     * optional commands.
48
     *
49
     * @param SymfonyCommand[] $commands
50
     *
51
     * @throws OutOfBoundsException
52
     */
53 2
    public static function createApplication(HelperSet $helperSet, array $commands = []) : Application
54
    {
55 2
        $cli = new Application('Doctrine Command Line Interface', Versions::getVersion('doctrine/orm'));
56 2
        $cli->setCatchExceptions(true);
57 2
        $cli->setHelperSet($helperSet);
58 2
        self::addCommands($cli);
59 2
        $cli->addCommands($commands);
60
61 2
        return $cli;
62
    }
63
64 2
    public static function addCommands(Application $cli) : void
65
    {
66 2
        $cli->addCommands(
67
            [
68
                // DBAL Commands
69 2
                new DBALConsole\Command\ImportCommand(),
70 2
                new DBALConsole\Command\ReservedWordsCommand(),
71 2
                new DBALConsole\Command\RunSqlCommand(),
72
73
                // ORM Commands
74 2
                new Command\ClearCache\CollectionRegionCommand(),
75 2
                new Command\ClearCache\EntityRegionCommand(),
76 2
                new Command\ClearCache\MetadataCommand(),
77 2
                new Command\ClearCache\QueryCommand(),
78 2
                new Command\ClearCache\QueryRegionCommand(),
79 2
                new Command\ClearCache\ResultCommand(),
80 2
                new Command\SchemaTool\CreateCommand(),
81 2
                new Command\SchemaTool\UpdateCommand(),
82 2
                new Command\SchemaTool\DropCommand(),
83 2
                new Command\EnsureProductionSettingsCommand(),
84 2
                new Command\GenerateProxiesCommand(),
85 2
                new Command\RunDqlCommand(),
86 2
                new Command\ValidateSchemaCommand(),
87 2
                new Command\InfoCommand(),
88 2
                new Command\MappingDescribeCommand(),
89
            ]
90
        );
91 2
    }
92
93
    public static function printCliConfigTemplate() : void
94
    {
95
        echo <<<'HELP'
96
You are missing a "cli-config.php" or "config/cli-config.php" file in your
97
project, which is required to get the Doctrine Console working. You can use the
98
following sample as a template:
99
100
<?php
101
use Doctrine\ORM\Tools\Console\ConsoleRunner;
102
103
// replace with file to your own project bootstrap
104
require_once 'bootstrap.php';
105
106
// replace with mechanism to retrieve EntityManager in your app
107
$entityManager = GetEntityManager();
108
109
return ConsoleRunner::createHelperSet($entityManager);
110
111
HELP;
112
    }
113
}
114