Completed
Pull Request — 2.11.x (#3956)
by David
14:33
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
use TypeError;
15
use const E_USER_DEPRECATED;
16
use function sprintf;
17
use function trigger_error;
18
19
/**
20
 * Handles running the Console Tools inside Symfony Console context.
21
 */
22
class ConsoleRunner
23
{
24
    /**
25
     * Create a Symfony Console HelperSet
26
     *
27
     * @deprecated use a ConnectionProvider instead.
28
     *
29
     * @return HelperSet
30
     */
31 130
    public static function createHelperSet(Connection $connection)
32
    {
33 130
        return new HelperSet([
34 130
            'db' => new ConnectionHelper($connection),
0 ignored issues
show
Deprecated Code introduced by
The class Doctrine\DBAL\Tools\Cons...Helper\ConnectionHelper has been deprecated: use a ConnectionProvider instead. ( Ignorable by Annotation )

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

34
            'db' => /** @scrutinizer ignore-deprecated */ new ConnectionHelper($connection),
Loading history...
35
        ]);
36
    }
37
38
    /**
39
     * Runs console with the given connection provider or helperset (deprecated).
40
     *
41
     * @param ConnectionProvider|HelperSet $helperSetOrConnectionProvider
42
     * @param Command[]                    $commands
43
     *
44
     * @return void
45
     */
46
    public static function run($helperSetOrConnectionProvider, $commands = [])
47
    {
48
        $cli = new Application('Doctrine Command Line Interface', Version::VERSION);
49
50
        $cli->setCatchExceptions(true);
51
52
        /** @var ConnectionProvider|null $connectionProvider */
53
        $connectionProvider = $helperSetOrConnectionProvider;
54
        if ($helperSetOrConnectionProvider instanceof HelperSet) {
55
            $connectionProvider = null;
56
            $cli->setHelperSet($helperSetOrConnectionProvider);
57
            @trigger_error(sprintf('Passing an instance of "%s" as the first argument is deprecated. Pass an instance of "%s" instead.', HelperSet::class, ConnectionProvider::class), E_USER_DEPRECATED);
58
        } elseif (! $helperSetOrConnectionProvider instanceof ConnectionProvider) {
0 ignored issues
show
introduced by
$helperSetOrConnectionProvider is always a sub-type of Doctrine\DBAL\Tools\Console\ConnectionProvider.
Loading history...
59
            throw new TypeError(sprintf('First argument must be an instance of "%s" or "%s"', HelperSet::class, ConnectionProvider::class));
60
        }
61
62
        self::addCommands($cli, $connectionProvider);
63
64
        $cli->addCommands($commands);
65
        $cli->run();
66
    }
67
68
    /**
69
     * @return void
70
     */
71
    public static function addCommands(Application $cli, ?ConnectionProvider $connectionProvider = null)
72
    {
73
        $cli->addCommands([
74
            new RunSqlCommand($connectionProvider),
75
            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

75
            /** @scrutinizer ignore-deprecated */ new ImportCommand(),
Loading history...
76
            new ReservedWordsCommand($connectionProvider),
77
        ]);
78
    }
79
80
    /**
81
     * Prints the instructions to create a configuration file
82
     *
83
     * @return void
84
     */
85
    public static function printCliConfigTemplate()
86
    {
87
        echo <<<'HELP'
88
You are missing a "cli-config.php" or "config/cli-config.php" file in your
89
project, which is required to get the Doctrine-DBAL Console working. You can use the
90
following sample as a template:
91
92
<?php
93
use Doctrine\DBAL\Tools\Console\ConnectionProvider\SingleConnectionProvider;
94
95
// You can append new commands to $commands array, if needed
96
97
// replace with the mechanism to retrieve DBAL connection(s) in your app
98
// and return a Doctrine\DBAL\Tools\Console\ConnectionProvider instance.
99
$connection = getDBALConnection();
100
101
// in case you have a single connection you can use SingleConnectionProvider
102
// otherwise you need to implement the Doctrine\DBAL\Tools\Console\ConnectionProvider interface with your custom logic
103
return new SingleConnectionProvider($connection);
104
105
HELP;
106
    }
107
}
108