|
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(), |
|
|
|
|
|
|
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
|
|
|
|