From 70548fdf26b5fc0fb7773daaa663f49430d7d114 Mon Sep 17 00:00:00 2001 From: Robin Chalas Date: Fri, 30 Nov 2018 11:55:38 +0100 Subject: [PATCH] Add BC layer for container-aware commands --- Command/CreateDatabaseDoctrineCommand.php | 2 +- Command/DoctrineCommand.php | 63 +++++++++++++++++++-- Command/DropDatabaseDoctrineCommand.php | 2 +- Command/GenerateEntitiesDoctrineCommand.php | 4 +- 4 files changed, 62 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..c3aea6be0 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,60 @@ */ 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(); + if (null === $doctrine) { + @trigger_error(sprintf( + 'The "%s" constructor expects a "%s" instance as first argument, not passing it will throw a \TypeError in DoctrineBundle 2.0.', + get_class($this), + ManagerRegistry::class + ), E_USER_DEPRECATED); + } + $this->doctrine = $doctrine; } + /** + * @deprecated + */ + public function setContainer(ContainerInterface $container = null) + { + @trigger_error(sprintf('The "%s()" method is deprecated and will be removed in DoctrineBundle 2.0.', __METHOD__), E_USER_DEPRECATED); + + $this->container = $container; + } + + /** + * @return ContainerInterface + * + * @throws \LogicException + * + * @deprecated + */ + protected function getContainer() + { + @trigger_error(sprintf('The "%s()" method is deprecated and will be removed in DoctrineBundle 2.0.', __METHOD__), E_USER_DEPRECATED); + + if (null === $this->container) { + $application = $this->getApplication(); + if (null === $application) { + 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 +100,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 +122,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)) {