Completed
Push — 2.10.x ( 6c789b...72d908 )
by Grégoire
22s queued 15s
created

ConsoleRunner::createHelperSet()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Doctrine\DBAL\Tools\Console;
4
5
use Doctrine\DBAL\Connection;
6
use Doctrine\DBAL\Tools\Console\Command\ImportCommand;
7
use Doctrine\DBAL\Tools\Console\Command\ReservedWordsCommand;
8
use Doctrine\DBAL\Tools\Console\Command\RunSqlCommand;
9
use Doctrine\DBAL\Tools\Console\Helper\ConnectionHelper;
10
use Doctrine\DBAL\Version;
11
use Symfony\Component\Console\Application;
12
use Symfony\Component\Console\Command\Command;
13
use Symfony\Component\Console\Helper\HelperSet;
14
15
/**
16
 * Handles running the Console Tools inside Symfony Console context.
17
 */
18
class ConsoleRunner
19
{
20
    /**
21
     * Create a Symfony Console HelperSet
22
     *
23
     * @return HelperSet
24
     */
25 120
    public static function createHelperSet(Connection $connection)
26
    {
27 120
        return new HelperSet([
28 120
            'db' => new ConnectionHelper($connection),
29
        ]);
30
    }
31
32
    /**
33
     * Runs console with the given helperset.
34
     *
35
     * @param Command[] $commands
36
     *
37
     * @return void
38
     */
39
    public static function run(HelperSet $helperSet, $commands = [])
40
    {
41
        $cli = new Application('Doctrine Command Line Interface', Version::VERSION);
42
43
        $cli->setCatchExceptions(true);
44
        $cli->setHelperSet($helperSet);
45
46
        self::addCommands($cli);
47
48
        $cli->addCommands($commands);
49
        $cli->run();
50
    }
51
52
    /**
53
     * @return void
54
     */
55
    public static function addCommands(Application $cli)
56
    {
57
        $cli->addCommands([
58
            new RunSqlCommand(),
59
            new ImportCommand(),
0 ignored issues
show
Deprecated Code introduced by
The class Doctrine\DBAL\Tools\Console\Command\ImportCommand has been deprecated: Use a database client application instead ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

59
            /** @scrutinizer ignore-deprecated */ new ImportCommand(),
Loading history...
60
            new ReservedWordsCommand(),
61
        ]);
62
    }
63
64
    /**
65
     * Prints the instructions to create a configuration file
66
     *
67
     * @return void
68
     */
69
    public static function printCliConfigTemplate()
70
    {
71
        echo <<<'HELP'
72
You are missing a "cli-config.php" or "config/cli-config.php" file in your
73
project, which is required to get the Doctrine-DBAL Console working. You can use the
74
following sample as a template:
75
76
<?php
77
use Doctrine\DBAL\Tools\Console\ConsoleRunner;
78
79
// replace with the mechanism to retrieve DBAL connection in your app
80
$connection = getDBALConnection();
81
82
// You can append new commands to $commands array, if needed
83
84
return ConsoleRunner::createHelperSet($connection);
85
86
HELP;
87
    }
88
}
89