From 105ae6e303f1613f4ef5be67aca7b68761903c53 Mon Sep 17 00:00:00 2001 From: Steven Renaux Date: Mon, 4 Sep 2023 11:21:33 +0200 Subject: [PATCH] Add dissociation for LiveProp and add component type --- .../src/Command/ComponentDebugCommand.php | 92 +++++++++++++------ 1 file changed, 64 insertions(+), 28 deletions(-) diff --git a/src/TwigComponent/src/Command/ComponentDebugCommand.php b/src/TwigComponent/src/Command/ComponentDebugCommand.php index 8350c1311bf..2d0bf2bb017 100644 --- a/src/TwigComponent/src/Command/ComponentDebugCommand.php +++ b/src/TwigComponent/src/Command/ComponentDebugCommand.php @@ -19,6 +19,9 @@ use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Style\SymfonyStyle; use Symfony\Component\Finder\Finder; +use Symfony\UX\LiveComponent\Attribute\AsLiveComponent; +use Symfony\UX\LiveComponent\Attribute\LiveProp; +use Symfony\UX\TwigComponent\Attribute\AsTwigComponent; use Symfony\UX\TwigComponent\ComponentFactory; use Symfony\UX\TwigComponent\Twig\PropsNode; use Twig\Environment; @@ -62,24 +65,41 @@ protected function execute(InputInterface $input, OutputInterface $output): int } $class = $metadata->get('class'); + $type = null; $allProperties = []; if ($class) { $propertyLabel = 'Properties (type / name / default value if exist)'; + $type = 'AsTwigComponent'; + + if ($metadata->get('live')) { + $type = 'AsLiveComponent'; + } $reflectionClass = new \ReflectionClass($class); $properties = $reflectionClass->getProperties(); + $allLiveProperties = []; foreach ($properties as $property) { if ($property->isPublic()) { $visibility = $property->getType()?->getName(); $propertyName = $property->getName(); $value = $property->getDefaultValue(); + $propertyAttributes = $property->getAttributes(LiveProp::class); - $allProperties = [ - ...$allProperties, - $visibility.' $'.$propertyName.(null !== $value ? ' = '.$value : ''), - ]; + $propertyDisplay = $visibility.' $'.$propertyName.(null !== $value ? ' = '.$value : ''); + + if (\count($propertyAttributes) > 0) { + $allLiveProperties = [ + ...$allLiveProperties, + $propertyDisplay, + ]; + } else { + $allProperties = [ + ...$allProperties, + $propertyDisplay, + ]; + } } } } else { @@ -124,11 +144,16 @@ protected function execute(InputInterface $input, OutputInterface $output): int $componentInfos = [ ['Component', $name], + ['Type', $type], ['Class', $class ?? 'Anonymous component'], ['Template', $metadata->getTemplate()], [$propertyLabel, \count($allProperties) > 0 ? implode("\n", $allProperties) : null], ]; + if (isset($allLiveProperties) && \count($allLiveProperties) > 0) { + $componentInfos[] = ['Live Properties', implode("\n", $allLiveProperties)]; + } + $table = new Table($output); $table->setHeaders(['Property', 'Value']); @@ -155,53 +180,64 @@ protected function execute(InputInterface $input, OutputInterface $output): int $attributes = $reflectionClass->getAttributes(); foreach ($attributes as $attribute) { - $arguments = $attribute->getArguments(); - - $name = $arguments['name'] ?? $arguments[0] ?? null; - $template = $arguments['template'] ?? $arguments[1] ?? null; - - if (null !== $template || null !== $name) { - if (null !== $template && null !== $name) { - $templateFile = str_replace('components/', '', $template); - $metadata = $this->componentFactory->metadataFor($name); - } elseif (null !== $name) { - $templateFile = str_replace(':', '/', "{$name}.html.twig"); - $metadata = $this->componentFactory->metadataFor($name); + $attributeName = $attribute->getName(); + + if (\in_array($attributeName, [AsTwigComponent::class, AsLiveComponent::class])) { + $arguments = $attribute->getArguments(); + + $name = $arguments['name'] ?? $arguments[0] ?? null; + $template = $arguments['template'] ?? $arguments[1] ?? null; + + if (null !== $template || null !== $name) { + if (null !== $template && null !== $name) { + $templateFile = str_replace('components/', '', $template); + $metadata = $this->componentFactory->metadataFor($name); + } elseif (null !== $name) { + $templateFile = str_replace(':', '/', "{$name}.html.twig"); + $metadata = $this->componentFactory->metadataFor($name); + } else { + $templateFile = str_replace('components/', '', $template); + $metadata = $this->componentFactory->metadataFor(str_replace('.html.twig', '', $templateFile)); + } } else { - $templateFile = str_replace('components/', '', $template); - $metadata = $this->componentFactory->metadataFor(str_replace('.html.twig', '', $templateFile)); + $templateFile = "{$reflectionClass->getShortName()}.html.twig"; + $metadata = $this->componentFactory->metadataFor($reflectionClass->getShortName()); } - } else { - $templateFile = "{$reflectionClass->getShortName()}.html.twig"; - $metadata = $this->componentFactory->metadataFor($reflectionClass->getShortName()); - } - $componentsWithClass[] = $metadata->getName(); + $componentsWithClass[] = [ + 'name' => $metadata->getName(), + 'type' => substr($attributeName, strrpos($attributeName, '\\') + 1), + ]; - if (($key = array_search($templateFile, $anonymousTemplatesComponents)) !== false) { - unset($anonymousTemplatesComponents[$key]); + if (($key = array_search($templateFile, $anonymousTemplatesComponents)) !== false) { + unset($anonymousTemplatesComponents[$key]); + } } } } - $anonymousComponents = array_map(fn ($template): string => str_replace('/', ':', str_replace('.html.twig', '', $template)), $anonymousTemplatesComponents); + $anonymousComponents = array_map(fn ($template): array => [ + 'name' => str_replace('/', ':', str_replace('.html.twig', '', $template)), + 'type' => null, + ], $anonymousTemplatesComponents); $allComponents = array_merge($componentsWithClass, $anonymousComponents); $dataToRender = []; foreach ($allComponents as $component) { - $metadata = $this->componentFactory->metadataFor($component); + $metadata = $this->componentFactory->metadataFor($component['name']); $dataToRender = [...$dataToRender, [ $metadata->getName(), $metadata->get('class') ?? 'Anonymous component', $metadata->getTemplate(), + $component['type'], ], ]; } $table = new Table($output); - $table->setHeaders(['Component', 'Class', 'Template']); + $table->setHeaders(['Component', 'Class', 'Template', 'Type']); foreach ($dataToRender as $data) { $table->addRow($data);