Skip to content

Commit

Permalink
Merge pull request #877 from dunglas/commands-sf42
Browse files Browse the repository at this point in the history
 Port commands for Symfony 4.2 (follows #876)
  • Loading branch information
kimhemsoe committed Nov 30, 2018
2 parents bca3c13 + b69d78a commit 82d2c63
Show file tree
Hide file tree
Showing 11 changed files with 118 additions and 26 deletions.
4 changes: 3 additions & 1 deletion Command/CreateDatabaseDoctrineCommand.php
Expand Up @@ -11,6 +11,8 @@

/**
* Database tool allows you to easily create your configured databases.
*
* @final
*/
class CreateDatabaseDoctrineCommand extends DoctrineCommand
{
Expand Down Expand Up @@ -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);

Expand Down
62 changes: 58 additions & 4 deletions Command/DoctrineCommand.php
Expand Up @@ -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
*
Expand Down Expand Up @@ -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) {
Expand All @@ -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');
}
}
4 changes: 3 additions & 1 deletion Command/DropDatabaseDoctrineCommand.php
Expand Up @@ -11,6 +11,8 @@

/**
* Database tool allows you to easily drop your configured databases.
*
* @final
*/
class DropDatabaseDoctrineCommand extends DoctrineCommand
{
Expand Down Expand Up @@ -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);

Expand Down
6 changes: 4 additions & 2 deletions Command/GenerateEntitiesDoctrineCommand.php
Expand Up @@ -13,6 +13,8 @@

/**
* Generate entity classes from mapping information
*
* @final
*/
class GenerateEntitiesDoctrineCommand extends DoctrineCommand
{
Expand Down Expand Up @@ -81,7 +83,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
' If you wish to generate your entities, use <info>make:entity --regenerate</info> from MakerBundle instead.',
]);

$manager = new DisconnectedMetadataFactory($this->getContainer()->get('doctrine'));
$manager = new DisconnectedMetadataFactory($this->getDoctrine());

try {
$bundle = $this->getApplication()->getKernel()->getBundle($input->getArgument('name'));
Expand All @@ -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)) {
Expand Down
19 changes: 17 additions & 2 deletions Command/ImportMappingDoctrineCommand.php
Expand Up @@ -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;
Expand All @@ -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}
*/
Expand Down Expand Up @@ -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';

Expand Down
2 changes: 1 addition & 1 deletion Repository/ServiceEntityRepository.php
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions Resources/config/dbal.xml
Expand Up @@ -72,21 +72,21 @@

<!-- commands -->
<service id="doctrine.database_create_command" class="Doctrine\Bundle\DoctrineBundle\Command\CreateDatabaseDoctrineCommand">
<argument type="service" id="doctrine" />

<tag name="console.command" command="doctrine:database:create" />
</service>

<service id="doctrine.database_drop_command" class="Doctrine\Bundle\DoctrineBundle\Command\DropDatabaseDoctrineCommand">
<argument type="service" id="doctrine" />

<tag name="console.command" command="doctrine:database:drop" />
</service>

<service id="doctrine.generate_entities_command" class="Doctrine\Bundle\DoctrineBundle\Command\GenerateEntitiesDoctrineCommand">
<tag name="console.command" command="doctrine:generate:entities" />
</service>

<service id="doctrine.mapping_import_command" class="Doctrine\Bundle\DoctrineBundle\Command\ImportMappingDoctrineCommand">
<tag name="console.command" command="doctrine:mapping:import" />
</service>

<service id="doctrine.query_sql_command" class="Doctrine\Bundle\DoctrineBundle\Command\Proxy\RunSqlDoctrineCommand">
<tag name="console.command" command="doctrine:query:sql" />
</service>
Expand Down
13 changes: 13 additions & 0 deletions Resources/config/orm.xml
Expand Up @@ -200,5 +200,18 @@
<tag name="console.command" command="doctrine:schema:validate" />
</service>

<service id="doctrine.mapping_import_command" class="Doctrine\Bundle\DoctrineBundle\Command\ImportMappingDoctrineCommand">
<argument type="service" id="doctrine" />
<argument>%kernel.bundles%</argument>

<tag name="console.command" command="doctrine:mapping:import" />
</service>

<service id="doctrine.generate_entities_command" class="Doctrine\Bundle\DoctrineBundle\Command\GenerateEntitiesDoctrineCommand">
<argument type="service" id="doctrine" />

<tag name="console.command" command="doctrine:generate:entities" />
</service>

</services>
</container>
12 changes: 7 additions & 5 deletions Tests/Command/CreateDatabaseDoctrineTest.php
Expand Up @@ -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(
Expand Down Expand Up @@ -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]);
Expand All @@ -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())
Expand Down
12 changes: 7 additions & 5 deletions Tests/Command/DropDatabaseDoctrineTest.php
Expand Up @@ -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(
Expand All @@ -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(
Expand All @@ -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())
Expand Down
2 changes: 1 addition & 1 deletion Tests/ContainerTest.php
Expand Up @@ -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'));
Expand Down

0 comments on commit 82d2c63

Please sign in to comment.