Skip to content

Commit

Permalink
[DoctrineBundle] moved Doctrine proxy commands to their own sub-names…
Browse files Browse the repository at this point in the history
…pace
  • Loading branch information
fabpot committed May 1, 2011
1 parent 6edb644 commit 03f7049
Show file tree
Hide file tree
Showing 16 changed files with 105 additions and 132 deletions.
32 changes: 0 additions & 32 deletions src/Symfony/Bundle/DoctrineBundle/Command/DoctrineCommand.php
Expand Up @@ -12,16 +12,7 @@
namespace Symfony\Bundle\DoctrineBundle\Command;

use Symfony\Bundle\FrameworkBundle\Command\Command;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Output\Output;
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Component\HttpKernel\Bundle\Bundle;
use Doctrine\DBAL\Tools\Console\Helper\ConnectionHelper;
use Doctrine\ORM\Tools\Console\Helper\EntityManagerHelper;
use Doctrine\ORM\Tools\DisconnectedClassMetadataFactory;
use Doctrine\ORM\Mapping\ClassMetadata;
use Doctrine\ORM\Mapping\ClassMetadataInfo;
Expand All @@ -30,33 +21,10 @@
/**
* Base class for Doctrine console commands to extend from.
*
* Provides some helper and convenience methods to configure doctrine commands in the context of bundles
* and multiple connections/entity managers.
*
* @author Fabien Potencier <fabien@symfony.com>
*/
abstract class DoctrineCommand extends Command
{
/**
* Convenience method to push the helper sets of a given entity manager into the application.
*
* @param string $emName
*/
public function setApplicationEntityManager($emName)
{
$em = self::getEntityManager($emName);
$helperSet = $this->getApplication()->getHelperSet();
$helperSet->set(new ConnectionHelper($em->getConnection()), 'db');
$helperSet->set(new EntityManagerHelper($em), 'em');
}

public function setApplicationConnection($connName)
{
$connection = $this->getDoctrineConnection($connName);
$helperSet = $this->getApplication()->getHelperSet();
$helperSet->set(new ConnectionHelper($connection), 'db');
}

protected function getEntityGenerator()
{
$entityGenerator = new EntityGenerator();
Expand Down
Expand Up @@ -74,8 +74,8 @@ protected function execute(InputInterface $input, OutputInterface $output)
$exporter->setEntityGenerator($entityGenerator);
}

$this->setApplicationEntityManager($input->getOption('em'));
$em = $this->getHelper('em')->getEntityManager();
$em = $this->getEntityManager($input->getOption('em'));

$databaseDriver = new DatabaseDriver($em->getConnection()->getSchemaManager());
$em->getConfiguration()->setMetadataDriverImpl($databaseDriver);

Expand Down
Expand Up @@ -47,9 +47,8 @@ protected function execute(InputInterface $input, OutputInterface $output)
{
$entityManagerName = $input->getOption('em') ? $input->getOption('em') : $this->container->getParameter('doctrine.orm.default_entity_manager');

$this->setApplicationEntityManager($input->getOption('em'));
/* @var $entityManager Doctrine\ORM\EntityManager */
$entityManager = $this->getHelper('em')->getEntityManager();
$entityManager = $this->getEntityManager($input->getOption('em'));

$entityClassNames = $entityManager->getConfiguration()
->getMetadataDriverImpl()
Expand Down
Expand Up @@ -9,7 +9,7 @@
* file that was distributed with this source code.
*/

namespace Symfony\Bundle\DoctrineBundle\Command;
namespace Symfony\Bundle\DoctrineBundle\Command\Proxy;

use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputInterface;
Expand Down Expand Up @@ -46,7 +46,7 @@ protected function configure()

protected function execute(InputInterface $input, OutputInterface $output)
{
$this->setApplicationEntityManager($input->getOption('em'));
DoctrineCommandHelper::setApplicationEntityManager($this->getApplication(), $input->getOption('em'));

return parent::execute($input, $output);
}
Expand Down
Expand Up @@ -9,7 +9,7 @@
* file that was distributed with this source code.
*/

namespace Symfony\Bundle\DoctrineBundle\Command;
namespace Symfony\Bundle\DoctrineBundle\Command\Proxy;

use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputInterface;
Expand Down Expand Up @@ -46,7 +46,7 @@ protected function configure()

protected function execute(InputInterface $input, OutputInterface $output)
{
$this->setApplicationEntityManager($input->getOption('em'));
DoctrineCommandHelper::setApplicationEntityManager($this->getApplication(), $input->getOption('em'));

return parent::execute($input, $output);
}
Expand Down
Expand Up @@ -9,7 +9,7 @@
* file that was distributed with this source code.
*/

namespace Symfony\Bundle\DoctrineBundle\Command;
namespace Symfony\Bundle\DoctrineBundle\Command\Proxy;

use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputInterface;
Expand Down Expand Up @@ -58,7 +58,7 @@ protected function configure()

protected function execute(InputInterface $input, OutputInterface $output)
{
$this->setApplicationEntityManager($input->getOption('em'));
DoctrineCommandHelper::setApplicationEntityManager($this->getApplication(), $input->getOption('em'));

return parent::execute($input, $output);
}
Expand Down
Expand Up @@ -9,7 +9,7 @@
* file that was distributed with this source code.
*/

namespace Symfony\Bundle\DoctrineBundle\Command;
namespace Symfony\Bundle\DoctrineBundle\Command\Proxy;

use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;
Expand Down Expand Up @@ -43,7 +43,7 @@ protected function configure()

protected function execute(InputInterface $input, OutputInterface $output)
{
$this->setApplicationEntityManager($input->getOption('em'));
DoctrineCommandHelper::setApplicationEntityManager($this->getApplication(), $input->getOption('em'));

return parent::execute($input, $output);
}
Expand Down
Expand Up @@ -9,7 +9,7 @@
* file that was distributed with this source code.
*/

namespace Symfony\Bundle\DoctrineBundle\Command;
namespace Symfony\Bundle\DoctrineBundle\Command\Proxy;

use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;
Expand Down Expand Up @@ -47,7 +47,7 @@ protected function configure()

protected function execute(InputInterface $input, OutputInterface $output)
{
$this->setApplicationEntityManager($input->getOption('em'));
DoctrineCommandHelper::setApplicationEntityManager($this->getApplication(), $input->getOption('em'));

parent::execute($input, $output);
}
Expand Down
@@ -0,0 +1,79 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Bundle\DoctrineBundle\Command\Proxy;

use Symfony\Component\Console\Application;
use Doctrine\DBAL\Tools\Console\Helper\ConnectionHelper;
use Doctrine\ORM\Tools\Console\Helper\EntityManagerHelper;

/**
* Provides some helper and convenience methods to configure doctrine commands in the context of bundles
* and multiple connections/entity managers.
*
* @author Fabien Potencier <fabien@symfony.com>
*/
abstract class DoctrineCommandHelper
{
/**
* Convenience method to push the helper sets of a given entity manager into the application.
*
* @param string $emName
*/
static public function setApplicationEntityManager(Application $application, $emName)
{
$em = self::getEntityManager($application, $emName);
$helperSet = $application->getHelperSet();
$helperSet->set(new ConnectionHelper($em->getConnection()), 'db');
$helperSet->set(new EntityManagerHelper($em), 'em');
}

static public function setApplicationConnection(Application $application, $connName)
{
$connection = self::getDoctrineConnection($application, $connName);
$helperSet = $application->getHelperSet();
$helperSet->set(new ConnectionHelper($connection), 'db');
}

static protected function getEntityManager(Application $application, $name)
{
$container = $application->getKernel()->getContainer();

$name = $name ?: $container->getParameter('doctrine.orm.default_entity_manager');

$ems = $container->getParameter('doctrine.orm.entity_managers');
if (!isset($ems[$name])) {
throw new \InvalidArgumentException(sprintf('Could not find Doctrine EntityManager named "%s"', $name));
}

return $container->get($ems[$name]);
}

/**
* Get a doctrine dbal connection by symfony name.
*
* @param string $name
* @return Doctrine\DBAL\Connection
*/
static protected function getDoctrineConnection(Application $application, $name)
{
$container = $application->getKernel()->getContainer();

$name = $name ?: $container->getParameter('doctrine.dbal.default_connection');

$connections = $container->getParameter('doctrine.dbal.connections');
if (!isset($connections[$name])) {
throw new \InvalidArgumentException(sprintf('<error>Could not find a connection named <comment>%s</comment></error>', $name));
}

return $container->get($connections[$name]);
}
}
Expand Up @@ -9,7 +9,7 @@
* file that was distributed with this source code.
*/

namespace Symfony\Bundle\DoctrineBundle\Command;
namespace Symfony\Bundle\DoctrineBundle\Command\Proxy;

use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;
Expand Down Expand Up @@ -47,7 +47,7 @@ protected function configure()

protected function execute(InputInterface $input, OutputInterface $output)
{
$this->setApplicationEntityManager($input->getOption('em'));
DoctrineCommandHelper::setApplicationEntityManager($this->getApplication(), $input->getOption('em'));

parent::execute($input, $output);
}
Expand Down
Expand Up @@ -9,7 +9,7 @@
* file that was distributed with this source code.
*/

namespace Symfony\Bundle\DoctrineBundle\Command;
namespace Symfony\Bundle\DoctrineBundle\Command\Proxy;

use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;
Expand Down Expand Up @@ -47,7 +47,7 @@ protected function configure()

protected function execute(InputInterface $input, OutputInterface $output)
{
$this->setApplicationEntityManager($input->getOption('em'));
DoctrineCommandHelper::setApplicationEntityManager($this->getApplication(), $input->getOption('em'));

parent::execute($input, $output);
}
Expand Down
Expand Up @@ -9,7 +9,7 @@
* file that was distributed with this source code.
*/

namespace Symfony\Bundle\DoctrineBundle\Command;
namespace Symfony\Bundle\DoctrineBundle\Command\Proxy;

use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;
Expand Down Expand Up @@ -47,7 +47,7 @@ protected function configure()

protected function execute(InputInterface $input, OutputInterface $output)
{
$this->setApplicationEntityManager($input->getOption('em'));
DoctrineCommandHelper::setApplicationEntityManager($this->getApplication(), $input->getOption('em'));

return parent::execute($input, $output);
}
Expand Down
Expand Up @@ -9,7 +9,7 @@
* file that was distributed with this source code.
*/

namespace Symfony\Bundle\DoctrineBundle\Command;
namespace Symfony\Bundle\DoctrineBundle\Command\Proxy;

use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;
Expand Down Expand Up @@ -51,7 +51,7 @@ protected function configure()

protected function execute(InputInterface $input, OutputInterface $output)
{
$this->setApplicationEntityManager($input->getOption('em'));
DoctrineCommandHelper::setApplicationEntityManager($this->getApplication(), $input->getOption('em'));

return parent::execute($input, $output);
}
Expand Down
Expand Up @@ -9,7 +9,7 @@
* file that was distributed with this source code.
*/

namespace Symfony\Bundle\DoctrineBundle\Command;
namespace Symfony\Bundle\DoctrineBundle\Command\Proxy;

use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;
Expand Down Expand Up @@ -43,8 +43,8 @@ protected function configure()

protected function execute(InputInterface $input, OutputInterface $output)
{
$this->setApplicationConnection($this->getApplication(), $input->getOption('connection'));
DoctrineCommandHelper::setApplicationConnection($this->getApplication(), $input->getOption('connection'));

return parent::execute($input, $output);
}
}
}
Expand Up @@ -9,7 +9,7 @@
* file that was distributed with this source code.
*/

namespace Symfony\Bundle\DoctrineBundle\Command;
namespace Symfony\Bundle\DoctrineBundle\Command\Proxy;

use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;
Expand Down Expand Up @@ -47,7 +47,7 @@ protected function configure()

protected function execute(InputInterface $input, OutputInterface $output)
{
$this->setApplicationEntityManager($input->getOption('em'));
DoctrineCommandHelper::setApplicationEntityManager($this->getApplication(), $input->getOption('em'));

parent::execute($input, $output);
}
Expand Down

2 comments on commit 03f7049

@beberlei
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, the commit log shows everything being moved to Command/Proxy not just the proxy commands?

@fabpot
Copy link
Member Author

@fabpot fabpot commented on 03f7049 May 1, 2011

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Proxy sub-namespace is for commands that are just proxies to the ones from the Doctrine project.

Please sign in to comment.