Completed
Push — master ( 20b64d...66daec )
by Marco
18s queued 12s
created

lib/Doctrine/DBAL/Tools/Console/ConsoleRunner.php (1 issue)

1
<?php
2
/*
3
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14
 *
15
 * This software consists of voluntary contributions made by many individuals
16
 * and is licensed under the MIT license. For more information, see
17
 * <http://www.doctrine-project.org>.
18
 */
19
20
namespace Doctrine\DBAL\Tools\Console;
21
22
use Doctrine\DBAL\Connection;
23
use Doctrine\DBAL\Tools\Console\Command\ImportCommand;
24
use Doctrine\DBAL\Tools\Console\Command\ReservedWordsCommand;
25
use Doctrine\DBAL\Tools\Console\Command\RunSqlCommand;
26
use Doctrine\DBAL\Tools\Console\Helper\ConnectionHelper;
27
use Doctrine\DBAL\Version;
28
use Symfony\Component\Console\Application;
29
use Symfony\Component\Console\Helper\HelperSet;
30
31
/**
32
 * Handles running the Console Tools inside Symfony Console context.
33
 */
34
class ConsoleRunner
35
{
36
    /**
37
     * Create a Symfony Console HelperSet
38
     *
39
     * @param Connection $connection
40
     *
41
     * @return HelperSet
42
     */
43 95
    public static function createHelperSet(Connection $connection)
44
    {
45 95
        return new HelperSet([
46 95
            'db' => new ConnectionHelper($connection)
47
        ]);
48
    }
49
50
    /**
51
     * Runs console with the given helperset.
52
     *
53
     * @param \Symfony\Component\Console\Helper\HelperSet  $helperSet
54
     * @param \Symfony\Component\Console\Command\Command[] $commands
55
     *
56
     * @return void
57
     */
58
    public static function run(HelperSet $helperSet, $commands = [])
59
    {
60
        $cli = new Application('Doctrine Command Line Interface', Version::VERSION);
61
62
        $cli->setCatchExceptions(true);
63
        $cli->setHelperSet($helperSet);
64
65
        self::addCommands($cli);
66
67
        $cli->addCommands($commands);
68
        $cli->run();
69
    }
70
71
    /**
72
     * @param Application $cli
73
     *
74
     * @return void
75
     */
76
    public static function addCommands(Application $cli)
77
    {
78
        $cli->addCommands([
79
            new RunSqlCommand(),
80
            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

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