Jelajahi Sumber

[DoctrineBundle] moved Doctrine proxy commands to their own sub-namespace

Fabien Potencier 14 tahun lalu
induk
melakukan
03f7049c7e

+ 0 - 32
src/Symfony/Bundle/DoctrineBundle/Command/DoctrineCommand.php

@@ -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;
@@ -30,33 +21,10 @@ use Doctrine\ORM\Tools\EntityGenerator;
 /**
  * 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();

+ 2 - 2
src/Symfony/Bundle/DoctrineBundle/Command/ImportMappingDoctrineCommand.php

@@ -74,8 +74,8 @@ EOT
             $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);
 

+ 1 - 2
src/Symfony/Bundle/DoctrineBundle/Command/InfoDoctrineCommand.php

@@ -47,9 +47,8 @@ EOT
     {
         $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()

+ 2 - 2
src/Symfony/Bundle/DoctrineBundle/Command/ClearMetadataCacheDoctrineCommand.php

@@ -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;
@@ -46,7 +46,7 @@ EOT
 
     protected function execute(InputInterface $input, OutputInterface $output)
     {
-        $this->setApplicationEntityManager($input->getOption('em'));
+        DoctrineCommandHelper::setApplicationEntityManager($this->getApplication(), $input->getOption('em'));
 
         return parent::execute($input, $output);
     }

+ 2 - 2
src/Symfony/Bundle/DoctrineBundle/Command/ClearQueryCacheDoctrineCommand.php

@@ -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;
@@ -46,7 +46,7 @@ EOT
 
     protected function execute(InputInterface $input, OutputInterface $output)
     {
-        $this->setApplicationEntityManager($input->getOption('em'));
+        DoctrineCommandHelper::setApplicationEntityManager($this->getApplication(), $input->getOption('em'));
 
         return parent::execute($input, $output);
     }

+ 2 - 2
src/Symfony/Bundle/DoctrineBundle/Command/ClearResultCacheDoctrineCommand.php

@@ -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;
@@ -58,7 +58,7 @@ EOT
 
     protected function execute(InputInterface $input, OutputInterface $output)
     {
-        $this->setApplicationEntityManager($input->getOption('em'));
+        DoctrineCommandHelper::setApplicationEntityManager($this->getApplication(), $input->getOption('em'));
 
         return parent::execute($input, $output);
     }

+ 2 - 2
src/Symfony/Bundle/DoctrineBundle/Command/ConvertMappingDoctrineCommand.php

@@ -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;
@@ -43,7 +43,7 @@ EOT
 
     protected function execute(InputInterface $input, OutputInterface $output)
     {
-        $this->setApplicationEntityManager($input->getOption('em'));
+        DoctrineCommandHelper::setApplicationEntityManager($this->getApplication(), $input->getOption('em'));
 
         return parent::execute($input, $output);
     }

+ 2 - 2
src/Symfony/Bundle/DoctrineBundle/Command/CreateSchemaDoctrineCommand.php

@@ -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;
@@ -47,7 +47,7 @@ EOT
 
     protected function execute(InputInterface $input, OutputInterface $output)
     {
-        $this->setApplicationEntityManager($input->getOption('em'));
+        DoctrineCommandHelper::setApplicationEntityManager($this->getApplication(), $input->getOption('em'));
 
         parent::execute($input, $output);
     }

+ 79 - 0
src/Symfony/Bundle/DoctrineBundle/Command/Proxy/DoctrineCommandHelper.php

@@ -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]);
+    }
+}

+ 2 - 2
src/Symfony/Bundle/DoctrineBundle/Command/DropSchemaDoctrineCommand.php

@@ -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;
@@ -47,7 +47,7 @@ EOT
 
     protected function execute(InputInterface $input, OutputInterface $output)
     {
-        $this->setApplicationEntityManager($input->getOption('em'));
+        DoctrineCommandHelper::setApplicationEntityManager($this->getApplication(), $input->getOption('em'));
 
         parent::execute($input, $output);
     }

+ 2 - 2
src/Symfony/Bundle/DoctrineBundle/Command/EnsureProductionSettingsDoctrineCommand.php

@@ -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;
@@ -47,7 +47,7 @@ EOT
 
     protected function execute(InputInterface $input, OutputInterface $output)
     {
-        $this->setApplicationEntityManager($input->getOption('em'));
+        DoctrineCommandHelper::setApplicationEntityManager($this->getApplication(), $input->getOption('em'));
 
         parent::execute($input, $output);
     }

+ 2 - 2
src/Symfony/Bundle/DoctrineBundle/Command/GenerateProxiesDoctrineCommand.php

@@ -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;
@@ -47,7 +47,7 @@ EOT
 
     protected function execute(InputInterface $input, OutputInterface $output)
     {
-        $this->setApplicationEntityManager($input->getOption('em'));
+        DoctrineCommandHelper::setApplicationEntityManager($this->getApplication(), $input->getOption('em'));
 
         return parent::execute($input, $output);
     }

+ 2 - 2
src/Symfony/Bundle/DoctrineBundle/Command/RunDqlDoctrineCommand.php

@@ -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;
@@ -51,7 +51,7 @@ EOT
 
     protected function execute(InputInterface $input, OutputInterface $output)
     {
-        $this->setApplicationEntityManager($input->getOption('em'));
+        DoctrineCommandHelper::setApplicationEntityManager($this->getApplication(), $input->getOption('em'));
 
         return parent::execute($input, $output);
     }

+ 3 - 3
src/Symfony/Bundle/DoctrineBundle/Command/RunSqlDoctrineCommand.php

@@ -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;
@@ -43,8 +43,8 @@ EOT
 
     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);
     }
-}
+}

+ 2 - 2
src/Symfony/Bundle/DoctrineBundle/Command/UpdateSchemaDoctrineCommand.php

@@ -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;
@@ -47,7 +47,7 @@ EOT
 
     protected function execute(InputInterface $input, OutputInterface $output)
     {
-        $this->setApplicationEntityManager($input->getOption('em'));
+        DoctrineCommandHelper::setApplicationEntityManager($this->getApplication(), $input->getOption('em'));
 
         parent::execute($input, $output);
     }

+ 0 - 73
src/Symfony/Bundle/DoctrineBundle/Tests/Command/DoctrineCommandTest.php

@@ -1,73 +0,0 @@
-<?php
-
-/*
- * This file is part of the Symfony framework.
- *
- * (c) Fabien Potencier <fabien@symfony.com>
- *
- * This source file is subject to the MIT license that is bundled
- * with this source code in the file LICENSE.
- */
-
-namespace Symfony\Bundle\DoctrineBundle\Tests\Command;
-
-use Symfony\Bundle\DoctrineBundle\Tests\TestCase;
-use Symfony\Bundle\FrameworkBundle\Console\Application;
-use Symfony\Component\DependencyInjection\Container;
-
-class DoctrineCommandTest extends TestCase
-{
-    private $em;
-
-    public function testSetApplicationEntityManager()
-    {
-        $application = $this->getApplication();
-        $command = $this->getCommand($application);
-        $command->setApplicationEntityManager('test');
-
-        $this->assertTrue($application->getHelperSet()->has('em'));
-        $this->assertTrue($application->getHelperSet()->has('db'));
-    }
-
-    public function testSetApplicationConnection()
-    {
-        $application = $this->getApplication();
-        $command = $this->getCommand($application);
-        $command->setApplicationConnection('test');
-
-        $this->assertFalse($application->getHelperSet()->has('em'));
-        $this->assertTrue($application->getHelperSet()->has('db'));
-    }
-
-    protected function getApplication()
-    {
-        return new Application($this->createKernelMock('test'));
-    }
-
-    protected function getCommand($application)
-    {
-        $command = $this->getMockForAbstractClass('Symfony\Bundle\DoctrineBundle\Command\DoctrineCommand', array(), '', false);
-        $command->setApplication($application);
-        $r = new \ReflectionObject($command);
-        $p = $r->getProperty('container');
-        $p->setAccessible(true);
-        $p->setValue($command, $application->getKernel()->getContainer());
-
-        return $command;
-    }
-
-    protected function createKernelMock($name)
-    {
-        $this->em = $this->createTestEntityManager();
-        $kernel = $this->getMock('Symfony\Component\HttpKernel\KernelInterface');
-        $container = new Container();
-        $container->set(sprintf('doctrine.orm.%s_entity_manager', $name), $this->em);
-        $container->set(sprintf('doctrine.dbal.%s_connection', $name), $this->em->getConnection());
-        $container->setParameter('doctrine.orm.entity_managers', array($name => sprintf('doctrine.orm.%s_entity_manager', $name)));
-        $container->setParameter('doctrine.dbal.connections', array($name => sprintf('doctrine.dbal.%s_connection', $name)));
-        $kernel->expects($this->any())->method('getContainer')->will($this->returnValue($container));
-        $kernel->expects($this->any())->method('getBundles')->will($this->returnValue(array()));
-
-        return $kernel;
-    }
-}