Skip to content

Commit

Permalink
Remove console helper
Browse files Browse the repository at this point in the history
  • Loading branch information
derrabus committed Apr 10, 2022
1 parent 16748bd commit ed502c2
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 148 deletions.
19 changes: 19 additions & 0 deletions UPGRADE.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,24 @@
# Upgrade to 3.0

## BC Break: Removed `EntityManagerHelper` and related logic

All console commands require a `$entityManagerProvider` to be passed via the
constructor. Commands won't try to get the entity manager from a previously
registered `em` console helper.

The following classes have been removed:

* `Doctrine\ORM\Tools\Console\EntityManagerProvider\HelperSetManagerProvider`
* `Doctrine\ORM\Tools\Console\Helper\EntityManagerHelper`

The following breaking changes have been applied to `Doctrine\ORM\Tools\Console\ConsoleRunner`:

* The method `createHelperSet()` has been removed.
* The methods `run()` and `createApplication()` don't accept an instance of
`HelperSet` as first argument anymore.
* The method `addCommands()` requires an instance of `EntityManagerProvider`
as second argument now.

## BC Break: `Exception\ORMException` is no longer a class, but an interface

All methods in `Doctrine\ORM\ORMException` have been extracted to dedicated exceptions.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,20 @@

namespace Doctrine\ORM\Tools\Console\Command;

use Doctrine\Deprecations\Deprecation;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Tools\Console\EntityManagerProvider;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;

