1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
|
<?php
/*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the MIT license. For more information, see
* <http://www.doctrine-project.org>.
*/
namespace Doctrine\ORM\Tools\Console\Command;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\Persistence\Mapping\MappingException;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use const JSON_PRETTY_PRINT;
use const JSON_UNESCAPED_SLASHES;
use const JSON_UNESCAPED_UNICODE;
use function json_encode;
/**
* Show information about mapped entities.
*
* @link www.doctrine-project.org
* @since 2.4
* @author Daniel Leech <daniel@dantleech.com>
*/
final class MappingDescribeCommand extends Command
{
/**
* {@inheritdoc}
*/
protected function configure()
{
$this->setName('orm:mapping:describe')
->addArgument('entityName', InputArgument::REQUIRED, 'Full or partial name of entity')
->setDescription('Display information about mapped objects')
->setHelp(<<<EOT
The %command.full_name% command describes the metadata for the given full or partial entity class name.
<info>%command.full_name%</info> My\Namespace\Entity\MyEntity
Or:
<info>%command.full_name%</info> MyEntity
EOT
);
}
/**
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$ui = new SymfonyStyle($input, $output);
/* @var $entityManager \Doctrine\ORM\EntityManagerInterface */
$entityManager = $this->getHelper('em')->getEntityManager();
$this->displayEntity($input->getArgument('entityName'), $entityManager, $ui);
return 0;
}
/**
* Display all the mapping information for a single Entity.
*
* @param string $entityName Full or partial entity class name
* @param EntityManagerInterface $entityManager
* @param SymfonyStyle $ui
*/
private function displayEntity($entityName, EntityManagerInterface $entityManager, SymfonyStyle $ui)
{
$metadata = $this->getClassMetadata($entityName, $entityManager);
$ui->table(
['Field', 'Value'],
array_merge(
[
$this->formatField('Name', $metadata->name),
$this->formatField('Root entity name', $metadata->rootEntityName),
$this->formatField('Custom generator definition', $metadata->customGeneratorDefinition),
$this->formatField('Custom repository class', $metadata->customRepositoryClassName),
$this->formatField('Mapped super class?', $metadata->isMappedSuperclass),
$this->formatField('Embedded class?', $metadata->isEmbeddedClass),
$this->formatField('Parent classes', $metadata->parentClasses),
$this->formatField('Sub classes', $metadata->subClasses),
$this->formatField('Embedded classes', $metadata->subClasses),
$this->formatField('Named queries', $metadata->namedQueries),
$this->formatField('Named native queries', $metadata->namedNativeQueries),
$this->formatField('SQL result set mappings', $metadata->sqlResultSetMappings),
$this->formatField('Identifier', $metadata->identifier),
$this->formatField('Inheritance type', $metadata->inheritanceType),
$this->formatField('Discriminator column', $metadata->discriminatorColumn),
$this->formatField('Discriminator value', $metadata->discriminatorValue),
$this->formatField('Discriminator map', $metadata->discriminatorMap),
$this->formatField('Generator type', $metadata->generatorType),
$this->formatField('Table', $metadata->table),
$this->formatField('Composite identifier?', $metadata->isIdentifierComposite),
$this->formatField('Foreign identifier?', $metadata->containsForeignIdentifier),
$this->formatField('Sequence generator definition', $metadata->sequenceGeneratorDefinition),
$this->formatField('Table generator definition', $metadata->tableGeneratorDefinition),
$this->formatField('Change tracking policy', $metadata->changeTrackingPolicy),
$this->formatField('Versioned?', $metadata->isVersioned),
$this->formatField('Version field', $metadata->versionField),
$this->formatField('Read only?', $metadata->isReadOnly),
$this->formatEntityListeners($metadata->entityListeners),
],
[$this->formatField('Association mappings:', '')],
$this->formatMappings($metadata->associationMappings),
[$this->formatField('Field mappings:', '')],
$this->formatMappings($metadata->fieldMappings)
)
);
}
/**
* Return all mapped entity class names
*
* @param EntityManagerInterface $entityManager
*
* @return string[]
*/
private function getMappedEntities(EntityManagerInterface $entityManager) : array
{
$entityClassNames = $entityManager->getConfiguration()
->getMetadataDriverImpl()
->getAllClassNames();
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.'
);
}
return $entityClassNames;
}
/**
* Return the class metadata for the given entity
* name
*
* @param string $entityName Full or partial entity name
* @param EntityManagerInterface $entityManager
*
* @return \Doctrine\ORM\Mapping\ClassMetadata
*/
private function getClassMetadata($entityName, EntityManagerInterface $entityManager)
{
try {
return $entityManager->getClassMetadata($entityName);
} catch (MappingException $e) {
}
$matches = array_filter(
$this->getMappedEntities($entityManager),
function ($mappedEntity) use ($entityName) {
return preg_match('{' . preg_quote($entityName) . '}', $mappedEntity);
}
);
if ( ! $matches) {
throw new \InvalidArgumentException(sprintf(
'Could not find any mapped Entity classes matching "%s"',
$entityName
));
}
if (count($matches) > 1) {
throw new \InvalidArgumentException(sprintf(
'Entity name "%s" is ambiguous, possible matches: "%s"',
$entityName, implode(', ', $matches)
));
}
return $entityManager->getClassMetadata(current($matches));
}
/**
* Format the given value for console output
*
* @param mixed $value
*
* @return string
*/
private function formatValue($value)
{
if ('' === $value) {
return '';
}
if (null === $value) {
return '<comment>Null</comment>';
}
if (is_bool($value)) {
return '<comment>' . ($value ? 'True' : 'False') . '</comment>';
}
if (empty($value)) {
return '<comment>Empty</comment>';
}
if (is_array($value)) {
return json_encode($value, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT);
}
if (is_object($value)) {
return sprintf('<%s>', get_class($value));
}
if (is_scalar($value)) {
return (string) $value;
}
throw new \InvalidArgumentException(sprintf('Do not know how to format value "%s"', print_r($value, true)));
}
/**
* Add the given label and value to the two column table output
*
* @param string $label Label for the value
* @param mixed $value A Value to show
*
* @return string[]
*
* @psalm-return array{0: string, 1: string}
*/
private function formatField($label, $value) : array
{
if (null === $value) {
$value = '<comment>None</comment>';
}
return [sprintf('<info>%s</info>', $label), $this->formatValue($value)];
}
/**
* Format the association mappings
*
* @param array $propertyMappings
*
* @return string[][]
*
* @psalm-return list<array{0: string, 1: string}>
*/
private function formatMappings(array $propertyMappings) : array
{
$output = [];
foreach ($propertyMappings as $propertyName => $mapping) {
$output[] = $this->formatField(sprintf(' %s', $propertyName), '');
foreach ($mapping as $field => $value) {
$output[] = $this->formatField(sprintf(' %s', $field), $this->formatValue($value));
}
}
return $output;
}
/**
* Format the entity listeners
*
* @param array $entityListeners
*
* @return string[]
*
* @psalm-return array{0: string, 1: string}
*/
private function formatEntityListeners(array $entityListeners) : array
{
return $this->formatField('Entity listeners', array_map('get_class', $entityListeners));
}
}
|