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 function sprintf; |
16
|
|
|
use function trigger_error; |
17
|
|
|
use const E_USER_DEPRECATED; |
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
|
120 |
|
public static function createHelperSet(Connection $connection) |
32
|
|
|
{ |
33
|
120 |
|
return new HelperSet([ |
34
|
120 |
|
'db' => new ConnectionHelper($connection), |
|
|
|
|
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
|
|
|
$connectionProvider = null; |
53
|
|
|
if ($helperSetOrConnectionProvider instanceof HelperSet) { |
54
|
|
|
@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); |
55
|
|
|
$connectionProvider = null; |
56
|
|
|
$cli->setHelperSet($helperSetOrConnectionProvider); |
57
|
|
|
} elseif ($helperSetOrConnectionProvider instanceof ConnectionProvider) { |
|
|
|
|
58
|
|
|
$connectionProvider = $helperSetOrConnectionProvider; |
59
|
|
|
} else { |
60
|
|
|
throw new TypeError(sprintf('First argument must be an instance of "%s" or "%s"', HelperSet::class, ConnectionProvider::class)); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
self::addCommands($cli, $connectionProvider); |
64
|
|
|
|
65
|
|
|
$cli->addCommands($commands); |
66
|
|
|
$cli->run(); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @return void |
71
|
|
|
*/ |
72
|
|
|
public static function addCommands(Application $cli, ?ConnectionProvider $connectionProvider = null) |
73
|
|
|
{ |
74
|
|
|
$cli->addCommands([ |
75
|
|
|
new RunSqlCommand($connectionProvider), |
76
|
|
|
new ImportCommand(), |
|
|
|
|
77
|
|
|
new ReservedWordsCommand($connectionProvider), |
78
|
|
|
]); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Prints the instructions to create a configuration file |
83
|
|
|
* |
84
|
|
|
* @return void |
85
|
|
|
*/ |
86
|
|
|
public static function printCliConfigTemplate() |
87
|
|
|
{ |
88
|
|
|
echo <<<'HELP' |
89
|
|
|
You are missing a "cli-config.php" or "config/cli-config.php" file in your |
90
|
|
|
project, which is required to get the Doctrine-DBAL Console working. You can use the |
91
|
|
|
following sample as a template: |
92
|
|
|
|
93
|
|
|
<?php |
94
|
|
|
use Doctrine\DBAL\Tools\Console\ConnectionProvider\SingleConnectionProvider; |
95
|
|
|
|
96
|
|
|
// You can append new commands to $commands array, if needed |
97
|
|
|
|
98
|
|
|
// replace with the mechanism to retrieve DBAL connection(s) in your app |
99
|
|
|
// and return a Doctrine\DBAL\Tools\Console\ConnectionProvider instance. |
100
|
|
|
$connection = getDBALConnection(); |
101
|
|
|
|
102
|
|
|
// in case you have a single connection you can use SingleConnectionProvider |
103
|
|
|
// otherwise you need to implement the Doctrine\DBAL\Tools\Console\ConnectionProvider interface with your custom logic |
104
|
|
|
return new SingleConnectionProvider($connection); |
105
|
|
|
|
106
|
|
|
HELP; |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|