|
| 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\UX\LiveComponent\Command; |
| 13 | + |
| 14 | +use Symfony\Component\Console\Attribute\AsCommand; |
| 15 | +use Symfony\Component\Console\Command\Command; |
| 16 | +use Symfony\Component\Console\Input\InputArgument; |
| 17 | +use Symfony\Component\Console\Input\InputInterface; |
| 18 | +use Symfony\Component\Console\Input\InputOption; |
| 19 | +use Symfony\Component\Console\Output\OutputInterface; |
| 20 | +use Symfony\Component\Console\Style\SymfonyStyle; |
| 21 | +use Symfony\UX\LiveComponent\Attribute\AsLiveComponent; |
| 22 | +use Symfony\UX\LiveComponent\Attribute\LiveArg; |
| 23 | +use Symfony\UX\LiveComponent\Attribute\LiveProp; |
| 24 | + |
| 25 | +#[AsCommand(name: 'debug:live-component', description: 'Display live components and their usage for an application')] |
| 26 | +class LiveComponentDebugCommand extends Command |
| 27 | +{ |
| 28 | + public function __construct( |
| 29 | + protected readonly array $componentClassMap, |
| 30 | + ) { |
| 31 | + parent::__construct(); |
| 32 | + } |
| 33 | + |
| 34 | + protected function configure(): void |
| 35 | + { |
| 36 | + $this |
| 37 | + ->setDefinition([ |
| 38 | + new InputArgument( |
| 39 | + 'name', |
| 40 | + InputArgument::OPTIONAL, |
| 41 | + 'A LiveComponent name or part of the name' |
| 42 | + ), |
| 43 | + new InputOption( |
| 44 | + name: 'listening', |
| 45 | + mode: InputOption::VALUE_REQUIRED, |
| 46 | + description: 'Filter list to display only those listening to the given event' |
| 47 | + ), |
| 48 | + ]) |
| 49 | + ->setHelp( |
| 50 | + <<<'EOF' |
| 51 | + The <info>%command.name%</info> display all the live components in your application. |
| 52 | +
|
| 53 | + To list all live components: |
| 54 | +
|
| 55 | + <info>php %command.full_name%</info> |
| 56 | +
|
| 57 | + To get specific information about a component, specify its name (or a part of it): |
| 58 | +
|
| 59 | + <info>php %command.full_name% Alert</info> |
| 60 | + EOF |
| 61 | + ); |
| 62 | + } |
| 63 | + |
| 64 | + protected function execute(InputInterface $input, OutputInterface $output): int |
| 65 | + { |
| 66 | + $io = new SymfonyStyle($input, $output); |
| 67 | + $name = $input->getArgument('name'); |
| 68 | + |
| 69 | + if (\is_string($name)) { |
| 70 | + $componentName = $this->findComponentName($io, $name, $input->isInteractive()); |
| 71 | + if (null === $componentName) { |
| 72 | + $io->error(\sprintf('Unknown LiveComponent "%s".', $name)); |
| 73 | + |
| 74 | + return Command::FAILURE; |
| 75 | + } |
| 76 | + |
| 77 | + $this->displayComponentDetails($io, $componentName); |
| 78 | + |
| 79 | + return Command::SUCCESS; |
| 80 | + } |
| 81 | + |
| 82 | + $components = $this->listComponents($input->getOption('listening')); |
| 83 | + |
| 84 | + $this->displayComponentsTable($components, $io); |
| 85 | + |
| 86 | + return Command::SUCCESS; |
| 87 | + } |
| 88 | + |
| 89 | + private function findComponentName(SymfonyStyle $io, string $name, bool $interactive): ?string |
| 90 | + { |
| 91 | + $components = []; |
| 92 | + foreach ($this->componentClassMap as $component) { |
| 93 | + if ($name === $component['key']) { |
| 94 | + return $name; |
| 95 | + } |
| 96 | + if (str_contains($component['key'], $name)) { |
| 97 | + $components[$component['key']] = $component['key']; |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + if ($interactive && \count($components)) { |
| 102 | + return $io->choice('Select one of the following component to display its information', array_values($components), 0); |
| 103 | + } |
| 104 | + |
| 105 | + return null; |
| 106 | + } |
| 107 | + |
| 108 | + private function listComponents(?string $eventFilter = null): array |
| 109 | + { |
| 110 | + if (null === $eventFilter) { |
| 111 | + return $this->componentClassMap; |
| 112 | + } |
| 113 | + |
| 114 | + $filteredComponents = []; |
| 115 | + foreach ($this->componentClassMap as $name => $component) { |
| 116 | + foreach (AsLiveComponent::liveListeners($component['class']) as $listener) { |
| 117 | + if ($listener['event'] === $eventFilter) { |
| 118 | + $filteredComponents[$name] = $component; |
| 119 | + break; |
| 120 | + } |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + return $filteredComponents; |
| 125 | + } |
| 126 | + |
| 127 | + private function displayComponentDetails(SymfonyStyle $io, string $name): void |
| 128 | + { |
| 129 | + $component = $this->componentClassMap[$name]; |
| 130 | + |
| 131 | + $table = $io->createTable(); |
| 132 | + $table->setHeaderTitle('Component'); |
| 133 | + $table->setHeaders(['Property', 'Value']); |
| 134 | + $table->addRows([ |
| 135 | + ['Name', $component['key']], |
| 136 | + ['Class', $component['class']], |
| 137 | + ]); |
| 138 | + |
| 139 | + $table->addRows([ |
| 140 | + ['LiveProps', implode("\n", $this->getComponentLiveProps($component['class']))], |
| 141 | + ['LiveListeners', implode("\n", $this->getComponentLiveListeners($component['class']))], |
| 142 | + ]); |
| 143 | + |
| 144 | + $table->render(); |
| 145 | + } |
| 146 | + |
| 147 | + private function displayComponentsTable(array $components, SymfonyStyle $io): void |
| 148 | + { |
| 149 | + $table = $io->createTable(); |
| 150 | + $table->setStyle('default'); |
| 151 | + $table->setHeaderTitle('Components'); |
| 152 | + $table->setHeaders(['Name', 'Class']); |
| 153 | + foreach ($components as $component) { |
| 154 | + $table->addRow([ |
| 155 | + $component['key'], |
| 156 | + $component['class'] ?? '', |
| 157 | + ]); |
| 158 | + } |
| 159 | + $table->render(); |
| 160 | + } |
| 161 | + |
| 162 | + /** |
| 163 | + * @return array<string, string> |
| 164 | + */ |
| 165 | + private function getComponentLiveProps(string $class): array |
| 166 | + { |
| 167 | + $properties = []; |
| 168 | + $reflectionClass = new \ReflectionClass($class); |
| 169 | + foreach ($reflectionClass->getProperties() as $property) { |
| 170 | + if (!$property->isPublic()) { |
| 171 | + continue; |
| 172 | + } |
| 173 | + if (empty($property->getAttributes(LiveProp::class))) { |
| 174 | + continue; |
| 175 | + } |
| 176 | + |
| 177 | + $type = $this->displayType($property->getType()); |
| 178 | + $propertyName = '$'.$property->getName(); |
| 179 | + $defaultValueDisplay = $property->hasDefaultValue() ? |
| 180 | + $this->displayDefaultValue($property->getDefaultValue()) : |
| 181 | + ''; |
| 182 | + $arguments = $property->getAttributes(LiveProp::class)[0]->getArguments(); |
| 183 | + $argumentsDisplay = empty($arguments) ? |
| 184 | + '' : |
| 185 | + ' ('.implode(', ', array_map( |
| 186 | + static fn ($key, $value) => $key.': '.json_encode($value), |
| 187 | + array_keys($arguments), |
| 188 | + $arguments |
| 189 | + )).')'; |
| 190 | + |
| 191 | + $propertyDisplay = $type.$propertyName.$defaultValueDisplay.$argumentsDisplay; |
| 192 | + $properties[$property->name] = $propertyDisplay; |
| 193 | + } |
| 194 | + |
| 195 | + return $properties; |
| 196 | + } |
| 197 | + |
| 198 | + /** |
| 199 | + * @return array<string, string> |
| 200 | + */ |
| 201 | + private function getComponentLiveListeners(string $class): array |
| 202 | + { |
| 203 | + $events = []; |
| 204 | + foreach (AsLiveComponent::liveListeners($class) as $liveListener) { |
| 205 | + $name = $liveListener['event']; |
| 206 | + $methodName = $liveListener['action']; |
| 207 | + $method = new \ReflectionMethod($class, $methodName); |
| 208 | + $parameters = array_map( |
| 209 | + fn (\ReflectionParameter $parameter) => $this->displayType($parameter->getType()).'$'.$parameter->getName().$this->displayDefaultValue($parameter->isDefaultValueAvailable() ? $parameter->getDefaultValue() : null), |
| 210 | + array_filter( |
| 211 | + $method->getParameters(), |
| 212 | + static fn (\ReflectionParameter $parameter) => !empty($parameter->getAttributes(LiveArg::class)) |
| 213 | + ) |
| 214 | + ); |
| 215 | + $parametersDisplay = empty($parameters) ? |
| 216 | + '' : |
| 217 | + ' ('.implode(', ', $parameters).')'; |
| 218 | + |
| 219 | + $display = $name.' => '.$methodName.$parametersDisplay; |
| 220 | + $events[] = $display; |
| 221 | + } |
| 222 | + |
| 223 | + return $events; |
| 224 | + } |
| 225 | + |
| 226 | + private function displayType(?\ReflectionType $type): string |
| 227 | + { |
| 228 | + $display = (string) $type; |
| 229 | + if ($type instanceof \ReflectionNamedType) { |
| 230 | + $display = $type->getName(); |
| 231 | + if ($type->allowsNull() && 'mixed' !== $display) { |
| 232 | + $display = '?'.$display; |
| 233 | + } |
| 234 | + } |
| 235 | + if ('' !== $display) { |
| 236 | + $display .= ' '; |
| 237 | + } |
| 238 | + |
| 239 | + return $display; |
| 240 | + } |
| 241 | + |
| 242 | + private function displayDefaultValue(mixed $defaultValue): string |
| 243 | + { |
| 244 | + return (null !== $defaultValue) ? |
| 245 | + ' = '.json_encode($defaultValue) : |
| 246 | + ''; |
| 247 | + } |
| 248 | +} |
0 commit comments