| 148 |
lars |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
namespace Doctrine\DBAL\Tools\Console;
|
|
|
4 |
|
|
|
5 |
use Composer\InstalledVersions;
|
|
|
6 |
use Doctrine\DBAL\Tools\Console\Command\ReservedWordsCommand;
|
|
|
7 |
use Doctrine\DBAL\Tools\Console\Command\RunSqlCommand;
|
|
|
8 |
use Exception;
|
|
|
9 |
use Symfony\Component\Console\Application;
|
|
|
10 |
use Symfony\Component\Console\Command\Command;
|
|
|
11 |
|
|
|
12 |
use function assert;
|
|
|
13 |
|
|
|
14 |
/**
|
|
|
15 |
* Handles running the Console Tools inside Symfony Console context.
|
|
|
16 |
*
|
|
|
17 |
* @deprecated Use Symfony Console documentation to bootstrap a command-line application.
|
|
|
18 |
*/
|
|
|
19 |
class ConsoleRunner
|
|
|
20 |
{
|
|
|
21 |
/**
|
|
|
22 |
* Runs console with the given connection provider.
|
|
|
23 |
*
|
|
|
24 |
* @param Command[] $commands
|
|
|
25 |
*
|
|
|
26 |
* @return void
|
|
|
27 |
*
|
|
|
28 |
* @throws Exception
|
|
|
29 |
*/
|
|
|
30 |
public static function run(ConnectionProvider $connectionProvider, $commands = [])
|
|
|
31 |
{
|
|
|
32 |
$version = InstalledVersions::getVersion('doctrine/dbal');
|
|
|
33 |
assert($version !== null);
|
|
|
34 |
|
|
|
35 |
$cli = new Application('Doctrine Command Line Interface', $version);
|
|
|
36 |
|
|
|
37 |
$cli->setCatchExceptions(true);
|
|
|
38 |
self::addCommands($cli, $connectionProvider);
|
|
|
39 |
$cli->addCommands($commands);
|
|
|
40 |
$cli->run();
|
|
|
41 |
}
|
|
|
42 |
|
|
|
43 |
/** @return void */
|
|
|
44 |
public static function addCommands(Application $cli, ConnectionProvider $connectionProvider)
|
|
|
45 |
{
|
|
|
46 |
$cli->addCommands([
|
|
|
47 |
new RunSqlCommand($connectionProvider),
|
|
|
48 |
new ReservedWordsCommand($connectionProvider),
|
|
|
49 |
]);
|
|
|
50 |
}
|
|
|
51 |
|
|
|
52 |
/**
|
|
|
53 |
* Prints the instructions to create a configuration file
|
|
|
54 |
*
|
|
|
55 |
* @deprecated This method will be removed without replacement.
|
|
|
56 |
*
|
|
|
57 |
* @return void
|
|
|
58 |
*/
|
|
|
59 |
public static function printCliConfigTemplate()
|
|
|
60 |
{
|
|
|
61 |
echo <<<'HELP'
|
|
|
62 |
You are missing a "cli-config.php" or "config/cli-config.php" file in your
|
|
|
63 |
project, which is required to get the Doctrine-DBAL Console working. You can use the
|
|
|
64 |
following sample as a template:
|
|
|
65 |
|
|
|
66 |
<?php
|
|
|
67 |
use Doctrine\DBAL\Tools\Console\ConnectionProvider\SingleConnectionProvider;
|
|
|
68 |
|
|
|
69 |
// You can append new commands to $commands array, if needed
|
|
|
70 |
|
|
|
71 |
// replace with the mechanism to retrieve DBAL connection(s) in your app
|
|
|
72 |
// and return a Doctrine\DBAL\Tools\Console\ConnectionProvider instance.
|
|
|
73 |
$connection = getDBALConnection();
|
|
|
74 |
|
|
|
75 |
// in case you have a single connection you can use SingleConnectionProvider
|
|
|
76 |
// otherwise you need to implement the Doctrine\DBAL\Tools\Console\ConnectionProvider interface with your custom logic
|
|
|
77 |
return new SingleConnectionProvider($connection);
|
|
|
78 |
|
|
|
79 |
HELP;
|
|
|
80 |
}
|
|
|
81 |
}
|