Skip to content

Commit 105ae6e

Browse files
committed
Add dissociation for LiveProp and add component type
1 parent d67f8bd commit 105ae6e

File tree

1 file changed

+64
-28
lines changed

1 file changed

+64
-28
lines changed

src/TwigComponent/src/Command/ComponentDebugCommand.php

Lines changed: 64 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919
use Symfony\Component\Console\Output\OutputInterface;
2020
use Symfony\Component\Console\Style\SymfonyStyle;
2121
use Symfony\Component\Finder\Finder;
22+
use Symfony\UX\LiveComponent\Attribute\AsLiveComponent;
23+
use Symfony\UX\LiveComponent\Attribute\LiveProp;
24+
use Symfony\UX\TwigComponent\Attribute\AsTwigComponent;
2225
use Symfony\UX\TwigComponent\ComponentFactory;
2326
use Symfony\UX\TwigComponent\Twig\PropsNode;
2427
use Twig\Environment;
@@ -62,24 +65,41 @@ protected function execute(InputInterface $input, OutputInterface $output): int
6265
}
6366

6467
$class = $metadata->get('class');
68+
$type = null;
6569
$allProperties = [];
6670

6771
if ($class) {
6872
$propertyLabel = 'Properties (type / name / default value if exist)';
73+
$type = 'AsTwigComponent';
74+
75+
if ($metadata->get('live')) {
76+
$type = 'AsLiveComponent';
77+
}
6978

7079
$reflectionClass = new \ReflectionClass($class);
7180
$properties = $reflectionClass->getProperties();
81+
$allLiveProperties = [];
7282

7383
foreach ($properties as $property) {
7484
if ($property->isPublic()) {
7585
$visibility = $property->getType()?->getName();
7686
$propertyName = $property->getName();
7787
$value = $property->getDefaultValue();
88+
$propertyAttributes = $property->getAttributes(LiveProp::class);
7889

79-
$allProperties = [
80-
...$allProperties,
81-
$visibility.' $'.$propertyName.(null !== $value ? ' = '.$value : ''),
82-
];
90+
$propertyDisplay = $visibility.' $'.$propertyName.(null !== $value ? ' = '.$value : '');
91+
92+
if (\count($propertyAttributes) > 0) {
93+
$allLiveProperties = [
94+
...$allLiveProperties,
95+
$propertyDisplay,
96+
];
97+
} else {
98+
$allProperties = [
99+
...$allProperties,
100+
$propertyDisplay,
101+
];
102+
}
83103
}
84104
}
85105
} else {
@@ -124,11 +144,16 @@ protected function execute(InputInterface $input, OutputInterface $output): int
124144

125145
$componentInfos = [
126146
['Component', $name],
147+
['Type', $type],
127148
['Class', $class ?? 'Anonymous component'],
128149
['Template', $metadata->getTemplate()],
129150
[$propertyLabel, \count($allProperties) > 0 ? implode("\n", $allProperties) : null],
130151
];
131152

153+
if (isset($allLiveProperties) && \count($allLiveProperties) > 0) {
154+
$componentInfos[] = ['Live Properties', implode("\n", $allLiveProperties)];
155+
}
156+
132157
$table = new Table($output);
133158
$table->setHeaders(['Property', 'Value']);
134159

@@ -155,53 +180,64 @@ protected function execute(InputInterface $input, OutputInterface $output): int
155180
$attributes = $reflectionClass->getAttributes();
156181

157182
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);
183+
$attributeName = $attribute->getName();
184+
185+
if (\in_array($attributeName, [AsTwigComponent::class, AsLiveComponent::class])) {
186+
$arguments = $attribute->getArguments();
187+
188+
$name = $arguments['name'] ?? $arguments[0] ?? null;
189+
$template = $arguments['template'] ?? $arguments[1] ?? null;
190+
191+
if (null !== $template || null !== $name) {
192+
if (null !== $template && null !== $name) {
193+
$templateFile = str_replace('components/', '', $template);
194+
$metadata = $this->componentFactory->metadataFor($name);
195+
} elseif (null !== $name) {
196+
$templateFile = str_replace(':', '/', "{$name}.html.twig");
197+
$metadata = $this->componentFactory->metadataFor($name);
198+
} else {
199+
$templateFile = str_replace('components/', '', $template);
200+
$metadata = $this->componentFactory->metadataFor(str_replace('.html.twig', '', $templateFile));
201+
}
170202
} else {
171-
$templateFile = str_replace('components/', '', $template);
172-
$metadata = $this->componentFactory->metadataFor(str_replace('.html.twig', '', $templateFile));
203+
$templateFile = "{$reflectionClass->getShortName()}.html.twig";
204+
$metadata = $this->componentFactory->metadataFor($reflectionClass->getShortName());
173205
}
174-
} else {
175-
$templateFile = "{$reflectionClass->getShortName()}.html.twig";
176-
$metadata = $this->componentFactory->metadataFor($reflectionClass->getShortName());
177-
}
178206

179-
$componentsWithClass[] = $metadata->getName();
207+
$componentsWithClass[] = [
208+
'name' => $metadata->getName(),
209+
'type' => substr($attributeName, strrpos($attributeName, '\\') + 1),
210+
];
180211

181-
if (($key = array_search($templateFile, $anonymousTemplatesComponents)) !== false) {
182-
unset($anonymousTemplatesComponents[$key]);
212+
if (($key = array_search($templateFile, $anonymousTemplatesComponents)) !== false) {
213+
unset($anonymousTemplatesComponents[$key]);
214+
}
183215
}
184216
}
185217
}
186218

187-
$anonymousComponents = array_map(fn ($template): string => str_replace('/', ':', str_replace('.html.twig', '', $template)), $anonymousTemplatesComponents);
219+
$anonymousComponents = array_map(fn ($template): array => [
220+
'name' => str_replace('/', ':', str_replace('.html.twig', '', $template)),
221+
'type' => null,
222+
], $anonymousTemplatesComponents);
188223

189224
$allComponents = array_merge($componentsWithClass, $anonymousComponents);
190225
$dataToRender = [];
191226
foreach ($allComponents as $component) {
192-
$metadata = $this->componentFactory->metadataFor($component);
227+
$metadata = $this->componentFactory->metadataFor($component['name']);
193228

194229
$dataToRender = [...$dataToRender,
195230
[
196231
$metadata->getName(),
197232
$metadata->get('class') ?? 'Anonymous component',
198233
$metadata->getTemplate(),
234+
$component['type'],
199235
],
200236
];
201237
}
202238

203239
$table = new Table($output);
204-
$table->setHeaders(['Component', 'Class', 'Template']);
240+
$table->setHeaders(['Component', 'Class', 'Template', 'Type']);
205241

206242
foreach ($dataToRender as $data) {
207243
$table->addRow($data);

0 commit comments

Comments
 (0)