Skip to content

Commit

Permalink
Add dissociation for LiveProp and add component type
Browse files Browse the repository at this point in the history
  • Loading branch information
StevenRenaux committed Sep 4, 2023
1 parent d67f8bd commit 105ae6e
Showing 1 changed file with 64 additions and 28 deletions.
92 changes: 64 additions & 28 deletions src/TwigComponent/src/Command/ComponentDebugCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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']);

Expand All @@ -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);
Expand Down

0 comments on commit 105ae6e

Please sign in to comment.