1
0
mirror of synced 2025-01-31 12:32:59 +03:00

#1120 - avoiding storing the Doctrine\ORM\EntityManager in the command itself

This commit is contained in:
Marco Pivetta 2014-10-19 17:49:28 +02:00
parent e9d7c23261
commit cf078d8da8

View File

@ -19,12 +19,12 @@
namespace Doctrine\ORM\Tools\Console\Command; namespace Doctrine\ORM\Tools\Console\Command;
use Doctrine\ORM\Mapping\MappingException; use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Helper\TableHelper;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Helper\TableHelper;
/** /**
* Show information about mapped entities. * Show information about mapped entities.
@ -79,7 +79,7 @@ EOT
$this->output = $output; $this->output = $output;
$this->entityManager = $entityManager; $this->entityManager = $entityManager;
$this->displayEntity($entityName); $this->displayEntity($entityName, $entityManager);
return 0; return 0;
} }
@ -88,10 +88,11 @@ EOT
* Display all the mapping information for a single Entity. * Display all the mapping information for a single Entity.
* *
* @param string $entityName Full or partial entity class name * @param string $entityName Full or partial entity class name
* @param EntityManagerInterface $entityManager
*/ */
private function displayEntity($entityName) private function displayEntity($entityName, EntityManagerInterface $entityManager)
{ {
$meta = $this->getClassMetadata($entityName); $meta = $this->getClassMetadata($entityName, $entityManager);
$this->formatField('Name', $meta->name); $this->formatField('Name', $meta->name);
$this->formatField('Root entity name', $meta->rootEntityName); $this->formatField('Root entity name', $meta->rootEntityName);
@ -144,15 +145,18 @@ EOT
/** /**
* Return all mapped entity class names * Return all mapped entity class names
* *
* @return array * @param EntityManagerInterface $entityManager
*
* @return \Doctrine\ORM\Mapping\ClassMetadata[]
*/ */
private function getMappedEntities() private function getMappedEntities(EntityManagerInterface $entityManager)
{ {
$entityClassNames = $this->entityManager->getConfiguration() $entityClassNames = $entityManager
->getConfiguration()
->getMetadataDriverImpl() ->getMetadataDriverImpl()
->getAllClassNames(); ->getAllClassNames();
if (!$entityClassNames) { if ( ! $entityClassNames) {
throw new \InvalidArgumentException( throw new \InvalidArgumentException(
'You do not have any mapped Doctrine ORM entities according to the current configuration. '. 'You do not have any mapped Doctrine ORM entities according to the current configuration. '.
'If you have entities or mapping files you should check your mapping configuration for errors.' 'If you have entities or mapping files you should check your mapping configuration for errors.'
@ -167,13 +171,16 @@ EOT
* name * name
* *
* @param string $entityName Full or partial entity name * @param string $entityName Full or partial entity name
* @param EntityManagerInterface $entityManager
*
* @return \Doctrine\ORM\Mapping\ClassMetadata
*/ */
private function getClassMetadata($entityName) private function getClassMetadata($entityName, EntityManagerInterface $entityManager)
{ {
try { try {
$meta = $this->entityManager->getClassMetadata($entityName); $meta = $entityManager->getClassMetadata($entityName);
} catch (\Doctrine\Common\Persistence\Mapping\MappingException $e) { } catch (\Doctrine\Common\Persistence\Mapping\MappingException $e) {
$mappedEntities = $this->getMappedEntities(); $mappedEntities = $this->getMappedEntities($entityManager);
$matches = array_filter($mappedEntities, function ($mappedEntity) use ($entityName) { $matches = array_filter($mappedEntities, function ($mappedEntity) use ($entityName) {
if (preg_match('{' . preg_quote($entityName) . '}', $mappedEntity)) { if (preg_match('{' . preg_quote($entityName) . '}', $mappedEntity)) {
return true; return true;
@ -190,7 +197,7 @@ EOT
} }
if (1 === count($matches)) { if (1 === count($matches)) {
$meta = $this->entityManager->getClassMetadata(current($matches)); $meta = $entityManager->getClassMetadata(current($matches));
} else { } else {
throw new \InvalidArgumentException(sprintf( throw new \InvalidArgumentException(sprintf(
'Entity name "%s" is ambigous, possible matches: "%s"', 'Entity name "%s" is ambigous, possible matches: "%s"',
@ -206,6 +213,8 @@ EOT
* Format the given value for console output * Format the given value for console output
* *
* @param mixed $value * @param mixed $value
*
* @return string
*/ */
private function formatValue($value) private function formatValue($value)
{ {
@ -245,11 +254,10 @@ EOT
} }
/** /**
* Add the given label and value to the two column table * Add the given label and value to the two column table output
* output
* *
* @param string $label Label for the value * @param string $label Label for the value
* @param mixed $valueA Value to show * @param mixed $value A Value to show
*/ */
private function formatField($label, $value) private function formatField($label, $value)
{ {
@ -284,6 +292,7 @@ EOT
private function formatEntityListeners($entityListeners) private function formatEntityListeners($entityListeners)
{ {
$entityListenerNames = array(); $entityListenerNames = array();
foreach ($entityListeners as $entityListener) { foreach ($entityListeners as $entityListener) {
$entityListenerNames[] = get_class($entityListener); $entityListenerNames[] = get_class($entityListener);
} }