From af81538710f1775a37abef5feb9c4dade07fa8f8 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 1/6] 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()) From 557ceabe939506fc7b74f08b116e41464f92a54c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Dunglas?= Date: Fri, 16 Nov 2018 16:47:12 +0100 Subject: [PATCH 2/6] Move DBAL commands to dbal.xml --- Resources/config/dbal.xml | 13 +++++++++++++ Resources/config/orm.xml | 12 ------------ 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/Resources/config/dbal.xml b/Resources/config/dbal.xml index c53360c44..c0f65d7c9 100644 --- a/Resources/config/dbal.xml +++ b/Resources/config/dbal.xml @@ -91,5 +91,18 @@ + + + + + + + + + + + + + diff --git a/Resources/config/orm.xml b/Resources/config/orm.xml index bba2ee9f9..c98239e89 100644 --- a/Resources/config/orm.xml +++ b/Resources/config/orm.xml @@ -213,17 +213,5 @@ - - - - - - - - - - - - From 8a7fe904e0e34146f2fa455b2ebd72af21a70c9b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Dunglas?= Date: Fri, 23 Nov 2018 19:18:36 +0100 Subject: [PATCH 3/6] Fix command definitions --- Resources/config/dbal.xml | 21 ++++----------------- Resources/config/orm.xml | 2 +- 2 files changed, 5 insertions(+), 18 deletions(-) diff --git a/Resources/config/dbal.xml b/Resources/config/dbal.xml index c0f65d7c9..a94e91755 100644 --- a/Resources/config/dbal.xml +++ b/Resources/config/dbal.xml @@ -72,10 +72,14 @@ + + + + @@ -83,26 +87,9 @@ - - - - - - - - - - - - - - - - - diff --git a/Resources/config/orm.xml b/Resources/config/orm.xml index c98239e89..78b039c55 100644 --- a/Resources/config/orm.xml +++ b/Resources/config/orm.xml @@ -200,7 +200,7 @@ - + %kernel.bundles% From e8923b6080488a29a73ac5063bbf1d9b9e08dbde Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Dunglas?= Date: Fri, 23 Nov 2018 19:26:40 +0100 Subject: [PATCH 4/6] Fix CS --- Command/DoctrineCommand.php | 3 +++ Command/ImportMappingDoctrineCommand.php | 3 +++ Repository/ServiceEntityRepository.php | 2 +- Tests/ContainerTest.php | 2 +- 4 files changed, 8 insertions(+), 2 deletions(-) diff --git a/Command/DoctrineCommand.php b/Command/DoctrineCommand.php index 6b47f3bff..70f356254 100644 --- a/Command/DoctrineCommand.php +++ b/Command/DoctrineCommand.php @@ -17,6 +17,9 @@ */ abstract class DoctrineCommand extends Command { + /** + * @var ManagerRegistry + */ protected $doctrine; public function __construct(ManagerRegistry $doctrine) diff --git a/Command/ImportMappingDoctrineCommand.php b/Command/ImportMappingDoctrineCommand.php index 29cbe4cb4..9b947417d 100644 --- a/Command/ImportMappingDoctrineCommand.php +++ b/Command/ImportMappingDoctrineCommand.php @@ -20,6 +20,9 @@ */ class ImportMappingDoctrineCommand extends DoctrineCommand { + /** + * @var string[] + */ private $bundles; /** diff --git a/Repository/ServiceEntityRepository.php b/Repository/ServiceEntityRepository.php index 5d4597fa2..639634f28 100644 --- a/Repository/ServiceEntityRepository.php +++ b/Repository/ServiceEntityRepository.php @@ -29,7 +29,7 @@ public function __construct(ManagerRegistry $registry, $entityClass) { $manager = $registry->getManagerForClass($entityClass); - if (null === $manager) { + if ($manager === null) { throw new LogicException(sprintf( 'Could not find the entity manager for class "%s". Check your Doctrine configuration to make sure it is configured to load this entity’s metadata.', $entityClass diff --git a/Tests/ContainerTest.php b/Tests/ContainerTest.php index 5918739c7..07d393db4 100644 --- a/Tests/ContainerTest.php +++ b/Tests/ContainerTest.php @@ -49,7 +49,7 @@ public function testContainer() $this->assertFalse($container->has('doctrine.dbal.default_connection.events.mysqlsessioninit')); if (interface_exists('Symfony\Component\PropertyInfo\PropertyInfoExtractorInterface') && class_exists('Symfony\Bridge\Doctrine\PropertyInfo\DoctrineExtractor')) { - if (!interface_exists(PropertyInitializableExtractorInterface::class)) { + if (! interface_exists(PropertyInitializableExtractorInterface::class)) { $this->assertInstanceOf('Doctrine\Common\Persistence\Mapping\ClassMetadataFactory', $container->get('doctrine.orm.default_entity_manager.metadata_factory')); } $this->assertInstanceOf('Symfony\Bridge\Doctrine\PropertyInfo\DoctrineExtractor', $container->get('doctrine.orm.default_entity_manager.property_info_extractor')); From f2abf939b6d3c826418582d02d39b7f0f194daa0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Dunglas?= Date: Sat, 24 Nov 2018 10:24:42 +0100 Subject: [PATCH 5/6] Fix CS --- Command/DoctrineCommand.php | 4 +--- Command/ImportMappingDoctrineCommand.php | 4 +--- 2 files changed, 2 insertions(+), 6 deletions(-) diff --git a/Command/DoctrineCommand.php b/Command/DoctrineCommand.php index 70f356254..3a5ab97b4 100644 --- a/Command/DoctrineCommand.php +++ b/Command/DoctrineCommand.php @@ -17,9 +17,7 @@ */ abstract class DoctrineCommand extends Command { - /** - * @var ManagerRegistry - */ + /** @var ManagerRegistry */ protected $doctrine; public function __construct(ManagerRegistry $doctrine) diff --git a/Command/ImportMappingDoctrineCommand.php b/Command/ImportMappingDoctrineCommand.php index 9b947417d..c358e92f8 100644 --- a/Command/ImportMappingDoctrineCommand.php +++ b/Command/ImportMappingDoctrineCommand.php @@ -20,9 +20,7 @@ */ class ImportMappingDoctrineCommand extends DoctrineCommand { - /** - * @var string[] - */ + /** @var string[] */ private $bundles; /** From b69d78aca774d98fd00f2f25535e72532c716da4 Mon Sep 17 00:00:00 2001 From: Robin Chalas Date: Fri, 30 Nov 2018 11:55:38 +0100 Subject: [PATCH 6/6] Add BC layer for container-aware commands --- Command/CreateDatabaseDoctrineCommand.php | 2 +- Command/DoctrineCommand.php | 51 +++++++++++++++++++-- Command/DropDatabaseDoctrineCommand.php | 2 +- Command/GenerateEntitiesDoctrineCommand.php | 4 +- 4 files changed, 50 insertions(+), 9 deletions(-) diff --git a/Command/CreateDatabaseDoctrineCommand.php b/Command/CreateDatabaseDoctrineCommand.php index d6be565ac..f48cc57f0 100644 --- a/Command/CreateDatabaseDoctrineCommand.php +++ b/Command/CreateDatabaseDoctrineCommand.php @@ -46,7 +46,7 @@ protected function execute(InputInterface $input, OutputInterface $output) { $connectionName = $input->getOption('connection'); if (empty($connectionName)) { - $connectionName = $this->doctrine->getDefaultConnectionName(); + $connectionName = $this->getDoctrine()->getDefaultConnectionName(); } $connection = $this->getDoctrineConnection($connectionName); diff --git a/Command/DoctrineCommand.php b/Command/DoctrineCommand.php index 3a5ab97b4..e881d02a2 100644 --- a/Command/DoctrineCommand.php +++ b/Command/DoctrineCommand.php @@ -9,6 +9,7 @@ use Doctrine\ORM\Tools\EntityGenerator; use LogicException; use Symfony\Component\Console\Command\Command; +use Symfony\Component\DependencyInjection\ContainerInterface; /** * Base class for Doctrine console commands to extend from. @@ -17,16 +18,48 @@ */ abstract class DoctrineCommand extends Command { - /** @var ManagerRegistry */ - protected $doctrine; + /** @var ManagerRegistry|null */ + private $doctrine; - public function __construct(ManagerRegistry $doctrine) + /** @var ContainerInterface|null */ + private $container; + + public function __construct(ManagerRegistry $doctrine = null) { parent::__construct(); $this->doctrine = $doctrine; } + /** + * @deprecated + */ + public function setContainer(ContainerInterface $container = null) + { + $this->container = $container; + } + + /** + * @deprecated + * + * @return ContainerInterface + * + * @throws LogicException + */ + protected function getContainer() + { + if ($this->container === null) { + $application = $this->getApplication(); + if ($application === null) { + throw new LogicException('The container cannot be retrieved as the application instance is not yet set.'); + } + + $this->container = $application->getKernel()->getContainer(); + } + + return $this->container; + } + /** * get a doctrine entity generator * @@ -55,7 +88,7 @@ protected function getEntityGenerator() */ protected function getEntityManager($name, $shardId = null) { - $manager = $this->doctrine->getManager($name); + $manager = $this->getDoctrine()->getManager($name); if ($shardId) { if (! $manager->getConnection() instanceof PoolingShardConnection) { @@ -77,6 +110,14 @@ protected function getEntityManager($name, $shardId = null) */ protected function getDoctrineConnection($name) { - return $this->doctrine->getConnection($name); + return $this->getDoctrine()->getConnection($name); + } + + /** + * @return ManagerRegistry + */ + protected function getDoctrine() + { + return $this->doctrine ?: $this->doctrine = $this->getContainer()->get('doctrine'); } } diff --git a/Command/DropDatabaseDoctrineCommand.php b/Command/DropDatabaseDoctrineCommand.php index eef7583e1..e34442e46 100644 --- a/Command/DropDatabaseDoctrineCommand.php +++ b/Command/DropDatabaseDoctrineCommand.php @@ -55,7 +55,7 @@ protected function execute(InputInterface $input, OutputInterface $output) { $connectionName = $input->getOption('connection'); if (empty($connectionName)) { - $connectionName = $this->doctrine->getDefaultConnectionName(); + $connectionName = $this->getDoctrine()->getDefaultConnectionName(); } $connection = $this->getDoctrineConnection($connectionName); diff --git a/Command/GenerateEntitiesDoctrineCommand.php b/Command/GenerateEntitiesDoctrineCommand.php index 22cc5817b..2baeae297 100644 --- a/Command/GenerateEntitiesDoctrineCommand.php +++ b/Command/GenerateEntitiesDoctrineCommand.php @@ -83,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->doctrine); + $manager = new DisconnectedMetadataFactory($this->getDoctrine()); try { $bundle = $this->getApplication()->getKernel()->getBundle($input->getArgument('name')); @@ -95,7 +95,7 @@ protected function execute(InputInterface $input, OutputInterface $output) $pos = strpos($name, ':'); if ($pos !== false) { - $name = $this->doctrine->getAliasNamespace(substr($name, 0, $pos)) . '\\' . substr($name, $pos + 1); + $name = $this->getDoctrine()->getAliasNamespace(substr($name, 0, $pos)) . '\\' . substr($name, $pos + 1); } if (class_exists($name)) {