-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathConsoleRunner.php
79 lines (64 loc) · 2.07 KB
/
ConsoleRunner.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
<?php
declare(strict_types=1);
namespace Doctrine\DBAL\Tools\Console;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Tools\Console\Command\ReservedWordsCommand;
use Doctrine\DBAL\Tools\Console\Command\RunSqlCommand;
use Doctrine\DBAL\Tools\Console\Helper\ConnectionHelper;
use PackageVersions\Versions;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Helper\HelperSet;
/**
* Handles running the Console Tools inside Symfony Console context.
*/
class ConsoleRunner
{
/**
* Create a Symfony Console HelperSet
*/
public static function createHelperSet(Connection $connection) : HelperSet
{
return new HelperSet([
'db' => new ConnectionHelper($connection),
]);
}
/**
* Runs console with the given helperset.
*
* @param array<int, Command> $commands
*/
public static function run(HelperSet $helperSet, array $commands = []) : void
{
$cli = new Application('Doctrine Command Line Interface', Versions::getVersion('doctrine/dbal'));
$cli->setCatchExceptions(true);
$cli->setHelperSet($helperSet);
self::addCommands($cli);
$cli->addCommands($commands);
$cli->run();
}
public static function addCommands(Application $cli) : void
{
$cli->addCommands([
new RunSqlCommand(),
new ReservedWordsCommand(),
]);
}
/**
* Prints the instructions to create a configuration file
*/
public static function printCliConfigTemplate() : void
{
echo <<<'HELP'
You are missing a "cli-config.php" or "config/cli-config.php" file in your
project, which is required to get the Doctrine-DBAL Console working. You can use the
following sample as a template:
<?php
use Doctrine\DBAL\Tools\Console\ConsoleRunner;
// replace with the mechanism to retrieve DBAL connection in your app
$connection = getDBALConnection();
// You can append new commands to $commands array, if needed
return ConsoleRunner::createHelperSet($connection);
HELP;
}
}