|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <[email protected]> |
| 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\TwigComponent\Command; |
| 13 | + |
| 14 | +use Symfony\Component\Console\Attribute\AsCommand; |
| 15 | +use Symfony\Component\Console\Command\Command; |
| 16 | +use Symfony\Component\Console\Helper\Table; |
| 17 | +use Symfony\Component\Console\Input\InputArgument; |
| 18 | +use Symfony\Component\Console\Input\InputInterface; |
| 19 | +use Symfony\Component\Console\Input\InputOption; |
| 20 | +use Symfony\Component\Console\Output\OutputInterface; |
| 21 | +use Symfony\Component\Console\Style\SymfonyStyle; |
| 22 | +use Symfony\Component\Finder\Finder; |
| 23 | +use Symfony\UX\LiveComponent\Attribute\LiveAction; |
| 24 | +use Symfony\UX\LiveComponent\Attribute\LiveProp; |
| 25 | +use Symfony\UX\TwigComponent\Attribute\PostMount; |
| 26 | +use Symfony\UX\TwigComponent\Attribute\PreMount; |
| 27 | +use Symfony\UX\TwigComponent\ComponentFactory; |
| 28 | +use Symfony\UX\TwigComponent\ComponentMetadata; |
| 29 | +use Symfony\UX\TwigComponent\Twig\PropsNode; |
| 30 | +use Twig\Environment; |
| 31 | + |
| 32 | +#[AsCommand(name: 'debug:twig-component', description: 'Display current components and them usages for an application')] |
| 33 | +class ComponentDebugCommand extends Command |
| 34 | +{ |
| 35 | + public function __construct(private string $twigTemplatesPath, private ComponentFactory $componentFactory, private Environment $twigEnvironment, private iterable $components) |
| 36 | + { |
| 37 | + parent::__construct(); |
| 38 | + } |
| 39 | + |
| 40 | + protected function configure(): void |
| 41 | + { |
| 42 | + $this |
| 43 | + ->setDefinition([ |
| 44 | + new InputArgument('name', InputArgument::OPTIONAL, 'A component name'), |
| 45 | + new InputOption('dir', null, InputOption::VALUE_REQUIRED, 'Show all components with a specific directory in templates', 'components'), |
| 46 | + ]) |
| 47 | + ->setHelp(<<<'EOF' |
| 48 | + The <info>%command.name%</info> display all components in your application: |
| 49 | +
|
| 50 | + <info>php %command.full_name%</info> |
| 51 | +
|
| 52 | + Find all components within a specific directory in templates by specifying the directory name with the <info>--dir</info> option: |
| 53 | +
|
| 54 | + <info>php %command.full_name% --dir=bar/foo</info> |
| 55 | + |
| 56 | + EOF |
| 57 | + ) |
| 58 | + ; |
| 59 | + } |
| 60 | + |
| 61 | + protected function execute(InputInterface $input, OutputInterface $output): int |
| 62 | + { |
| 63 | + $io = new SymfonyStyle($input, $output); |
| 64 | + $name = $input->getArgument('name'); |
| 65 | + $componentsDir = $input->getOption('dir'); |
| 66 | + |
| 67 | + if (null !== $name) { |
| 68 | + try { |
| 69 | + $metadata = $this->componentFactory->metadataFor($name); |
| 70 | + } catch (\Exception $e) { |
| 71 | + $io->error($e->getMessage()); |
| 72 | + |
| 73 | + return Command::FAILURE; |
| 74 | + } |
| 75 | + |
| 76 | + $class = $metadata->get('class'); |
| 77 | + $live = null; |
| 78 | + $allProperties = []; |
| 79 | + |
| 80 | + if ($class) { |
| 81 | + if ($metadata->get('live')) { |
| 82 | + $live = 'X'; |
| 83 | + } |
| 84 | + |
| 85 | + $reflectionClass = new \ReflectionClass($class); |
| 86 | + $properties = $reflectionClass->getProperties(); |
| 87 | + $allLiveProperties = []; |
| 88 | + |
| 89 | + foreach ($properties as $property) { |
| 90 | + if ($property->isPublic()) { |
| 91 | + $visibility = $property->getType()?->getName(); |
| 92 | + $propertyName = $property->getName(); |
| 93 | + $value = $property->getDefaultValue(); |
| 94 | + $propertyAttributes = $property->getAttributes(LiveProp::class); |
| 95 | + |
| 96 | + $propertyDisplay = $visibility.' $'.$propertyName.(null !== $value ? ' = '.$value : ''); |
| 97 | + |
| 98 | + if (\count($propertyAttributes) > 0) { |
| 99 | + $allLiveProperties[] = $propertyDisplay; |
| 100 | + } else { |
| 101 | + $allProperties[] = $propertyDisplay; |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + $methods = $reflectionClass->getMethods(); |
| 107 | + $allEvents = []; |
| 108 | + $allActions = []; |
| 109 | + |
| 110 | + foreach ($methods as $method) { |
| 111 | + if ('mount' === $method->getName()) { |
| 112 | + $allEvents[] = 'Mount'; |
| 113 | + } |
| 114 | + |
| 115 | + foreach ($method->getAttributes() as $attribute) { |
| 116 | + if (PreMount::class === $attribute->getName()) { |
| 117 | + $allEvents[] = 'PreMount'; |
| 118 | + break; |
| 119 | + } |
| 120 | + |
| 121 | + if (PostMount::class === $attribute->getName()) { |
| 122 | + $allEvents[] = 'PostMount'; |
| 123 | + break; |
| 124 | + } |
| 125 | + |
| 126 | + if (LiveAction::class === $attribute->getName()) { |
| 127 | + $allActions[] = $method->getName(); |
| 128 | + break; |
| 129 | + } |
| 130 | + } |
| 131 | + } |
| 132 | + } else { |
| 133 | + $allProperties = $this->getPropertiesForAnonymousComponent($metadata); |
| 134 | + } |
| 135 | + |
| 136 | + $componentInfos = [ |
| 137 | + ['Component', $name], |
| 138 | + ['Live', $live], |
| 139 | + ['Class', $class ?? 'Anonymous component'], |
| 140 | + ['Template', $metadata->getTemplate()], |
| 141 | + ['Properties', \count($allProperties) > 0 ? implode("\n", $allProperties) : null], |
| 142 | + ]; |
| 143 | + |
| 144 | + if (isset($allLiveProperties) && \count($allLiveProperties) > 0) { |
| 145 | + $componentInfos[] = ['Live Properties', implode("\n", $allLiveProperties)]; |
| 146 | + } |
| 147 | + if (isset($allEvents) && \count($allEvents) > 0) { |
| 148 | + $componentInfos[] = ['Events', implode("\n", $allEvents)]; |
| 149 | + } |
| 150 | + if (isset($allActions) && \count($allActions) > 0) { |
| 151 | + $componentInfos[] = ['LiveAction Methods', implode("\n", $allActions)]; |
| 152 | + } |
| 153 | + |
| 154 | + $table = new Table($output); |
| 155 | + $table->setHeaders(['Property', 'Value'])->setRows($componentInfos); |
| 156 | + $table->render(); |
| 157 | + |
| 158 | + return Command::SUCCESS; |
| 159 | + } |
| 160 | + |
| 161 | + $finderTemplates = new Finder(); |
| 162 | + $finderTemplates->files()->in("{$this->twigTemplatesPath}/components"); |
| 163 | + |
| 164 | + $anonymousTemplatesComponents = []; |
| 165 | + foreach ($finderTemplates as $template) { |
| 166 | + $anonymousTemplatesComponents[] = $template->getRelativePathname(); |
| 167 | + } |
| 168 | + |
| 169 | + $componentsWithClass = []; |
| 170 | + foreach ($this->components as $class) { |
| 171 | + $reflectionClass = new \ReflectionClass($class); |
| 172 | + $attributes = $reflectionClass->getAttributes(); |
| 173 | + |
| 174 | + foreach ($attributes as $attribute) { |
| 175 | + $arguments = $attribute->getArguments(); |
| 176 | + |
| 177 | + $name = $arguments['name'] ?? $arguments[0] ?? null; |
| 178 | + $template = $arguments['template'] ?? $arguments[1] ?? null; |
| 179 | + |
| 180 | + if (null !== $template || null !== $name) { |
| 181 | + if (null !== $template && null !== $name) { |
| 182 | + $templateFile = str_replace('components/', '', $template); |
| 183 | + $metadata = $this->componentFactory->metadataFor($name); |
| 184 | + } elseif (null !== $name) { |
| 185 | + $templateFile = str_replace(':', '/', "{$name}.html.twig"); |
| 186 | + $metadata = $this->componentFactory->metadataFor($name); |
| 187 | + } else { |
| 188 | + $templateFile = str_replace('components/', '', $template); |
| 189 | + $metadata = $this->componentFactory->metadataFor(str_replace('.html.twig', '', $templateFile)); |
| 190 | + } |
| 191 | + } else { |
| 192 | + $templateFile = "{$reflectionClass->getShortName()}.html.twig"; |
| 193 | + $metadata = $this->componentFactory->metadataFor($reflectionClass->getShortName()); |
| 194 | + } |
| 195 | + |
| 196 | + $componentsWithClass[] = [ |
| 197 | + 'name' => $metadata->getName(), |
| 198 | + 'live' => null !== $metadata->get('live') ? 'X' : null, |
| 199 | + ]; |
| 200 | + |
| 201 | + if (($key = array_search($templateFile, $anonymousTemplatesComponents)) !== false) { |
| 202 | + unset($anonymousTemplatesComponents[$key]); |
| 203 | + } |
| 204 | + } |
| 205 | + } |
| 206 | + |
| 207 | + $anonymousComponents = array_map(fn ($template): array => [ |
| 208 | + 'name' => str_replace('/', ':', str_replace('.html.twig', '', $template)), |
| 209 | + 'live' => null, |
| 210 | + ], $anonymousTemplatesComponents); |
| 211 | + |
| 212 | + $allComponents = array_merge($componentsWithClass, $anonymousComponents); |
| 213 | + $dataToRender = []; |
| 214 | + foreach ($allComponents as $component) { |
| 215 | + $metadata = $this->componentFactory->metadataFor($component['name']); |
| 216 | + |
| 217 | + if (str_contains($metadata->getTemplate(), $componentsDir)) { |
| 218 | + $dataToRender[] = [ |
| 219 | + $metadata->getName(), |
| 220 | + $metadata->get('class') ?? 'Anonymous component', |
| 221 | + $metadata->getTemplate(), |
| 222 | + $component['live'], |
| 223 | + ]; |
| 224 | + } |
| 225 | + } |
| 226 | + |
| 227 | + $table = new Table($output); |
| 228 | + $table->setHeaders(['Component', 'Class', 'Template', 'Live'])->setRows($dataToRender); |
| 229 | + $table->render(); |
| 230 | + |
| 231 | + return Command::SUCCESS; |
| 232 | + } |
| 233 | + |
| 234 | + private function getPropertiesForAnonymousComponent(ComponentMetadata $metadata): array |
| 235 | + { |
| 236 | + $allProperties = []; |
| 237 | + |
| 238 | + $source = $this->twigEnvironment->load($metadata->getTemplate())->getSourceContext(); |
| 239 | + $tokenStream = $this->twigEnvironment->tokenize($source); |
| 240 | + $bodyNode = $this->twigEnvironment->parse($tokenStream)->getNode('body')->getNode(0); |
| 241 | + |
| 242 | + $propsNode = []; |
| 243 | + |
| 244 | + foreach ($bodyNode as $node) { |
| 245 | + if ($node instanceof PropsNode) { |
| 246 | + $propsNode = $node; |
| 247 | + break; |
| 248 | + } |
| 249 | + } |
| 250 | + |
| 251 | + if (\count($propsNode) > 0) { |
| 252 | + $allVariables = $propsNode->getAttribute('names'); |
| 253 | + |
| 254 | + foreach ($allVariables as $variable) { |
| 255 | + if ($propsNode->hasNode($variable)) { |
| 256 | + $value = $propsNode->getNode($variable)->getAttribute('value'); |
| 257 | + |
| 258 | + if (\is_bool($value)) { |
| 259 | + $value = $value ? 'true' : 'false'; |
| 260 | + } |
| 261 | + |
| 262 | + $property = $variable.' = '.$value; |
| 263 | + } else { |
| 264 | + $property = $variable; |
| 265 | + } |
| 266 | + |
| 267 | + $allProperties[] = $property; |
| 268 | + } |
| 269 | + } |
| 270 | + |
| 271 | + return $allProperties; |
| 272 | + } |
| 273 | +} |
0 commit comments