|
| 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\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\Output\OutputInterface; |
| 20 | +use Symfony\Component\Console\Style\SymfonyStyle; |
| 21 | +use Symfony\Component\Finder\Finder; |
| 22 | +use Symfony\UX\TwigComponent\ComponentFactory; |
| 23 | +use Symfony\UX\TwigComponent\Twig\PropsNode; |
| 24 | +use Twig\Environment; |
| 25 | + |
| 26 | +#[AsCommand(name: 'debug:component', description: 'Display current components and them usages for an application')] |
| 27 | +class ComponentDebugCommand extends Command |
| 28 | +{ |
| 29 | + public function __construct(private string $twigTemplatesPath, private ComponentFactory $componentFactory, private Environment $twigEnvironment, private iterable $components) |
| 30 | + { |
| 31 | + parent::__construct(); |
| 32 | + } |
| 33 | + |
| 34 | + protected function configure(): void |
| 35 | + { |
| 36 | + $this |
| 37 | + ->setDefinition([ |
| 38 | + new InputArgument('name', InputArgument::OPTIONAL, 'A component name'), |
| 39 | + ]) |
| 40 | + ->setHelp(<<<'EOF' |
| 41 | + The <info>%command.name%</info> display current components and them usages for an application: |
| 42 | +
|
| 43 | + <info>php %command.full_name%</info> |
| 44 | + |
| 45 | + EOF |
| 46 | + ) |
| 47 | + ; |
| 48 | + } |
| 49 | + |
| 50 | + protected function execute(InputInterface $input, OutputInterface $output): int |
| 51 | + { |
| 52 | + $io = new SymfonyStyle($input, $output); |
| 53 | + $name = $input->getArgument('name'); |
| 54 | + |
| 55 | + if ($name) { |
| 56 | + try { |
| 57 | + $metadata = $this->componentFactory->metadataFor($name); |
| 58 | + } catch (\Exception $e) { |
| 59 | + $io->error($e->getMessage()); |
| 60 | + |
| 61 | + return Command::FAILURE; |
| 62 | + } |
| 63 | + |
| 64 | + $class = $metadata->get('class'); |
| 65 | + $allProperties = []; |
| 66 | + |
| 67 | + if ($class) { |
| 68 | + $propertyLabel = 'Properties (type / name / default value if exist)'; |
| 69 | + |
| 70 | + $reflectionClass = new \ReflectionClass($class); |
| 71 | + $properties = $reflectionClass->getProperties(); |
| 72 | + |
| 73 | + foreach ($properties as $property) { |
| 74 | + if ($property->isPublic()) { |
| 75 | + $visibility = $property->getType()?->getName(); |
| 76 | + $propertyName = $property->getName(); |
| 77 | + $value = $property->getDefaultValue(); |
| 78 | + |
| 79 | + $allProperties = [ |
| 80 | + ...$allProperties, |
| 81 | + $visibility.' $'.$propertyName.(null !== $value ? ' = '.$value : ''), |
| 82 | + ]; |
| 83 | + } |
| 84 | + } |
| 85 | + } else { |
| 86 | + $propertyLabel = 'Properties (name / default value if exist)'; |
| 87 | + |
| 88 | + $source = $this->twigEnvironment->load($metadata->getTemplate())->getSourceContext(); |
| 89 | + $tokenStream = $this->twigEnvironment->tokenize($source); |
| 90 | + $bodyNode = $this->twigEnvironment->parse($tokenStream)->getNode('body')->getNode(0); |
| 91 | + |
| 92 | + $propsNode = []; |
| 93 | + |
| 94 | + foreach ($bodyNode as $node) { |
| 95 | + if ($node instanceof PropsNode) { |
| 96 | + $propsNode = $node; |
| 97 | + break; |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + if (\count($propsNode) > 0) { |
| 102 | + $allVariables = $propsNode->getAttribute('names'); |
| 103 | + |
| 104 | + foreach ($allVariables as $variable) { |
| 105 | + if ($propsNode->hasNode($variable)) { |
| 106 | + $value = $propsNode->getNode($variable)->getAttribute('value'); |
| 107 | + |
| 108 | + if (\is_bool($value)) { |
| 109 | + $value = $value ? 'true' : 'false'; |
| 110 | + } |
| 111 | + |
| 112 | + $property = $variable.' = '.$value; |
| 113 | + } else { |
| 114 | + $property = $variable; |
| 115 | + } |
| 116 | + |
| 117 | + $allProperties = [ |
| 118 | + ...$allProperties, |
| 119 | + $property, |
| 120 | + ]; |
| 121 | + } |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + $componentInfos = [ |
| 126 | + ['Component', $name], |
| 127 | + ['Class', $class ?? 'Anonymous component'], |
| 128 | + ['Template', $metadata->getTemplate()], |
| 129 | + [$propertyLabel, \count($allProperties) > 0 ? implode("\n", $allProperties) : null], |
| 130 | + ]; |
| 131 | + |
| 132 | + $table = new Table($output); |
| 133 | + $table->setHeaders(['Property', 'Value']); |
| 134 | + |
| 135 | + foreach ($componentInfos as $info) { |
| 136 | + $table->addRow($info); |
| 137 | + } |
| 138 | + |
| 139 | + $table->render(); |
| 140 | + |
| 141 | + return Command::SUCCESS; |
| 142 | + } |
| 143 | + |
| 144 | + $finderTemplates = new Finder(); |
| 145 | + $finderTemplates->files()->in("{$this->twigTemplatesPath}/components"); |
| 146 | + |
| 147 | + $anonymousTemplatesComponents = []; |
| 148 | + foreach ($finderTemplates as $template) { |
| 149 | + $anonymousTemplatesComponents[] = $template->getRelativePathname(); |
| 150 | + } |
| 151 | + |
| 152 | + $componentsWithClass = []; |
| 153 | + foreach ($this->components as $class) { |
| 154 | + $reflectionClass = new \ReflectionClass($class); |
| 155 | + $attributes = $reflectionClass->getAttributes(); |
| 156 | + |
| 157 | + foreach ($attributes as $attribute) { |
| 158 | + $arguments = $attribute->getArguments(); |
| 159 | + |
| 160 | + $name = $arguments['name'] ?? $arguments[0] ?? null; |
| 161 | + $template = $arguments['template'] ?? $arguments[1] ?? null; |
| 162 | + |
| 163 | + if (null !== $template || null !== $name) { |
| 164 | + if (null !== $template && null !== $name) { |
| 165 | + $templateFile = str_replace('components/', '', $template); |
| 166 | + $metadata = $this->componentFactory->metadataFor($name); |
| 167 | + } elseif (null !== $name) { |
| 168 | + $templateFile = str_replace(':', '/', "{$name}.html.twig"); |
| 169 | + $metadata = $this->componentFactory->metadataFor($name); |
| 170 | + } else { |
| 171 | + $templateFile = str_replace('components/', '', $template); |
| 172 | + $metadata = $this->componentFactory->metadataFor(str_replace('.html.twig', '', $templateFile)); |
| 173 | + } |
| 174 | + } else { |
| 175 | + $templateFile = "{$reflectionClass->getShortName()}.html.twig"; |
| 176 | + $metadata = $this->componentFactory->metadataFor($reflectionClass->getShortName()); |
| 177 | + } |
| 178 | + |
| 179 | + $componentsWithClass[] = $metadata->getName(); |
| 180 | + |
| 181 | + if (($key = array_search($templateFile, $anonymousTemplatesComponents)) !== false) { |
| 182 | + unset($anonymousTemplatesComponents[$key]); |
| 183 | + } |
| 184 | + } |
| 185 | + } |
| 186 | + |
| 187 | + $anonymousComponents = array_map(fn ($template): string => str_replace('/', ':', str_replace('.html.twig', '', $template)), $anonymousTemplatesComponents); |
| 188 | + |
| 189 | + $allComponents = array_merge($componentsWithClass, $anonymousComponents); |
| 190 | + $dataToRender = []; |
| 191 | + foreach ($allComponents as $component) { |
| 192 | + $metadata = $this->componentFactory->metadataFor($component); |
| 193 | + |
| 194 | + $dataToRender = [...$dataToRender, |
| 195 | + [ |
| 196 | + $metadata->getName(), |
| 197 | + $metadata->get('class') ?? 'Anonymous component', |
| 198 | + $metadata->getTemplate(), |
| 199 | + ], |
| 200 | + ]; |
| 201 | + } |
| 202 | + |
| 203 | + $table = new Table($output); |
| 204 | + $table->setHeaders(['Component', 'Class', 'Template']); |
| 205 | + |
| 206 | + foreach ($dataToRender as $data) { |
| 207 | + $table->addRow($data); |
| 208 | + } |
| 209 | + |
| 210 | + $table->render(); |
| 211 | + |
| 212 | + return Command::SUCCESS; |
| 213 | + } |
| 214 | +} |
0 commit comments