Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[LiveComponent] Add Events listening infos in Profiler and DebugCommand #1720

Open
wants to merge 1 commit into
base: 2.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 34 additions & 5 deletions src/TwigComponent/src/Command/TwigComponentDebugCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@
use Symfony\Component\Console\Helper\TableSeparator;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
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\TwigComponent\Attribute\AsTwigComponent;
use Symfony\UX\TwigComponent\Attribute\ExposeInTemplate;
use Symfony\UX\TwigComponent\ComponentFactory;
Expand Down Expand Up @@ -49,6 +51,11 @@ protected function configure(): void
$this
->setDefinition([
new InputArgument('name', InputArgument::OPTIONAL, 'A component name or part of the component name'),
new InputOption(
name: 'listening',
mode: InputOption::VALUE_REQUIRED,
description: 'Filter components list to display only those listening to the given action'
),
])
->setHelp(
<<<'EOF'
Expand Down Expand Up @@ -83,7 +90,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
return Command::SUCCESS;
}

$components = $this->findComponents();
$components = $this->findComponents($input->getOption('listening'));
$this->displayComponentsTable($io, $components);

return Command::SUCCESS;
Expand Down Expand Up @@ -128,14 +135,19 @@ private function findComponentName(SymfonyStyle $io, string $name, bool $interac
/**
* @return array<string, ComponentMetadata>
*/
private function findComponents(): array
private function findComponents(?string $listeningFilter): array
{
$components = [];
foreach ($this->componentClassMap as $class => $name) {
$components[$name] ??= $this->componentFactory->metadataFor($name);
if (null === $listeningFilter || \in_array($listeningFilter, $this->resolveEventsListening($class))) {
$components[$name] ??= $this->componentFactory->metadataFor($name);
}
}
foreach ($this->findAnonymousComponents() as $name => $template) {
$components[$name] ??= $this->componentFactory->metadataFor($name);

if (null === $listeningFilter) {
foreach ($this->findAnonymousComponents() as $name => $template) {
$components[$name] ??= $this->componentFactory->metadataFor($name);
}
}

return $components;
Expand Down Expand Up @@ -196,6 +208,14 @@ private function displayComponentDetails(SymfonyStyle $io, string $name): void
['Properties', implode("\n", $this->getComponentProperties($metadata))],
]);

$eventsListened = $this->resolveEventsListening($metadata->get('class'));
if ($eventsListened) {
$table->addRows([
new TableSeparator(),
['Listening to', implode("\n", $eventsListened)],
]);
}

$logMethod = function (\ReflectionMethod $m) {
$params = array_map(
fn (\ReflectionParameter $p) => '$'.$p->getName(),
Expand Down Expand Up @@ -314,4 +334,13 @@ private function getAnonymousComponentProperties(ComponentMetadata $metadata): a

return $properties;
}

private function resolveEventsListening(string $class): array
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The duplication of this method is unfortunate. Can we use/adjust this method?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the suggestion ! I was not aware of this method. I removed the @internal to use it, but don't you think it could be a separate service ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I removed the https://github.com/internal to use it

Can you keep @internal on the method. We don't want this to be used by end-users.

but don't you think it could be a separate service ?

Can you explain this a bit more?

Copy link
Author

@mbuliard mbuliard Apr 11, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you explain this a bit more?

Since the method is use elsewhere and its logic is not inherently linked to the attribute, I would think of creating a separate service let's say LiveListenersResolver and call it in the attribute and in my modifications.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair point, keeping it internal enables us to make this kind of refactor in the future. For now, let's keep as is.

{
if (class_exists(AsLiveComponent::class)) {
return array_column(AsLiveComponent::liveListeners($class), 'event');
}

return [];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Symfony\Component\HttpKernel\DataCollector\LateDataCollectorInterface;
use Symfony\Component\VarDumper\Caster\ClassStub;
use Symfony\Component\VarDumper\Cloner\Data;
use Symfony\UX\LiveComponent\Attribute\AsLiveComponent;
use Symfony\UX\TwigComponent\Event\PostRenderEvent;
use Symfony\UX\TwigComponent\Event\PreRenderEvent;
use Symfony\UX\TwigComponent\EventListener\TwigComponentLoggerListener;
Expand Down Expand Up @@ -115,6 +116,9 @@ private function collectDataFromLogger(): void
'template_path' => $this->resolveTemplatePath($metadata->getTemplate()), // defer ? lazy ?
'render_count' => 0,
'render_time' => 0,
'listening' => class_exists(AsLiveComponent::class) ?
array_column(AsLiveComponent::liveListeners($componentClass), 'event') :
[],
];

$renderId = spl_object_id($mountedComponent);
Expand Down
27 changes: 15 additions & 12 deletions src/TwigComponent/templates/Collector/twig_component.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,9 @@
{% else %}
<span class=text-muted">{{ component.template }}</span>
{% endif %}
{% if component.listening is not empty %}
<pre class="sf-dump"><span class="text-muted">Listening to {{ component.listening|join(', ') }}</span></pre>
{% endif %}
</td>
<td class="cell-right">{{ component.render_count }}</td>
<td class="cell-right">
Expand Down Expand Up @@ -285,18 +288,18 @@
</tr>
</thead>
<tbody id="render-{{ loop.index }}--details">
<tr class="{{ not render.input_props|default ? 'opacity-50' }}">
<th scope="row">Input props</th>
<td colspan="3">{{ profiler_dump(render.input_props) }}</td>
</tr>
<tr class="{{ not render.attributes|default ? 'opacity-50' }}">
<th scope="row">Attributes</th>
<td colspan="3">{{ profiler_dump(render.attributes) }}</td>
</tr>
<tr>
<th scope="row">Component</th>
<td colspan="3">{{ profiler_dump(render.component) }}</td>
</tr>
<tr class="{{ not render.input_props|default ? 'opacity-50' }}">
<th scope="row">Input props</th>
<td colspan="3">{{ profiler_dump(render.input_props) }}</td>
</tr>
<tr class="{{ not render.attributes|default ? 'opacity-50' }}">
<th scope="row">Attributes</th>
<td colspan="3">{{ profiler_dump(render.attributes) }}</td>
</tr>
<tr>
<th scope="row">Component</th>
<td colspan="3">{{ profiler_dump(render.component) }}</td>
</tr>
</tbody>
</table>
{% endfor %}
Expand Down
Loading