Commit 905f0a00 authored by David Maicher's avatar David Maicher

remove deprecations for ConnectionHelper

parent 3710903b
<?php
use Doctrine\DBAL\Tools\Console\ConnectionProvider;
use Doctrine\DBAL\Tools\Console\ConsoleRunner;
use Symfony\Component\Console\Helper\HelperSet;
$files = [__DIR__ . '/../vendor/autoload.php', __DIR__ . '/../../../autoload.php'];
$loader = null;
......@@ -42,17 +40,7 @@ if (! is_readable($configFile)) {
exit(1);
}
$commands = [];
$helperSetOrConnectionProvider = require $configFile;
$commands = [];
$connectionProvider = require $configFile;
if (! $helperSetOrConnectionProvider instanceof HelperSet && ! $helperSetOrConnectionProvider instanceof ConnectionProvider) {
foreach ($GLOBALS as $candidate) {
if ($candidate instanceof HelperSet) {
$helperSetOrConnectionProvider = $candidate;
break;
}
}
}
ConsoleRunner::run($helperSetOrConnectionProvider, $commands);
ConsoleRunner::run($connectionProvider, $commands);
......@@ -16,7 +16,6 @@ use Doctrine\DBAL\Platforms\Keywords\SQLAnywhereKeywords;
use Doctrine\DBAL\Platforms\Keywords\SQLiteKeywords;
use Doctrine\DBAL\Platforms\Keywords\SQLServer2012Keywords;
use Doctrine\DBAL\Tools\Console\ConnectionProvider;
use Exception;
use InvalidArgumentException;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
......@@ -28,9 +27,6 @@ use function assert;
use function count;
use function implode;
use function is_string;
use function trigger_error;
use const E_USER_DEPRECATED;
class ReservedWordsCommand extends Command
{
......@@ -49,18 +45,13 @@ class ReservedWordsCommand extends Command
'sqlserver' => SQLServer2012Keywords::class,
];
/** @var ConnectionProvider|null */
/** @var ConnectionProvider */
private $connectionProvider;
public function __construct(?ConnectionProvider $connectionProvider = null)
public function __construct(ConnectionProvider $connectionProvider)
{
parent::__construct();
$this->connectionProvider = $connectionProvider;
if ($connectionProvider !== null) {
return;
}
@trigger_error('Not passing a connection provider as the first constructor argument is deprecated', E_USER_DEPRECATED);
}
/**
......@@ -174,14 +165,6 @@ EOT
$connectionName = $input->getOption('connection');
assert(is_string($connectionName) || $connectionName === null);
if ($this->connectionProvider === null) {
if ($connectionName !== null) {
throw new Exception('Specifying a connection is only supported when a ConnectionProvider is used.');
}
return $this->getHelper('db')->getConnection();
}
if ($connectionName !== null) {
return $this->connectionProvider->getConnection($connectionName);
}
......
......@@ -5,7 +5,6 @@ namespace Doctrine\DBAL\Tools\Console\Command;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Tools\Console\ConnectionProvider;
use Doctrine\DBAL\Tools\Dumper;
use Exception;
use LogicException;
use RuntimeException;
use Symfony\Component\Console\Command\Command;
......@@ -19,9 +18,6 @@ use function is_bool;
use function is_numeric;
use function is_string;
use function stripos;
use function trigger_error;
use const E_USER_DEPRECATED;
/**
* Task for executing arbitrary SQL that can come from a file or directly from
......@@ -29,18 +25,13 @@ use const E_USER_DEPRECATED;
*/
class RunSqlCommand extends Command
{
/** @var ConnectionProvider|null */
/** @var ConnectionProvider */
private $connectionProvider;
public function __construct(?ConnectionProvider $connectionProvider = null)
public function __construct(ConnectionProvider $connectionProvider)
{
parent::__construct();
$this->connectionProvider = $connectionProvider;
if ($connectionProvider !== null) {
return;
}
@trigger_error('Not passing a connection provider as the first constructor argument is deprecated', E_USER_DEPRECATED);
}
/** @return void */
......@@ -104,14 +95,6 @@ EOT
$connectionName = $input->getOption('connection');
assert(is_string($connectionName) || $connectionName === null);
if ($this->connectionProvider === null) {
if ($connectionName !== null) {
throw new Exception('Specifying a connection is only supported when a ConnectionProvider is used.');
}
return $this->getHelper('db')->getConnection();
}
if ($connectionName !== null) {
return $this->connectionProvider->getConnection($connectionName);
}
......
......@@ -2,67 +2,30 @@
namespace Doctrine\DBAL\Tools\Console;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Tools\Console\Command\ReservedWordsCommand;
use Doctrine\DBAL\Tools\Console\Command\RunSqlCommand;
use Doctrine\DBAL\Tools\Console\Helper\ConnectionHelper;
use PackageVersions\Versions;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Helper\HelperSet;
use TypeError;
use function sprintf;
use function trigger_error;
use const E_USER_DEPRECATED;
/**
* Handles running the Console Tools inside Symfony Console context.
*/
class ConsoleRunner
{
/**
* Create a Symfony Console HelperSet
*
* @deprecated use a ConnectionProvider instead.
*
* @return HelperSet
*/
public static function createHelperSet(Connection $connection)
{
return new HelperSet([
'db' => new ConnectionHelper($connection),
]);
}
/**
* Runs console with the given connection provider or helperset (deprecated).
*
* @param ConnectionProvider|HelperSet $helperSetOrConnectionProvider
* @param Command[] $commands
* @param Command[] $commands
*
* @return void
*/
public static function run($helperSetOrConnectionProvider, $commands = [])
public static function run(ConnectionProvider $connectionProvider, $commands = [])
{
$cli = new Application('Doctrine Command Line Interface', Versions::getVersion('doctrine/dbal'));
$cli->setCatchExceptions(true);
$connectionProvider = null;
if ($helperSetOrConnectionProvider instanceof HelperSet) {
@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);
$connectionProvider = null;
$cli->setHelperSet($helperSetOrConnectionProvider);
} elseif ($helperSetOrConnectionProvider instanceof ConnectionProvider) {
$connectionProvider = $helperSetOrConnectionProvider;
} else {
throw new TypeError(sprintf('First argument must be an instance of "%s" or "%s"', HelperSet::class, ConnectionProvider::class));
}
self::addCommands($cli, $connectionProvider);
$cli->addCommands($commands);
$cli->run();
}
......@@ -70,11 +33,10 @@ class ConsoleRunner
/**
* @return void
*/
public static function addCommands(Application $cli, ?ConnectionProvider $connectionProvider = null)
public static function addCommands(Application $cli, ConnectionProvider $connectionProvider)
{
$cli->addCommands([
new RunSqlCommand(),
new ReservedWordsCommand(),
new RunSqlCommand($connectionProvider),
new ReservedWordsCommand($connectionProvider),
]);
}
......
<?php
namespace Doctrine\DBAL\Tools\Console\Helper;
use Doctrine\DBAL\Connection;
use Symfony\Component\Console\Helper\Helper;
/**
* Doctrine CLI Connection Helper.
*
* @deprecated use a ConnectionProvider instead.
*/
class ConnectionHelper extends Helper
{
/**
* The Doctrine database Connection.
*
* @var Connection
*/
protected $_connection;
/**
* @param Connection $connection The Doctrine database Connection.
*/
public function __construct(Connection $connection)
{
$this->_connection = $connection;
}
/**
* Retrieves the Doctrine database Connection.
*
* @return Connection
*/
public function getConnection()
{
return $this->_connection;
}
/**
* {@inheritdoc}
*/
public function getName()
{
return 'connection';
}
}
......@@ -4,7 +4,7 @@ namespace Doctrine\DBAL\Tests\Tools\Console;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Tools\Console\Command\RunSqlCommand;
use Doctrine\DBAL\Tools\Console\ConsoleRunner;
use Doctrine\DBAL\Tools\Console\ConnectionProvider\SingleConnectionProvider;
use LogicException;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
......@@ -24,20 +24,17 @@ class RunSqlCommandTest extends TestCase
protected function setUp(): void
{
$application = new Application();
$application->add(new RunSqlCommand());
$this->command = $application->find('dbal:run-sql');
$this->commandTester = new CommandTester($this->command);
$this->connectionMock = $this->createMock(Connection::class);
$this->connectionMock->method('fetchAllAssociative')
->willReturn([[1]]);
$this->connectionMock->method('executeUpdate')
->willReturn(42);
$helperSet = ConsoleRunner::createHelperSet($this->connectionMock);
$this->command->setHelperSet($helperSet);
$application = new Application();
$application->add(new RunSqlCommand(new SingleConnectionProvider($this->connectionMock)));
$this->command = $application->find('dbal:run-sql');
$this->commandTester = new CommandTester($this->command);
}
public function testMissingSqlArgument(): void
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment