diff --git a/Command/CreateDatabaseDoctrineCommand.php b/Command/CreateDatabaseDoctrineCommand.php
index 504e27e8e..f48cc57f0 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->getDoctrine()->getDefaultConnectionName();
}
$connection = $this->getDoctrineConnection($connectionName);
diff --git a/Command/DoctrineCommand.php b/Command/DoctrineCommand.php
index f136d52ff..e881d02a2 100644
--- a/Command/DoctrineCommand.php
+++ b/Command/DoctrineCommand.php
@@ -2,18 +2,64 @@
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;
+use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Base class for Doctrine console commands to extend from.
+ *
+ * @internal
*/
-abstract class DoctrineCommand extends ContainerAwareCommand
+abstract class DoctrineCommand extends Command
{
+ /** @var ManagerRegistry|null */
+ private $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
*
@@ -42,7 +88,7 @@ protected function getEntityGenerator()
*/
protected function getEntityManager($name, $shardId = null)
{
- $manager = $this->getContainer()->get('doctrine')->getManager($name);
+ $manager = $this->getDoctrine()->getManager($name);
if ($shardId) {
if (! $manager->getConnection() instanceof PoolingShardConnection) {
@@ -64,6 +110,14 @@ protected function getEntityManager($name, $shardId = null)
*/
protected function getDoctrineConnection($name)
{
- return $this->getContainer()->get('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 fee312495..e34442e46 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->getDoctrine()->getDefaultConnectionName();
}
$connection = $this->getDoctrineConnection($connectionName);
diff --git a/Command/GenerateEntitiesDoctrineCommand.php b/Command/GenerateEntitiesDoctrineCommand.php
index 80dc6898f..2baeae297 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->getDoctrine());
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->getDoctrine()->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..c358e92f8 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,24 @@
/**
* Import Doctrine ORM metadata mapping information from an existing database.
+ *
+ * @final
*/
class ImportMappingDoctrineCommand extends DoctrineCommand
{
+ /** @var string[] */
+ private $bundles;
+
+ /**
+ * @param string[] $bundles
+ */
+ public function __construct(ManagerRegistry $doctrine, array $bundles)
+ {
+ parent::__construct($doctrine);
+
+ $this->bundles = $bundles;
+ }
+
/**
* {@inheritDoc}
*/
@@ -74,8 +90,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/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/Resources/config/dbal.xml b/Resources/config/dbal.xml
index c53360c44..a94e91755 100644
--- a/Resources/config/dbal.xml
+++ b/Resources/config/dbal.xml
@@ -72,10 +72,14 @@
+
+
+
+
@@ -83,10 +87,6 @@
-
-
-
-
diff --git a/Resources/config/orm.xml b/Resources/config/orm.xml
index c9e6a81af..78b039c55 100644
--- a/Resources/config/orm.xml
+++ b/Resources/config/orm.xml
@@ -200,5 +200,18 @@
+
+
+ %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())
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'));