Skip to content

Commit dd1121f

Browse files
committed
Add LiveActions and Events
1 parent 105ae6e commit dd1121f

File tree

2 files changed

+54
-46
lines changed

2 files changed

+54
-46
lines changed

src/TwigComponent/src/Command/ComponentDebugCommand.php

Lines changed: 48 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,11 @@
2020
use Symfony\Component\Console\Style\SymfonyStyle;
2121
use Symfony\Component\Finder\Finder;
2222
use Symfony\UX\LiveComponent\Attribute\AsLiveComponent;
23+
use Symfony\UX\LiveComponent\Attribute\LiveAction;
2324
use Symfony\UX\LiveComponent\Attribute\LiveProp;
2425
use Symfony\UX\TwigComponent\Attribute\AsTwigComponent;
26+
use Symfony\UX\TwigComponent\Attribute\PostMount;
27+
use Symfony\UX\TwigComponent\Attribute\PreMount;
2528
use Symfony\UX\TwigComponent\ComponentFactory;
2629
use Symfony\UX\TwigComponent\Twig\PropsNode;
2730
use Twig\Environment;
@@ -55,7 +58,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
5558
$io = new SymfonyStyle($input, $output);
5659
$name = $input->getArgument('name');
5760

58-
if ($name) {
61+
if (null !== $name) {
5962
try {
6063
$metadata = $this->componentFactory->metadataFor($name);
6164
} catch (\Exception $e) {
@@ -69,7 +72,6 @@ protected function execute(InputInterface $input, OutputInterface $output): int
6972
$allProperties = [];
7073

7174
if ($class) {
72-
$propertyLabel = 'Properties (type / name / default value if exist)';
7375
$type = 'AsTwigComponent';
7476

7577
if ($metadata->get('live')) {
@@ -90,21 +92,40 @@ protected function execute(InputInterface $input, OutputInterface $output): int
9092
$propertyDisplay = $visibility.' $'.$propertyName.(null !== $value ? ' = '.$value : '');
9193

9294
if (\count($propertyAttributes) > 0) {
93-
$allLiveProperties = [
94-
...$allLiveProperties,
95-
$propertyDisplay,
96-
];
95+
$allLiveProperties[] = $propertyDisplay;
9796
} else {
98-
$allProperties = [
99-
...$allProperties,
100-
$propertyDisplay,
101-
];
97+
$allProperties[] = $propertyDisplay;
10298
}
10399
}
104100
}
105-
} else {
106-
$propertyLabel = 'Properties (name / default value if exist)';
107101

102+
$methods = $reflectionClass->getMethods();
103+
$allEvents = [];
104+
$allActions = [];
105+
106+
foreach ($methods as $method) {
107+
if ('mount' === $method->getName()) {
108+
$allEvents[] = 'Mount';
109+
}
110+
111+
foreach ($method->getAttributes() as $attribute) {
112+
if (PreMount::class === $attribute->getName()) {
113+
$allEvents[] = 'PreMount';
114+
break;
115+
}
116+
117+
if (PostMount::class === $attribute->getName()) {
118+
$allEvents[] = 'PostMount';
119+
break;
120+
}
121+
122+
if (LiveAction::class === $attribute->getName()) {
123+
$allActions[] = $method->getName();
124+
break;
125+
}
126+
}
127+
}
128+
} else {
108129
$source = $this->twigEnvironment->load($metadata->getTemplate())->getSourceContext();
109130
$tokenStream = $this->twigEnvironment->tokenize($source);
110131
$bodyNode = $this->twigEnvironment->parse($tokenStream)->getNode('body')->getNode(0);
@@ -134,10 +155,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
134155
$property = $variable;
135156
}
136157

137-
$allProperties = [
138-
...$allProperties,
139-
$property,
140-
];
158+
$allProperties[] = $property;
141159
}
142160
}
143161
}
@@ -147,20 +165,21 @@ protected function execute(InputInterface $input, OutputInterface $output): int
147165
['Type', $type],
148166
['Class', $class ?? 'Anonymous component'],
149167
['Template', $metadata->getTemplate()],
150-
[$propertyLabel, \count($allProperties) > 0 ? implode("\n", $allProperties) : null],
168+
['Properties', \count($allProperties) > 0 ? implode("\n", $allProperties) : null],
151169
];
152170

