From ed7eec336e04552e00246ec53acb67f74939ec0e Mon Sep 17 00:00:00 2001 From: "Alexander M. Turek" Date: Sun, 10 Apr 2022 13:52:00 +0200 Subject: [PATCH] Remove console helper --- UPGRADE.md | 19 ++++++++ .../Command/AbstractEntityManagerCommand.php | 15 +------ .../ORM/Tools/Console/ConsoleRunner.php | 32 +++----------- .../HelperSetManagerProvider.php | 43 ------------------- .../Console/Helper/EntityManagerHelper.php | 39 ----------------- .../Tools/Console/Command/InfoCommandTest.php | 19 +++----- .../ORM/Tools/Console/ConsoleRunnerTest.php | 21 +-------- 7 files changed, 32 insertions(+), 156 deletions(-) delete mode 100644 lib/Doctrine/ORM/Tools/Console/EntityManagerProvider/HelperSetManagerProvider.php delete mode 100644 lib/Doctrine/ORM/Tools/Console/Helper/EntityManagerHelper.php diff --git a/UPGRADE.md b/UPGRADE.md index 2de7b0ab41b..57bb052b480 100644 --- a/UPGRADE.md +++ b/UPGRADE.md @@ -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. diff --git a/lib/Doctrine/ORM/Tools/Console/Command/AbstractEntityManagerCommand.php b/lib/Doctrine/ORM/Tools/Console/Command/AbstractEntityManagerCommand.php index 77256a4a528..1da54248d93 100644 --- a/lib/Doctrine/ORM/Tools/Console/Command/AbstractEntityManagerCommand.php +++ b/lib/Doctrine/ORM/Tools/Console/Command/AbstractEntityManagerCommand.php @@ -4,7 +4,6 @@ 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; @@ -12,25 +11,13 @@ 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')); diff --git a/lib/Doctrine/ORM/Tools/Console/ConsoleRunner.php b/lib/Doctrine/ORM/Tools/Console/ConsoleRunner.php index 554ba3a1577..c0ae650d3cc 100644 --- a/lib/Doctrine/ORM/Tools/Console/ConsoleRunner.php +++ b/lib/Doctrine/ORM/Tools/Console/ConsoleRunner.php @@ -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; @@ -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(); } @@ -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'); @@ -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( diff --git a/lib/Doctrine/ORM/Tools/Console/EntityManagerProvider/HelperSetManagerProvider.php b/lib/Doctrine/ORM/Tools/Console/EntityManagerProvider/HelperSetManagerProvider.php deleted file mode 100644 index 2e03e4997a8..00000000000 --- a/lib/Doctrine/ORM/Tools/Console/EntityManagerProvider/HelperSetManagerProvider.php +++ /dev/null @@ -1,43 +0,0 @@ -getDefaultManager(); - } - - public function getDefaultManager(): EntityManagerInterface - { - $helper = $this->helperSet->get('entityManager'); - - assert($helper instanceof EntityManagerHelper); - - return $helper->getEntityManager(); - } -} diff --git a/lib/Doctrine/ORM/Tools/Console/Helper/EntityManagerHelper.php b/lib/Doctrine/ORM/Tools/Console/Helper/EntityManagerHelper.php deleted file mode 100644 index e20a7eca26c..00000000000 --- a/lib/Doctrine/ORM/Tools/Console/Helper/EntityManagerHelper.php +++ /dev/null @@ -1,39 +0,0 @@ -_em = $em; - } - - /** - * Retrieves Doctrine ORM EntityManager. - */ - public function getEntityManager(): EntityManagerInterface - { - return $this->_em; - } - - public function getName(): string - { - return 'entityManager'; - } -} diff --git a/tests/Doctrine/Tests/ORM/Tools/Console/Command/InfoCommandTest.php b/tests/Doctrine/Tests/ORM/Tools/Console/Command/InfoCommandTest.php index 021e77ec282..c1573a36d25 100644 --- a/tests/Doctrine/Tests/ORM/Tools/Console/Command/InfoCommandTest.php +++ b/tests/Doctrine/Tests/ORM/Tools/Console/Command/InfoCommandTest.php @@ -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 { @@ -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); @@ -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); diff --git a/tests/Doctrine/Tests/ORM/Tools/Console/ConsoleRunnerTest.php b/tests/Doctrine/Tests/ORM/Tools/Console/ConsoleRunnerTest.php index 3cfe53fb238..0a154649769 100644 --- a/tests/Doctrine/Tests/ORM/Tools/Console/ConsoleRunnerTest.php +++ b/tests/Doctrine/Tests/ORM/Tools/Console/ConsoleRunnerTest.php @@ -5,12 +5,10 @@ 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 @@ -18,18 +16,11 @@ */ 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')); @@ -52,16 +43,8 @@ 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)); } - - public function testCreateApplicationWithProvider(): void - { - $provider = $this->createMock(EntityManagerProvider::class); - $app = ConsoleRunner::createApplication($provider, []); - - self::assertTrue($app->has('orm:info')); - } }