Conversation
a6096e3 to
fec150f
Compare
fec150f to
8911a18
Compare
debug:live-component command
Kocal
left a comment
There was a problem hiding this comment.
Thanks for the suggestion, here some comments
|
|
||
| To get specific information about a component, specify its name (or a part of it): | ||
|
|
||
| <info>php %command.full_name% Alert</info> |
There was a problem hiding this comment.
Nitpick, let's reuse ProductSearch component from the official document, I don't see how a Alert can be a live component:
| <info>php %command.full_name% Alert</info> | |
| <info>php %command.full_name% ProductSearch </info> |
| if (\is_string($name)) { | ||
| $componentName = $this->findComponentName($io, $name, $input->isInteractive()); | ||
| if (null === $componentName) { | ||
| $io->error(\sprintf('Unknown LiveComponent "%s".', $name)); |
There was a problem hiding this comment.
| $io->error(\sprintf('Unknown LiveComponent "%s".', $name)); | |
| $io->error(\sprintf('Unknown component "%s".', $name)); |
| return null; | ||
| } | ||
|
|
||
| private function listComponents(?string $eventFilter = null): array |
There was a problem hiding this comment.
| private function listComponents(?string $eventFilter = null): array | |
| /** | |
| * @return array<string, LiveComponentMetadata> | |
| */ | |
| private function findComponents(?string $eventFilter = null): array |
There was a problem hiding this comment.
Can't we re-use the LiveComponentMetadataFactory & LiveComponentMetadata instead?
| if (null === $eventFilter) { | ||
| return $this->componentClassMap; | ||
| } |
There was a problem hiding this comment.
| if (null === $eventFilter) { | |
| return $this->componentClassMap; | |
| } | |
| $components = []; | |
| if (null === $eventFilter) { | |
| foreach ($this->componentClassMap as $class => $name) { | |
| $components[$name] ??= $this->liveComponentMetadataFactory->getMetadata($name); | |
| } | |
| return $components; | |
| } |
| foreach ($this->componentClassMap as $name => $component) { | ||
| foreach (AsLiveComponent::liveListeners($component['class']) as $listener) { | ||
| if ($listener['event'] === $eventFilter) { | ||
| $filteredComponents[$name] = $component; |
There was a problem hiding this comment.
| $filteredComponents[$name] = $component; | |
| $components[$name] ??= $this->liveComponentMetadataFactory->getMetadata($name); |
| return $this->componentClassMap; | ||
| } | ||
|
|
||
| $filteredComponents = []; |
There was a problem hiding this comment.
| $filteredComponents = []; |
| } | ||
| } | ||
|
|
||
| return $filteredComponents; |
There was a problem hiding this comment.
| return $filteredComponents; | |
| return $components; |
|
|
||
| private function displayComponentDetails(SymfonyStyle $io, string $name): void | ||
| { | ||
| $component = $this->componentClassMap[$name]; |
There was a problem hiding this comment.
Please do not directly depend on $componentClassMap but use the (internal) dedicated classes instead, that would makes things safer instead of relying on array-based and undocumented structures
| $properties = []; | ||
| $reflectionClass = new \ReflectionClass($class); | ||
| foreach ($reflectionClass->getProperties() as $property) { | ||
| if (!$property->isPublic()) { | ||
| continue; | ||
| } | ||
| if (empty($property->getAttributes(LiveProp::class))) { | ||
| continue; | ||
| } | ||
|
|
||
| $type = $this->displayType($property->getType()); | ||
| $propertyName = '$'.$property->getName(); | ||
| $defaultValueDisplay = $property->hasDefaultValue() ? | ||
| $this->displayDefaultValue($property->getDefaultValue()) : | ||
| ''; | ||
| $arguments = $property->getAttributes(LiveProp::class)[0]->getArguments(); | ||
| $argumentsDisplay = empty($arguments) ? | ||
| '' : | ||
| ' ('.implode(', ', array_map( | ||
| static fn ($key, $value) => $key.': '.json_encode($value), | ||
| array_keys($arguments), | ||
| $arguments | ||
| )).')'; | ||
|
|
||
| $propertyDisplay = $type.$propertyName.$defaultValueDisplay.$argumentsDisplay; | ||
| $properties[$property->name] = $propertyDisplay; | ||
| } | ||
|
|
||
| return $properties; |
There was a problem hiding this comment.
The code cold be super-simplified by relying on getAllLivePropsMetadata() from LiveComponentMetadata (as suggested earlier)
| $componentReferences[$tag['key']] = new Reference($id); | ||
| $componentNames[] = $tag['key']; | ||
| $componentClassMap[$tag['class']] = $tag['key']; | ||
| $definition->addTag('twig.component', $tag); |
debug:live-component commanddebug:live-component command
1e20046 to
a79410c
Compare
a79410c to
4880892
Compare
|
@Kocal Thanks for the review ! I also don't see a way to compute the name of a component in the LiveComponentPass without using directly TwigComponent configurations, that's why I added the |
Hello,
In several applications, I am using multiple events to communicate between LiveComponents.
It sometimes take me a little time to find out mistakes concerning these events.
That's why I propose this small command for debug specific to LiveComponents.
This command is largely copied from @StevenRenaux TwigComponentDebugCommand.
As the previous one, it can be used for listing or to see details of one component :
--listeningfilter.