Skip to content

Commit bc3d442

Browse files
committed
minor #2210 [TwigComponent] Fix ux:icon & ux:map renders (smnandre)
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
2 parents 2d9a149 + 24c1cb2 commit bc3d442

File tree

10 files changed

+40
-114
lines changed

10 files changed

+40
-114
lines changed

src/Icons/composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@
5252
"sort-packages": true
5353
},
5454
"conflict": {
55-
"symfony/flex": "<1.13"
55+
"symfony/flex": "<1.13",
56+
"symfony/ux-twig-component": "<2.21"
5657
},
5758
"extra": {
5859
"thanks": {

src/Icons/config/twig_component.php

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,6 @@
1616

1717
return static function (ContainerConfigurator $container): void {
1818
$container->services()
19-
->set('.ux_icons.twig_component_listener', UXIconComponentListener::class)
20-
->args([
21-
service('.ux_icons.icon_renderer'),
22-
])
23-
->tag('kernel.event_listener', [
24-
'event' => 'Symfony\UX\TwigComponent\Event\PreCreateForRenderEvent',
25-
'method' => 'onPreCreateForRender',
26-
])
27-
2819
->set('.ux_icons.twig_component.icon', UXIconComponent::class)
2920
->tag('twig.component', ['key' => 'UX:Icon'])
3021
;

src/Icons/src/Twig/UXIconComponentListener.php

Lines changed: 0 additions & 43 deletions
This file was deleted.

src/Icons/src/Twig/UXIconRuntime.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,12 @@ public function renderIcon(string $name, array $attributes = []): string
4747
throw $e;
4848
}
4949
}
50+
51+
public function render(array $args = []): string
52+
{
53+
$name = $args['name'];
54+
unset($args['name']);
55+
56+
return $this->renderIcon($name, $args);
57+
}
5058
}

src/Map/composer.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@
4242
"symfony/twig-bundle": "^6.4|^7.0",
4343
"symfony/ux-twig-component": "^2.18"
4444
},
45+
"conflict": {
46+
"symfony/ux-twig-component": "<2.21"
47+
},
4548
"extra": {
4649
"thanks": {
4750
"name": "symfony/ux",

src/Map/config/twig_component.php

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,6 @@
1717

1818
return static function (ContainerConfigurator $container): void {
1919
$container->services()
20-
->set('.ux_map.twig_component_listener', UXMapComponentListener::class)
21-
->args([
22-
service('ux_map.twig_runtime'),
23-
])
24-
->tag('kernel.event_listener', [
25-
'event' => PreCreateForRenderEvent::class,
26-
'method' => 'onPreCreateForRender',
27-
])
28-
2920
->set('.ux_map.twig_component.map', UXMapComponent::class)
3021
->tag('twig.component', ['key' => 'UX:Map'])
3122
;

src/Map/src/Twig/MapRuntime.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,4 +67,12 @@ public function renderMap(
6767

6868
return $this->renderer->renderMap($map, $attributes);
6969
}
70+
71+
public function render(array $args = []): string
72+
{
73+
$map = array_intersect_key($args, ['map' => 0, 'markers' => 0, 'polygons' => 0, 'center' => 1, 'zoom' => 2]);
74+
$attributes = array_diff_key($args, $map);
75+
76+
return $this->renderMap(...$map, attributes: $attributes);
77+
}
7078
}

src/Map/src/Twig/UXMapComponentListener.php

Lines changed: 0 additions & 42 deletions
This file was deleted.

src/TwigComponent/src/DependencyInjection/TwigComponentExtension.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@
3636
use Symfony\UX\TwigComponent\Twig\ComponentLexer;
3737
use Symfony\UX\TwigComponent\Twig\ComponentRuntime;
3838
use Symfony\UX\TwigComponent\Twig\TwigEnvironmentConfigurator;
39+
use function Symfony\Component\DependencyInjection\Loader\Configurator\service;
40+
use function Symfony\Component\DependencyInjection\Loader\Configurator\service_locator;
3941

4042
/**
4143
* @author Kevin Bond <[email protected]>
@@ -108,6 +110,10 @@ class_exists(AbstractArgument::class) ? new AbstractArgument(\sprintf('Added in
108110
$container->register('.ux.twig_component.twig.component_runtime', ComponentRuntime::class)
109111
->setArguments([
110112
new Reference('ux.twig_component.component_renderer'),
113+
service_locator([
114+
'ux:icon' => service('.ux_icons.twig_icon_runtime')->nullOnInvalid(),
115+
'ux:map' => service('ux_map.twig_runtime')->nullOnInvalid(),
116+
]),
111117
])
112118
->addTag('twig.runtime')
113119
;

src/TwigComponent/src/Twig/ComponentRuntime.php

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\UX\TwigComponent\Twig;
1313

14+
use Symfony\Component\DependencyInjection\ServiceLocator;
1415
use Symfony\UX\TwigComponent\ComponentRenderer;
1516
use Symfony\UX\TwigComponent\Event\PreRenderEvent;
1617

@@ -24,15 +25,13 @@ final class ComponentRuntime
2425
{
2526
public function __construct(
2627
private readonly ComponentRenderer $renderer,
28+
private readonly ServiceLocator $renderers,
2729
) {
2830
}
2931

30-
/**
31-
* @param array<string, mixed> $props
32-
*/
33-
public function render(string $name, array $props = []): string
32+
public function finishEmbedComponent(): void
3433
{
35-
return $this->renderer->createAndRender($name, $props);
34+
$this->renderer->finishEmbeddedComponentRender();
3635
}
3736

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

45+
public function render(string $name, array $props = []): string
46+
{
47+
if ($this->renderers->has($normalized = strtolower($name))) {
48+
return $this->renderers->get($normalized)->render($props);
49+
}
50+
51+
return $this->renderer->createAndRender($name, $props);
52+
}
53+
4654
/**
4755
* @param array<string, mixed> $props
4856
* @param array<string, mixed> $context
@@ -51,9 +59,4 @@ public function startEmbedComponent(string $name, array $props, array $context,
5159
{
5260
return $this->renderer->startEmbeddedComponentRender($name, $props, $context, $hostTemplateName, $index);
5361
}
54-
55-
public function finishEmbedComponent(): void
56-
{
57-
$this->renderer->finishEmbeddedComponentRender();
58-
}
5962
}

0 commit comments

Comments
 (0)