Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove deprecations for ConnectionHelper #4059

Merged
merged 1 commit into from
Jun 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 3 additions & 15 deletions bin/doctrine-dbal.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
<?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;
Expand Down Expand Up @@ -42,17 +40,7 @@
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);
21 changes: 2 additions & 19 deletions src/Tools/Console/Command/ReservedWordsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
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;
Expand All @@ -28,9 +27,6 @@
use function count;
use function implode;
use function is_string;
use function trigger_error;

use const E_USER_DEPRECATED;

class ReservedWordsCommand extends Command
{
Expand All @@ -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);
}

/**
Expand Down Expand Up @@ -174,14 +165,6 @@ private function getConnection(InputInterface $input): Connection
$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);
}
Expand Down
21 changes: 2 additions & 19 deletions src/Tools/Console/Command/RunSqlCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
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;
Expand All @@ -19,28 +18,20 @@
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
* the command line.
*/
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 */
Expand Down Expand Up @@ -104,14 +95,6 @@ private function getConnection(InputInterface $input): Connection
$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);
}
Expand Down
46 changes: 4 additions & 42 deletions src/Tools/Console/ConsoleRunner.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,79 +2,41 @@

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();
}

/**
* @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),
]);
}
Expand Down
47 changes: 0 additions & 47 deletions src/Tools/Console/Helper/ConnectionHelper.php

This file was deleted.

15 changes: 6 additions & 9 deletions tests/Tools/Console/RunSqlCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

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;
Expand All @@ -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
Expand Down