|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Doctrine\ORM\Tools\Console\Command\Debug; |
| 6 | + |
| 7 | +use Doctrine\ORM\Mapping\ClassMetadata; |
| 8 | +use Symfony\Component\Console\Completion\CompletionInput; |
| 9 | +use Symfony\Component\Console\Completion\CompletionSuggestions; |
| 10 | +use Symfony\Component\Console\Helper\TableSeparator; |
| 11 | +use Symfony\Component\Console\Input\InputArgument; |
| 12 | +use Symfony\Component\Console\Input\InputInterface; |
| 13 | +use Symfony\Component\Console\Output\OutputInterface; |
| 14 | +use Symfony\Component\Console\Style\SymfonyStyle; |
| 15 | + |
| 16 | +use function array_keys; |
| 17 | +use function array_merge; |
| 18 | +use function array_unique; |
| 19 | +use function array_values; |
| 20 | +use function assert; |
| 21 | +use function class_exists; |
| 22 | +use function ksort; |
| 23 | +use function ltrim; |
| 24 | +use function sort; |
| 25 | +use function sprintf; |
| 26 | + |
| 27 | +final class DebugEntityListenersDoctrineCommand extends AbstractCommand |
| 28 | +{ |
| 29 | + protected function configure(): void |
| 30 | + { |
| 31 | + $this |
| 32 | + ->setName('orm:debug:entity-listeners') |
| 33 | + ->setDescription('Lists entity listeners for a given entity') |
| 34 | + ->addArgument('entity', InputArgument::OPTIONAL, 'The fully-qualified entity class name') |
| 35 | + ->addArgument('event', InputArgument::OPTIONAL, 'The event name to filter by (e.g. postPersist)') |
| 36 | + ->setHelp(<<<'EOT' |
| 37 | +The <info>%command.name%</info> command lists all entity listeners for a given entity: |
| 38 | +
|
| 39 | + <info>php %command.full_name% 'App\Entity\User'</info> |
| 40 | +
|
| 41 | +To show only listeners for a specific event, pass the event name: |
| 42 | +
|
| 43 | + <info>php %command.full_name% 'App\Entity\User' postPersist</info> |
| 44 | +EOT); |
| 45 | + } |
| 46 | + |
| 47 | + protected function execute(InputInterface $input, OutputInterface $output): int |
| 48 | + { |
| 49 | + $io = new SymfonyStyle($input, $output); |
| 50 | + |
| 51 | + /** @var class-string|null $entityName */ |
| 52 | + $entityName = $input->getArgument('entity'); |
| 53 | + |
| 54 | + if ($entityName === null) { |
| 55 | + $choices = $this->listAllEntities(); |
| 56 | + |
| 57 | + if ($choices === []) { |
| 58 | + $io->error('No entities are configured.'); |
| 59 | + |
| 60 | + return self::FAILURE; |
| 61 | + } |
| 62 | + |
| 63 | + /** @var class-string $entityName */ |
| 64 | + $entityName = $io->choice('Which entity do you want to list listeners for?', $choices); |
| 65 | + } |
| 66 | + |
| 67 | + $entityName = ltrim($entityName, '\\'); |
| 68 | + $entityManager = $this->getManagerRegistry()->getManagerForClass($entityName); |
| 69 | + |
| 70 | + if ($entityManager === null) { |
| 71 | + $io->error(sprintf('No entity manager found for class "%s".', $entityName)); |
| 72 | + |
| 73 | + return self::FAILURE; |
| 74 | + } |
| 75 | + |
| 76 | + $classMetadata = $entityManager->getClassMetadata($entityName); |
| 77 | + assert($classMetadata instanceof ClassMetadata); |
| 78 | + |
| 79 | + $eventName = $input->getArgument('event'); |
| 80 | + |
| 81 | + if ($eventName === null) { |
| 82 | + $allListeners = $classMetadata->entityListeners; |
| 83 | + if (! $allListeners) { |
| 84 | + $io->info(sprintf('No listeners are configured for the "%s" entity.', $entityName)); |
| 85 | + |
| 86 | + return self::SUCCESS; |
| 87 | + } |
| 88 | + |
| 89 | + ksort($allListeners); |
| 90 | + } else { |
| 91 | + if (! isset($classMetadata->entityListeners[$eventName])) { |
| 92 | + $io->info(sprintf('No listeners are configured for the "%s" event.', $eventName)); |
| 93 | + |
| 94 | + return self::SUCCESS; |
| 95 | + } |
| 96 | + |
| 97 | + $allListeners = [$eventName => $classMetadata->entityListeners[$eventName]]; |
| 98 | + } |
| 99 | + |
| 100 | + $io->title(sprintf('Entity listeners for <info>%s</info>', $entityName)); |
| 101 | + |
| 102 | + $rows = []; |
| 103 | + foreach ($allListeners as $event => $listeners) { |
| 104 | + if ($rows) { |
| 105 | + $rows[] = new TableSeparator(); |
| 106 | + } |
| 107 | + |
| 108 | + foreach ($listeners as $order => $listener) { |
| 109 | + $rows[] = [$order === 0 ? $event : '', sprintf('#%d', ++$order), sprintf('%s::%s()', $listener['class'], $listener['method'])]; |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + $io->table(['Event', 'Order', 'Listener'], $rows); |
| 114 | + |
| 115 | + return self::SUCCESS; |
| 116 | + } |
| 117 | + |
| 118 | + public function complete(CompletionInput $input, CompletionSuggestions $suggestions): void |
| 119 | + { |
| 120 | + if ($input->mustSuggestArgumentValuesFor('entity')) { |
| 121 | + $suggestions->suggestValues($this->listAllEntities()); |
| 122 | + |
| 123 | + return; |
| 124 | + } |
| 125 | + |
| 126 | + if ($input->mustSuggestArgumentValuesFor('event')) { |
| 127 | + $entityName = ltrim($input->getArgument('entity'), '\\'); |
| 128 | + |
| 129 | + if (! class_exists($entityName)) { |
| 130 | + return; |
| 131 | + } |
| 132 | + |
| 133 | + $entityManager = $this->getManagerRegistry()->getManagerForClass($entityName); |
| 134 | + |
| 135 | + if ($entityManager === null) { |
| 136 | + return; |
| 137 | + } |
| 138 | + |
| 139 | + $classMetadata = $entityManager->getClassMetadata($entityName); |
| 140 | + assert($classMetadata instanceof ClassMetadata); |
| 141 | + |
| 142 | + $suggestions->suggestValues(array_keys($classMetadata->entityListeners)); |
| 143 | + |
| 144 | + return; |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | + /** @return list<class-string> */ |
| 149 | + private function listAllEntities(): array |
| 150 | + { |
| 151 | + $entities = []; |
| 152 | + foreach (array_keys($this->getManagerRegistry()->getManagerNames()) as $managerName) { |
| 153 | + $entities[] = $this->getEntityManager($managerName)->getConfiguration()->getMetadataDriverImpl()->getAllClassNames(); |
| 154 | + } |
| 155 | + |
| 156 | + $entities = array_values(array_unique(array_merge(...$entities))); |
| 157 | + |
| 158 | + sort($entities); |
| 159 | + |
| 160 | + return $entities; |
| 161 | + } |
| 162 | +} |
0 commit comments