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)) {