ConsoleRunner.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. namespace Doctrine\DBAL\Tools\Console;
  3. use Doctrine\DBAL\Connection;
  4. use Doctrine\DBAL\Tools\Console\Command\ImportCommand;
  5. use Doctrine\DBAL\Tools\Console\Command\ReservedWordsCommand;
  6. use Doctrine\DBAL\Tools\Console\Command\RunSqlCommand;
  7. use Doctrine\DBAL\Tools\Console\Helper\ConnectionHelper;
  8. use Doctrine\DBAL\Version;
  9. use Doctrine\Deprecations\Deprecation;
  10. use Symfony\Component\Console\Application;
  11. use Symfony\Component\Console\Command\Command;
  12. use Symfony\Component\Console\Helper\HelperSet;
  13. use TypeError;
  14. use function sprintf;
  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. * @deprecated use a ConnectionProvider instead.
  24. *
  25. * @return HelperSet
  26. */
  27. public static function createHelperSet(Connection $connection)
  28. {
  29. return new HelperSet([
  30. 'db' => new ConnectionHelper($connection),
  31. ]);
  32. }
  33. /**
  34. * Runs console with the given connection provider or helperset (deprecated).
  35. *
  36. * @param ConnectionProvider|HelperSet $helperSetOrConnectionProvider
  37. * @param Command[] $commands
  38. *
  39. * @return void
  40. */
  41. public static function run($helperSetOrConnectionProvider, $commands = [])
  42. {
  43. $cli = new Application('Doctrine Command Line Interface', Version::VERSION);
  44. $cli->setCatchExceptions(true);
  45. $connectionProvider = null;
  46. if ($helperSetOrConnectionProvider instanceof HelperSet) {
  47. Deprecation::trigger(
  48. 'doctrine/dbal',
  49. 'https://github.com/doctrine/dbal/pull/3956',
  50. 'Passing an instance of "%s" as the first argument is deprecated. Pass an instance of "%s" instead.',
  51. HelperSet::class,
  52. ConnectionProvider::class
  53. );
  54. $connectionProvider = null;
  55. $cli->setHelperSet($helperSetOrConnectionProvider);
  56. } elseif ($helperSetOrConnectionProvider instanceof ConnectionProvider) {
  57. $connectionProvider = $helperSetOrConnectionProvider;
  58. } else {
  59. throw new TypeError(sprintf(
  60. 'First argument must be an instance of "%s" or "%s"',
  61. HelperSet::class,
  62. ConnectionProvider::class
  63. ));
  64. }
  65. self::addCommands($cli, $connectionProvider);
  66. $cli->addCommands($commands);
  67. $cli->run();
  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. * Prints the instructions to create a configuration file
  82. *
  83. * @return void
  84. */
  85. public static function printCliConfigTemplate()
  86. {
  87. echo <<<'HELP'
  88. You are missing a "cli-config.php" or "config/cli-config.php" file in your
  89. project, which is required to get the Doctrine-DBAL Console working. You can use the
  90. following sample as a template:
  91. <?php
  92. use Doctrine\DBAL\Tools\Console\ConnectionProvider\SingleConnectionProvider;
  93. // You can append new commands to $commands array, if needed
  94. // replace with the mechanism to retrieve DBAL connection(s) in your app
  95. // and return a Doctrine\DBAL\Tools\Console\ConnectionProvider instance.
  96. $connection = getDBALConnection();
  97. // in case you have a single connection you can use SingleConnectionProvider
  98. // otherwise you need to implement the Doctrine\DBAL\Tools\Console\ConnectionProvider interface with your custom logic
  99. return new SingleConnectionProvider($connection);
  100. HELP;
  101. }
  102. }