Skip to content

Commit

Permalink
Port commands for Symfony 4.2 (follows #876)
Browse files Browse the repository at this point in the history
  • Loading branch information
dunglas committed Nov 15, 2018
1 parent 07be011 commit f9e03e8
Show file tree
Hide file tree
Showing 8 changed files with 81 additions and 20 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->doctrine->getDefaultConnectionName();
}
$connection = $this->getDoctrineConnection($connectionName);

Expand Down
20 changes: 16 additions & 4 deletions Command/DoctrineCommand.php
Expand Up @@ -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
*
Expand Down Expand Up @@ -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) {
Expand All @@ -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);
}
}
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->doctrine->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->doctrine);

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->doctrine->getAliasNamespace(substr($name, 0, $pos)) . '\\' . substr($name, $pos + 1);
}

if (class_exists($name)) {
Expand Down
18 changes: 16 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,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}
*/
Expand Down Expand Up @@ -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';

Expand Down
25 changes: 25 additions & 0 deletions Resources/config/orm.xml
Expand Up @@ -200,5 +200,30 @@
<tag name="console.command" command="doctrine:schema:validate" />
</service>

<service id="doctrine.import_mapping_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>

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

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

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

<tag name="console.command" command="doctrine:database:drop" />
</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

0 comments on commit f9e03e8

Please sign in to comment.