1111use Symfony \Component \Console \Application ;
1212use Symfony \Component \Console \Command \Command ;
1313use Symfony \Component \Console \Helper \HelperSet ;
14+ use TypeError ;
15+ use function sprintf ;
16+ use function trigger_error ;
17+ use const E_USER_DEPRECATED ;
1418
1519/**
1620 * Handles running the Console Tools inside Symfony Console context.
@@ -20,6 +24,8 @@ class ConsoleRunner
2024 /**
2125 * Create a Symfony Console HelperSet
2226 *
27+ * @deprecated use a ConnectionProvider instead.
28+ *
2329 * @return HelperSet
2430 */
2531 public static function createHelperSet (Connection $ connection )
@@ -30,20 +36,31 @@ public static function createHelperSet(Connection $connection)
3036 }
3137
3238 /**
33- * Runs console with the given helperset.
39+ * Runs console with the given connection provider or helperset (deprecated) .
3440 *
35- * @param Command[] $commands
41+ * @param ConnectionProvider|HelperSet $helperSetOrConnectionProvider
42+ * @param Command[] $commands
3643 *
3744 * @return void
3845 */
39- public static function run (HelperSet $ helperSet , $ commands = [])
46+ public static function run ($ helperSetOrConnectionProvider , $ commands = [])
4047 {
4148 $ cli = new Application ('Doctrine Command Line Interface ' , Version::VERSION );
4249
4350 $ cli ->setCatchExceptions (true );
44- $ cli ->setHelperSet ($ helperSet );
4551
46- self ::addCommands ($ cli );
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 );
4764
4865 $ cli ->addCommands ($ commands );
4966 $ cli ->run ();
@@ -52,12 +69,12 @@ public static function run(HelperSet $helperSet, $commands = [])
5269 /**
5370 * @return void
5471 */
55- public static function addCommands (Application $ cli )
72+ public static function addCommands (Application $ cli, ? ConnectionProvider $ connectionProvider = null )
5673 {
5774 $ cli ->addCommands ([
58- new RunSqlCommand (),
75+ new RunSqlCommand ($ connectionProvider ),
5976 new ImportCommand (),
60- new ReservedWordsCommand (),
77+ new ReservedWordsCommand ($ connectionProvider ),
6178 ]);
6279 }
6380
@@ -74,14 +91,17 @@ public static function printCliConfigTemplate()
7491following sample as a template:
7592
7693<?php
77- use Doctrine\DBAL\Tools\Console\ConsoleRunner;
78-
79- // replace with the mechanism to retrieve DBAL connection in your app
80- $connection = getDBALConnection();
94+ use Doctrine\DBAL\Tools\Console\ConnectionProvider\SingleConnectionProvider;
8195
8296// You can append new commands to $commands array, if needed
8397
84- return ConsoleRunner::createHelperSet($connection);
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);
85105
86106HELP;
87107 }
0 commit comments