Skip to content

Commit

Permalink
minor symfony#2210 [TwigComponent] Fix ux:icon & ux:map renders (smna…
Browse files Browse the repository at this point in the history
…ndre)

This PR was squashed before being merged into the 2.x branch.

Discussion
----------

[TwigComponent] Fix ux:icon & ux:map renders

Change implementation to avoid many dispatched events for nothing

Add a conflict in symfony/ux-map and symfony/ux-icons  to synchronise the 3 packages to 2.21

Performance gain around 20%

Commits
-------

24c1cb2 [TwigComponent] Fix ux:icon & ux:map renders
  • Loading branch information
javiereguiluz committed Sep 27, 2024
2 parents 2d9a149 + 24c1cb2 commit bc3d442
Show file tree
Hide file tree
Showing 10 changed files with 40 additions and 114 deletions.
3 changes: 2 additions & 1 deletion src/Icons/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@
"sort-packages": true
},
"conflict": {
"symfony/flex": "<1.13"
"symfony/flex": "<1.13",
"symfony/ux-twig-component": "<2.21"
},
"extra": {
"thanks": {
Expand Down
9 changes: 0 additions & 9 deletions src/Icons/config/twig_component.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,6 @@

return static function (ContainerConfigurator $container): void {
$container->services()
->set('.ux_icons.twig_component_listener', UXIconComponentListener::class)
->args([
service('.ux_icons.icon_renderer'),
])
->tag('kernel.event_listener', [
'event' => 'Symfony\UX\TwigComponent\Event\PreCreateForRenderEvent',
'method' => 'onPreCreateForRender',
])

->set('.ux_icons.twig_component.icon', UXIconComponent::class)
->tag('twig.component', ['key' => 'UX:Icon'])
;
Expand Down
43 changes: 0 additions & 43 deletions src/Icons/src/Twig/UXIconComponentListener.php

This file was deleted.

8 changes: 8 additions & 0 deletions src/Icons/src/Twig/UXIconRuntime.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,12 @@ public function renderIcon(string $name, array $attributes = []): string
throw $e;
}
}

public function render(array $args = []): string
{
$name = $args['name'];
unset($args['name']);

return $this->renderIcon($name, $args);
}
}
3 changes: 3 additions & 0 deletions src/Map/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@
"symfony/twig-bundle": "^6.4|^7.0",
"symfony/ux-twig-component": "^2.18"
},
"conflict": {
"symfony/ux-twig-component": "<2.21"
},
"extra": {
"thanks": {
"name": "symfony/ux",
Expand Down
9 changes: 0 additions & 9 deletions src/Map/config/twig_component.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,6 @@

return static function (ContainerConfigurator $container): void {
$container->services()
->set('.ux_map.twig_component_listener', UXMapComponentListener::class)
->args([
service('ux_map.twig_runtime'),
])
->tag('kernel.event_listener', [
'event' => PreCreateForRenderEvent::class,
'method' => 'onPreCreateForRender',
])

->set('.ux_map.twig_component.map', UXMapComponent::class)
->tag('twig.component', ['key' => 'UX:Map'])
;
Expand Down
8 changes: 8 additions & 0 deletions src/Map/src/Twig/MapRuntime.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,12 @@ public function renderMap(

return $this->renderer->renderMap($map, $attributes);
}

public function render(array $args = []): string
{
$map = array_intersect_key($args, ['map' => 0, 'markers' => 0, 'polygons' => 0, 'center' => 1, 'zoom' => 2]);
$attributes = array_diff_key($args, $map);

return $this->renderMap(...$map, attributes: $attributes);
}
}
42 changes: 0 additions & 42 deletions src/Map/src/Twig/UXMapComponentListener.php

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
use Symfony\UX\TwigComponent\Twig\ComponentLexer;
use Symfony\UX\TwigComponent\Twig\ComponentRuntime;
use Symfony\UX\TwigComponent\Twig\TwigEnvironmentConfigurator;
use function Symfony\Component\DependencyInjection\Loader\Configurator\service;
use function Symfony\Component\DependencyInjection\Loader\Configurator\service_locator;

/**
* @author Kevin Bond <[email protected]>
Expand Down Expand Up @@ -108,6 +110,10 @@ class_exists(AbstractArgument::class) ? new AbstractArgument(\sprintf('Added in
$container->register('.ux.twig_component.twig.component_runtime', ComponentRuntime::class)
->setArguments([
new Reference('ux.twig_component.component_renderer'),
service_locator([
'ux:icon' => service('.ux_icons.twig_icon_runtime')->nullOnInvalid(),
'ux:map' => service('ux_map.twig_runtime')->nullOnInvalid(),
]),
])
->addTag('twig.runtime')
;
Expand Down
23 changes: 13 additions & 10 deletions src/TwigComponent/src/Twig/ComponentRuntime.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Symfony\UX\TwigComponent\Twig;

use Symfony\Component\DependencyInjection\ServiceLocator;
use Symfony\UX\TwigComponent\ComponentRenderer;
use Symfony\UX\TwigComponent\Event\PreRenderEvent;

Expand All @@ -24,15 +25,13 @@ final class ComponentRuntime
{
public function __construct(
private readonly ComponentRenderer $renderer,
private readonly ServiceLocator $renderers,
) {
}

/**
* @param array<string, mixed> $props
*/
public function render(string $name, array $props = []): string
public function finishEmbedComponent(): void
{
return $this->renderer->createAndRender($name, $props);
$this->renderer->finishEmbeddedComponentRender();
}

/**
Expand All @@ -43,6 +42,15 @@ public function preRender(string $name, array $props): ?string
return $this->renderer->preCreateForRender($name, $props);
}

public function render(string $name, array $props = []): string
{
if ($this->renderers->has($normalized = strtolower($name))) {
return $this->renderers->get($normalized)->render($props);
}

return $this->renderer->createAndRender($name, $props);
}

/**
* @param array<string, mixed> $props
* @param array<string, mixed> $context
Expand All @@ -51,9 +59,4 @@ public function startEmbedComponent(string $name, array $props, array $context,
{
return $this->renderer->startEmbeddedComponentRender($name, $props, $context, $hostTemplateName, $index);
}

public function finishEmbedComponent(): void
{
$this->renderer->finishEmbeddedComponentRender();
}
}

0 comments on commit bc3d442

Please sign in to comment.