abstract class AbstractEntityManagerCommand extends Command
{
public function __construct(private ?EntityManagerProvider $entityManagerProvider = null)
public function __construct(private EntityManagerProvider $entityManagerProvider)
{
parent::__construct();
}

final protected function getEntityManager(InputInterface $input): EntityManagerInterface
{
// This is a backwards compatibility required check for commands extending Doctrine ORM commands
if (! $input->hasOption('em') || $this->entityManagerProvider === null) {
Deprecation::trigger(
'doctrine/orm',
'https://github.com/doctrine/orm/issues/8327',
'Not passing EntityManagerProvider as a dependency to command class "%s" is deprecated',
$this->getName()
);

return $this->getHelper('em')->getEntityManager();
}

return $input->getOption('em') === null
? $this->entityManagerProvider->getDefaultManager()
: $this->entityManagerProvider->getManager($input->getOption('em'));
Expand Down
32 changes: 5 additions & 27 deletions lib/Doctrine/ORM/Tools/Console/ConsoleRunner.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,10 @@

use Composer\InstalledVersions;
use Doctrine\DBAL\Tools\Console as DBALConsole;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Tools\Console\EntityManagerProvider\ConnectionFromManagerProvider;
use Doctrine\ORM\Tools\Console\EntityManagerProvider\HelperSetManagerProvider;
use Doctrine\ORM\Tools\Console\Helper\EntityManagerHelper;
use OutOfBoundsException;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Command\Command as SymfonyCommand;
use Symfony\Component\Console\Helper\HelperSet;

use function assert;

Expand All @@ -22,22 +18,14 @@
*/
final class ConsoleRunner
{
/**
* Create a Symfony Console HelperSet
*/
public static function createHelperSet(EntityManagerInterface $entityManager): HelperSet
{
return new HelperSet(['em' => new EntityManagerHelper($entityManager)]);
}

/**
* Runs console with the given helper set.
*
* @param SymfonyCommand[] $commands
*/
public static function run(HelperSet|EntityManagerProvider $helperSetOrProvider, array $commands = []): void
public static function run(EntityManagerProvider $entityManagerProvider, array $commands = []): void
{
$cli = self::createApplication($helperSetOrProvider, $commands);
$cli = self::createApplication($entityManagerProvider, $commands);
$cli->run();
}

Expand All @@ -50,7 +38,7 @@ public static function run(HelperSet|EntityManagerProvider $helperSetOrProvider,
* @throws OutOfBoundsException
*/
public static function createApplication(
HelperSet|EntityManagerProvider $helperSetOrProvider,
EntityManagerProvider $entityManagerProvider,
array $commands = []
): Application {
$version = InstalledVersions::getVersion('doctrine/orm');
Expand All @@ -59,24 +47,14 @@ public static function createApplication(
$cli = new Application('Doctrine Command Line Interface', $version);
$cli->setCatchExceptions(true);

if ($helperSetOrProvider instanceof HelperSet) {
$cli->setHelperSet($helperSetOrProvider);

$helperSetOrProvider = new HelperSetManagerProvider($helperSetOrProvider);
}

self::addCommands($cli, $helperSetOrProvider);
self::addCommands($cli, $entityManagerProvider);
$cli->addCommands($commands);

return $cli;
}

public static function addCommands(Application $cli, ?EntityManagerProvider $entityManagerProvider = null): void
public static function addCommands(Application $cli, EntityManagerProvider $entityManagerProvider): void
{
if ($entityManagerProvider === null) {
$entityManagerProvider = new HelperSetManagerProvider($cli->getHelperSet());
}

$connectionProvider = new ConnectionFromManagerProvider($entityManagerProvider);

$cli->addCommands(
Expand Down

This file was deleted.

39 changes: 0 additions & 39 deletions lib/Doctrine/ORM/Tools/Console/Helper/EntityManagerHelper.php

This file was deleted.

19 changes: 5 additions & 14 deletions tests/Doctrine/Tests/ORM/Tools/Console/Command/InfoCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,18 @@
use Doctrine\ORM\Mapping\MappingException;
use Doctrine\ORM\Tools\Console\Command\InfoCommand;
use Doctrine\ORM\Tools\Console\EntityManagerProvider\SingleManagerProvider;
use Doctrine\ORM\Tools\Console\Helper\EntityManagerHelper;
use Doctrine\Persistence\Mapping\Driver\MappingDriver;
use Doctrine\Tests\Models\Cache\AttractionInfo;
use Doctrine\Tests\Models\Cache\City;
use Doctrine\Tests\OrmFunctionalTestCase;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Helper\HelperSet;
use Symfony\Component\Console\Tester\CommandTester;

class InfoCommandTest extends OrmFunctionalTestCase
{
/** @var Application */
private $application;

/** @var InfoCommand */
private $command;

/** @var CommandTester */
private $tester;
private Application $application;
private InfoCommand $command;
private CommandTester $tester;

protected function setUp(): void
{
Expand Down Expand Up @@ -65,8 +58,7 @@ public function testEmptyEntityClassNames(): void
->willReturn($configuration);

$application = new Application();
$application->setHelperSet(new HelperSet(['em' => new EntityManagerHelper($em)]));
$application->add(new InfoCommand());
$application->add(new InfoCommand(new SingleManagerProvider($em)));

$command = $application->find('orm:info');
$tester = new CommandTester($command);
Expand Down Expand Up @@ -104,8 +96,7 @@ public function testInvalidEntityClassMetadata(): void
->willThrowException(new MappingException('exception message'));

$application = new Application();
$application->setHelperSet(new HelperSet(['em' => new EntityManagerHelper($em)]));
$application->add(new InfoCommand());
$application->add(new InfoCommand(new SingleManagerProvider($em)));

$command = $application->find('orm:info');
$tester = new CommandTester($command);
Expand Down
13 changes: 2 additions & 11 deletions tests/Doctrine/Tests/ORM/Tools/Console/ConsoleRunnerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,22 @@
namespace Doctrine\Tests\ORM\Tools\Console;

use Composer\InstalledVersions;
use Doctrine\Deprecations\PHPUnit\VerifyDeprecations;
use Doctrine\ORM\Tools\Console\ConsoleRunner;
use Doctrine\ORM\Tools\Console\EntityManagerProvider;
use Doctrine\Tests\DoctrineTestCase;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Helper\HelperSet;

/**
* @group DDC-3186
* @covers \Doctrine\ORM\Tools\Console\ConsoleRunner
*/
final class ConsoleRunnerTest extends DoctrineTestCase
{
use VerifyDeprecations;

public function testCreateApplicationShouldReturnAnApplicationWithTheCorrectCommands(): void
{
$this->expectDeprecationWithIdentifier('https://github.com/doctrine/orm/issues/8327');

$helperSet = new HelperSet();
$app = ConsoleRunner::createApplication($helperSet);
$app = ConsoleRunner::createApplication($this->createStub(EntityManagerProvider::class));

self::assertSame($helperSet, $app->getHelperSet());
self::assertSame(InstalledVersions::getVersion('doctrine/orm'), $app->getVersion());

self::assertTrue($app->has('dbal:reserved-words'));
self::assertTrue($app->has('dbal:run-sql'));
self::assertTrue($app->has('orm:clear-cache:region:collection'));
Expand All @@ -52,7 +43,7 @@ public function testCreateApplicationShouldReturnAnApplicationWithTheCorrectComm
public function testCreateApplicationShouldAppendGivenCommands(): void
{
$command = 'my:lovely-command';
$app = ConsoleRunner::createApplication(new HelperSet(), [new Command($command)]);
$app = ConsoleRunner::createApplication($this->createStub(EntityManagerProvider::class), [new Command($command)]);

self::assertTrue($app->has($command));
}
Expand Down

0 comments on commit ed502c2

Please sign in to comment.