|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <fabien@symfony.com> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Symfony\Bundle\DoctrineBundle\Command\Proxy; |
| 13 | + |
| 14 | +use Symfony\Component\Console\Application; |
| 15 | +use Doctrine\DBAL\Tools\Console\Helper\ConnectionHelper; |
| 16 | +use Doctrine\ORM\Tools\Console\Helper\EntityManagerHelper; |
| 17 | + |
| 18 | +/** |
| 19 | + * Provides some helper and convenience methods to configure doctrine commands in the context of bundles |
| 20 | + * and multiple connections/entity managers. |
| 21 | + * |
| 22 | + * @author Fabien Potencier <fabien@symfony.com> |
| 23 | + */ |
| 24 | +abstract class DoctrineCommandHelper |
| 25 | +{ |
| 26 | + /** |
| 27 | + * Convenience method to push the helper sets of a given entity manager into the application. |
| 28 | + * |
| 29 | + * @param string $emName |
| 30 | + */ |
| 31 | + static public function setApplicationEntityManager(Application $application, $emName) |
| 32 | + { |
| 33 | + $em = self::getEntityManager($application, $emName); |
| 34 | + $helperSet = $application->getHelperSet(); |
| 35 | + $helperSet->set(new ConnectionHelper($em->getConnection()), 'db'); |
| 36 | + $helperSet->set(new EntityManagerHelper($em), 'em'); |
| 37 | + } |
| 38 | + |
| 39 | + static public function setApplicationConnection(Application $application, $connName) |
| 40 | + { |
| 41 | + $connection = self::getDoctrineConnection($application, $connName); |
| 42 | + $helperSet = $application->getHelperSet(); |
| 43 | + $helperSet->set(new ConnectionHelper($connection), 'db'); |
| 44 | + } |
| 45 | + |
| 46 | + static protected function getEntityManager(Application $application, $name) |
| 47 | + { |
| 48 | + $container = $application->getKernel()->getContainer(); |
| 49 | + |
| 50 | + $name = $name ?: $container->getParameter('doctrine.orm.default_entity_manager'); |
| 51 | + |
| 52 | + $ems = $container->getParameter('doctrine.orm.entity_managers'); |
| 53 | + if (!isset($ems[$name])) { |
| 54 | + throw new \InvalidArgumentException(sprintf('Could not find Doctrine EntityManager named "%s"', $name)); |
| 55 | + } |
| 56 | + |
| 57 | + return $container->get($ems[$name]); |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * Get a doctrine dbal connection by symfony name. |
| 62 | + * |
| 63 | + * @param string $name |
| 64 | + * @return Doctrine\DBAL\Connection |
| 65 | + */ |
| 66 | + static protected function getDoctrineConnection(Application $application, $name) |
| 67 | + { |
| 68 | + $container = $application->getKernel()->getContainer(); |
| 69 | + |
| 70 | + $name = $name ?: $container->getParameter('doctrine.dbal.default_connection'); |
| 71 | + |
| 72 | + $connections = $container->getParameter('doctrine.dbal.connections'); |
| 73 | + if (!isset($connections[$name])) { |
| 74 | + throw new \InvalidArgumentException(sprintf('<error>Could not find a connection named <comment>%s</comment></error>', $name)); |
| 75 | + } |
| 76 | + |
| 77 | + return $container->get($connections[$name]); |
| 78 | + } |
| 79 | +} |
0 commit comments