153171
if (isset($allLiveProperties) && \count($allLiveProperties) > 0) {
154172
$componentInfos[] = ['Live Properties', implode("\n", $allLiveProperties)];
155173
}
156-
157-
$table = new Table($output);
158-
$table->setHeaders(['Property', 'Value']);
159-
160-
foreach ($componentInfos as $info) {
161-
$table->addRow($info);
174+
if (isset($allEvents) && \count($allEvents) > 0) {
175+
$componentInfos[] = ['Events', implode("\n", $allEvents)];
176+
}
177+
if (isset($allActions) && \count($allActions) > 0) {
178+
$componentInfos[] = ['LiveAction Methods', implode("\n", $allActions)];
162179
}
163180

181+
$table = new Table($output);
182+
$table->setHeaders(['Property', 'Value'])->setRows($componentInfos);
164183
$table->render();
165184

166185
return Command::SUCCESS;
@@ -226,23 +245,16 @@ protected function execute(InputInterface $input, OutputInterface $output): int
226245
foreach ($allComponents as $component) {
227246
$metadata = $this->componentFactory->metadataFor($component['name']);
228247

229-
$dataToRender = [...$dataToRender,
230-
[
231-
$metadata->getName(),
232-
$metadata->get('class') ?? 'Anonymous component',
233-
$metadata->getTemplate(),
234-
$component['type'],
235-
],
248+
$dataToRender[] = [
249+
$metadata->getName(),
250+
$metadata->get('class') ?? 'Anonymous component',
251+
$metadata->getTemplate(),
252+
$component['type'],
236253
];
237254
}
238255

239256
$table = new Table($output);
240-
$table->setHeaders(['Component', 'Class', 'Template', 'Type']);
241-
242-
foreach ($dataToRender as $data) {
243-
$table->addRow($data);
244-
}
245-
257+
$table->setHeaders(['Component', 'Class', 'Template', 'Type'])->setRows($dataToRender);
246258
$table->render();
247259

248260
return Command::SUCCESS;

src/TwigComponent/tests/Unit/Command/ComponentDebugCommandTest.php

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,11 @@ public function testWithNoComponent(): void
2424

2525
$commandTester->assertCommandIsSuccessful();
2626

27-
$this->tableDisplayCheck($commandTester->getDisplay());
27+
$display = $commandTester->getDisplay();
28+
29+
$this->assertStringContainsString('Component', $display);
30+
$this->assertStringContainsString('Class', $display);
31+
$this->assertStringContainsString('Template', $display);
2832
}
2933

3034
public function testWithNoMatchComponent(): void
@@ -46,7 +50,6 @@ public function testComponentWithClass(): void
4650
$display = $commandTester->getDisplay();
4751

4852
$this->tableDisplayCheck($display);
49-
$this->tableDisplayCheckWithOneComponent($display);
5053
$this->assertStringContainsString('BasicComponent', $display);
5154
$this->assertStringContainsString('Component\BasicComponent', $display);
5255
$this->assertStringContainsString('components/BasicComponent.html.twig', $display);
@@ -62,7 +65,6 @@ public function testComponentWithClassPropertiesAndCustomName(): void
6265
$display = $commandTester->getDisplay();
6366

6467
$this->tableDisplayCheck($display);
65-
$this->tableDisplayCheckWithOneComponent($display);
6668
$this->assertStringContainsString('component_c', $display);
6769
$this->assertStringContainsString('Component\ComponentC', $display);
6870
$this->assertStringContainsString('components/component_c.html.twig', $display);
@@ -81,7 +83,6 @@ public function testComponentWithClassPropertiesCustomNameAndCustomTemplate(): v
8183
$display = $commandTester->getDisplay();
8284

8385
$this->tableDisplayCheck($display);
84-
$this->tableDisplayCheckWithOneComponent($display);
8586
$this->assertStringContainsString('component_b', $display);
8687
$this->assertStringContainsString('Component\ComponentB', $display);
8788
$this->assertStringContainsString('components/custom1.html.twig', $display);
@@ -102,7 +103,6 @@ public function testWithAnonymousComponent(): void
102103
$this->assertStringContainsString('Button', $display);
103104
$this->assertStringContainsString('Anonymous component', $display);
104105
$this->assertStringContainsString('components/Button.html.twig', $display);
105-
$this->assertStringContainsString('Properties (name / default value if exist)', $display);
106106
$this->assertStringContainsString('label', $display);
107107
$this->assertStringContainsString('primary = true', $display);
108108
}
@@ -120,10 +120,6 @@ private function tableDisplayCheck(string $display): void
120120
$this->assertStringContainsString('Component', $display);
121121
$this->assertStringContainsString('Class', $display);
122122
$this->assertStringContainsString('Template', $display);
123-
}
124-
125-
private function tableDisplayCheckWithOneComponent(string $display): void
126-
{
127-
$this->assertStringContainsString('Properties (type / name / default value if exist)', $display);
123+
$this->assertStringContainsString('Properties', $display);
128124
}
129125
}

0 commit comments

Comments
 (0)