From 69016178b12897a31e1ea2bf13759a3678061ba8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Dunglas?= Date: Fri, 16 Nov 2018 00:43:05 +0100 Subject: [PATCH] Port commands for Symfony 4.2 (follows #876) --- Command/CreateDatabaseDoctrineCommand.php | 4 +++- Command/DoctrineCommand.php | 20 ++++++++++++---- Command/DropDatabaseDoctrineCommand.php | 4 +++- Command/GenerateEntitiesDoctrineCommand.php | 6 +++-- Command/ImportMappingDoctrineCommand.php | 18 ++++++++++++-- Resources/config/orm.xml | 25 ++++++++++++++++++++ Tests/Command/CreateDatabaseDoctrineTest.php | 12 ++++++---- Tests/Command/DropDatabaseDoctrineTest.php | 12 ++++++---- 8 files changed, 81 insertions(+), 20 deletions(-) diff --git a/Command/CreateDatabaseDoctrineCommand.php b/Command/CreateDatabaseDoctrineCommand.php index 504e27e8e..d6be565ac 100644 --- a/Command/CreateDatabaseDoctrineCommand.php +++ b/Command/CreateDatabaseDoctrineCommand.php @@ -11,6 +11,8 @@ /** * Database tool allows you to easily create your configured databases. + * + * @final */ class CreateDatabaseDoctrineCommand extends DoctrineCommand { @@ -44,7 +46,7 @@ protected function execute(InputInterface $input, OutputInterface $output) { $connectionName = $input->getOption('connection'); if (empty($connectionName)) { - $connectionName = $this->getContainer()->get('doctrine')->getDefaultConnectionName(); + $connectionName = $this->doctrine->getDefaultConnectionName(); } $connection = $this->getDoctrineConnection($connectionName); diff --git a/Command/DoctrineCommand.php b/Command/DoctrineCommand.php index f136d52ff..6b47f3bff 100644 --- a/Command/DoctrineCommand.php +++ b/Command/DoctrineCommand.php @@ -2,18 +2,30 @@ namespace Doctrine\Bundle\DoctrineBundle\Command; +use Doctrine\Common\Persistence\ManagerRegistry; use Doctrine\DBAL\Connection; use Doctrine\DBAL\Sharding\PoolingShardConnection; use Doctrine\ORM\EntityManager; use Doctrine\ORM\Tools\EntityGenerator; use LogicException; -use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; +use Symfony\Component\Console\Command\Command; /** * Base class for Doctrine console commands to extend from. + * + * @internal */ -abstract class DoctrineCommand extends ContainerAwareCommand +abstract class DoctrineCommand extends Command { + protected $doctrine; + + public function __construct(ManagerRegistry $doctrine) + { + parent::__construct(); + + $this->doctrine = $doctrine; + } + /** * get a doctrine entity generator * @@ -42,7 +54,7 @@ protected function getEntityGenerator() */ protected function getEntityManager($name, $shardId = null) { - $manager = $this->getContainer()->get('doctrine')->getManager($name); + $manager = $this->doctrine->getManager($name); if ($shardId) { if (! $manager->getConnection() instanceof PoolingShardConnection) { @@ -64,6 +76,6 @@ protected function getEntityManager($name, $shardId = null) */ protected function getDoctrineConnection($name) { - return $this->getContainer()->get('doctrine')->getConnection($name); + return $this->doctrine->getConnection($name); } } diff --git a/Command/DropDatabaseDoctrineCommand.php b/Command/DropDatabaseDoctrineCommand.php index fee312495..eef7583e1 100644 --- a/Command/DropDatabaseDoctrineCommand.php +++ b/Command/DropDatabaseDoctrineCommand.php @@ -11,6 +11,8 @@ /** * Database tool allows you to easily drop your configured databases. + * + * @final */ class DropDatabaseDoctrineCommand extends DoctrineCommand { @@ -53,7 +55,7 @@ protected function execute(InputInterface $input, OutputInterface $output) { $connectionName = $input->getOption('connection'); if (empty($connectionName)) { - $connectionName = $this->getContainer()->get('doctrine')->getDefaultConnectionName(); + $connectionName = $this->doctrine->getDefaultConnectionName(); } $connection = $this->getDoctrineConnection($connectionName); diff --git a/Command/GenerateEntitiesDoctrineCommand.php b/Command/GenerateEntitiesDoctrineCommand.php index 80dc6898f..22cc5817b 100644 --- a/Command/GenerateEntitiesDoctrineCommand.php +++ b/Command/GenerateEntitiesDoctrineCommand.php @@ -13,6 +13,8 @@ /** * Generate entity classes from mapping information + * + * @final */ class GenerateEntitiesDoctrineCommand extends DoctrineCommand { @@ -81,7 +83,7 @@ protected function execute(InputInterface $input, OutputInterface $output) ' If you wish to generate your entities, use make:entity --regenerate from MakerBundle instead.', ]); - $manager = new DisconnectedMetadataFactory($this->getContainer()->get('doctrine')); + $manager = new DisconnectedMetadataFactory($this->doctrine); try { $bundle = $this->getApplication()->getKernel()->getBundle($input->getArgument('name')); @@ -93,7 +95,7 @@ protected function execute(InputInterface $input, OutputInterface $output) $pos = strpos($name, ':'); if ($pos !== false) { - $name = $this->getContainer()->get('doctrine')->getAliasNamespace(substr($name, 0, $pos)) . '\\' . substr($name, $pos + 1); + $name = $this->doctrine->getAliasNamespace(substr($name, 0, $pos)) . '\\' . substr($name, $pos + 1); } if (class_exists($name)) { diff --git a/Command/ImportMappingDoctrineCommand.php b/Command/ImportMappingDoctrineCommand.php index 39a42b6dc..29cbe4cb4 100644 --- a/Command/ImportMappingDoctrineCommand.php +++ b/Command/ImportMappingDoctrineCommand.php @@ -2,6 +2,7 @@ namespace Doctrine\Bundle\DoctrineBundle\Command; +use Doctrine\Common\Persistence\ManagerRegistry; use Doctrine\ORM\Mapping\Driver\DatabaseDriver; use Doctrine\ORM\Tools\Console\MetadataFilter; use Doctrine\ORM\Tools\DisconnectedClassMetadataFactory; @@ -14,9 +15,23 @@ /** * Import Doctrine ORM metadata mapping information from an existing database. + * + * @final */ class ImportMappingDoctrineCommand extends DoctrineCommand { + private $bundles; + + /** + * @param string[] $bundles + */ + public function __construct(ManagerRegistry $doctrine, array $bundles) + { + parent::__construct($doctrine); + + $this->bundles = $bundles; + } + /** * {@inheritDoc} */ @@ -74,8 +89,7 @@ protected function execute(InputInterface $input, OutputInterface $output) } $namespaceOrBundle = $input->getArgument('name'); - $bundles = $this->getContainer()->getParameter('kernel.bundles'); - if (isset($bundles[$namespaceOrBundle])) { + if (isset($this->bundles[$namespaceOrBundle])) { $bundle = $this->getApplication()->getKernel()->getBundle($namespaceOrBundle); $namespace = $bundle->getNamespace() . '\Entity'; diff --git a/Resources/config/orm.xml b/Resources/config/orm.xml index c9e6a81af..bba2ee9f9 100644 --- a/Resources/config/orm.xml +++ b/Resources/config/orm.xml @@ -200,5 +200,30 @@ + + + %kernel.bundles% + + + + + + + + + + + + + + + + + + + + + + diff --git a/Tests/Command/CreateDatabaseDoctrineTest.php b/Tests/Command/CreateDatabaseDoctrineTest.php index 29d940620..91177576e 100644 --- a/Tests/Command/CreateDatabaseDoctrineTest.php +++ b/Tests/Command/CreateDatabaseDoctrineTest.php @@ -26,11 +26,12 @@ public function testExecute() 'driver' => 'pdo_sqlite', ]; + $container = $this->getMockContainer($connectionName, $params); + $application = new Application(); - $application->add(new CreateDatabaseDoctrineCommand()); + $application->add(new CreateDatabaseDoctrineCommand($container->get('doctrine'))); $command = $application->find('doctrine:database:create'); - $command->setContainer($this->getMockContainer($connectionName, $params)); $commandTester = new CommandTester($command); $commandTester->execute( @@ -65,11 +66,12 @@ public function testExecuteWithShardOption() ], ]; + $container = $this->getMockContainer($connectionName, $params); + $application = new Application(); - $application->add(new CreateDatabaseDoctrineCommand()); + $application->add(new CreateDatabaseDoctrineCommand($container->get('doctrine'))); $command = $application->find('doctrine:database:create'); - $command->setContainer($this->getMockContainer($connectionName, $params)); $commandTester = new CommandTester($command); $commandTester->execute(['command' => $command->getName(), '--shard' => 1]); @@ -91,7 +93,7 @@ public function testExecuteWithShardOption() private function getMockContainer($connectionName, $params = null) { // Mock the container and everything you'll need here - $mockDoctrine = $this->getMockBuilder('Doctrine\Common\Persistence\ConnectionRegistry') + $mockDoctrine = $this->getMockBuilder('Doctrine\Common\Persistence\ManagerRegistry') ->getMock(); $mockDoctrine->expects($this->any()) diff --git a/Tests/Command/DropDatabaseDoctrineTest.php b/Tests/Command/DropDatabaseDoctrineTest.php index 4b3a09cf7..79770032e 100644 --- a/Tests/Command/DropDatabaseDoctrineTest.php +++ b/Tests/Command/DropDatabaseDoctrineTest.php @@ -20,11 +20,12 @@ public function testExecute() 'driver' => 'pdo_sqlite', ]; + $container = $this->getMockContainer($connectionName, $params); + $application = new Application(); - $application->add(new DropDatabaseDoctrineCommand()); + $application->add(new DropDatabaseDoctrineCommand($container->get('doctrine'))); $command = $application->find('doctrine:database:drop'); - $command->setContainer($this->getMockContainer($connectionName, $params)); $commandTester = new CommandTester($command); $commandTester->execute( @@ -50,11 +51,12 @@ public function testExecuteWithoutOptionForceWillFailWithAttentionMessage() 'driver' => 'pdo_sqlite', ]; + $container = $this->getMockContainer($connectionName, $params); + $application = new Application(); - $application->add(new DropDatabaseDoctrineCommand()); + $application->add(new DropDatabaseDoctrineCommand($container->get('doctrine'))); $command = $application->find('doctrine:database:drop'); - $command->setContainer($this->getMockContainer($connectionName, $params)); $commandTester = new CommandTester($command); $commandTester->execute( @@ -81,7 +83,7 @@ public function testExecuteWithoutOptionForceWillFailWithAttentionMessage() private function getMockContainer($connectionName, $params = null) { // Mock the container and everything you'll need here - $mockDoctrine = $this->getMockBuilder('Doctrine\Common\Persistence\ConnectionRegistry') + $mockDoctrine = $this->getMockBuilder('Doctrine\Common\Persistence\ManagerRegistry') ->getMock(); $mockDoctrine->expects($this->any())