Skip to content

Commit

Permalink
doctrine#1120 - avoiding storing the Doctrine\ORM\EntityManager in …
Browse files Browse the repository at this point in the history
…the command itself
  • Loading branch information
Ocramius committed Oct 19, 2014
1 parent e9d7c23 commit cf078d8
Showing 1 changed file with 31 additions and 22 deletions.
53 changes: 31 additions & 22 deletions lib/Doctrine/ORM/Tools/Console/Command/MappingDescribeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@

namespace Doctrine\ORM\Tools\Console\Command;

use Doctrine\ORM\Mapping\MappingException;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Helper\TableHelper;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

/**
* Show information about mapped entities.
Expand Down Expand Up @@ -79,19 +79,20 @@ protected function execute(InputInterface $input, OutputInterface $output)
$this->output = $output;
$this->entityManager = $entityManager;

$this->displayEntity($entityName);
$this->displayEntity($entityName, $entityManager);

return 0;
}

/**
* 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('Root entity name', $meta->rootEntityName);
Expand Down Expand Up @@ -144,15 +145,18 @@ private function displayEntity($entityName)
/**
* 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()
->getMetadataDriverImpl()
->getAllClassNames();
$entityClassNames = $entityManager
->getConfiguration()
->getMetadataDriverImpl()
->getAllClassNames();

if (!$entityClassNames) {
if ( ! $entityClassNames) {
throw new \InvalidArgumentException(
'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.'
Expand All @@ -166,14 +170,17 @@ private function getMappedEntities()
* Return the class metadata for the given entity
* 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 {
$meta = $this->entityManager->getClassMetadata($entityName);
$meta = $entityManager->getClassMetadata($entityName);
} catch (\Doctrine\Common\Persistence\Mapping\MappingException $e) {
$mappedEntities = $this->getMappedEntities();
$mappedEntities = $this->getMappedEntities($entityManager);
$matches = array_filter($mappedEntities, function ($mappedEntity) use ($entityName) {
if (preg_match('{' . preg_quote($entityName) . '}', $mappedEntity)) {
return true;
Expand All @@ -190,7 +197,7 @@ private function getClassMetadata($entityName)
}

if (1 === count($matches)) {
$meta = $this->entityManager->getClassMetadata(current($matches));
$meta = $entityManager->getClassMetadata(current($matches));
} else {
throw new \InvalidArgumentException(sprintf(
'Entity name "%s" is ambigous, possible matches: "%s"',
Expand All @@ -206,6 +213,8 @@ private function getClassMetadata($entityName)
* Format the given value for console output
*
* @param mixed $value
*
* @return string
*/
private function formatValue($value)
{
Expand Down Expand Up @@ -245,11 +254,10 @@ private function formatValue($value)
}

/**
* Add the given label and value to the two column table
* output
* Add the given label and value to the two column table output
*
* @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)
{
Expand Down Expand Up @@ -284,6 +292,7 @@ private function formatAssociationMappings($associationMappings)
private function formatEntityListeners($entityListeners)
{
$entityListenerNames = array();

foreach ($entityListeners as $entityListener) {
$entityListenerNames[] = get_class($entityListener);
}
Expand Down

0 comments on commit cf078d8

Please sign in to comment.