From 24c1cb24e9414c33e44e87150973e578dfcbe02a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20Andr=C3=A9?= Date: Thu, 26 Sep 2024 03:42:12 +0200 Subject: [PATCH] [TwigComponent] Fix ux:icon & ux:map renders --- src/Icons/composer.json | 3 +- src/Icons/config/twig_component.php | 9 ---- .../src/Twig/UXIconComponentListener.php | 43 ------------------- src/Icons/src/Twig/UXIconRuntime.php | 8 ++++ src/Map/composer.json | 3 ++ src/Map/config/twig_component.php | 9 ---- src/Map/src/Twig/MapRuntime.php | 8 ++++ src/Map/src/Twig/UXMapComponentListener.php | 42 ------------------ .../TwigComponentExtension.php | 6 +++ .../src/Twig/ComponentRuntime.php | 23 +++++----- 10 files changed, 40 insertions(+), 114 deletions(-) delete mode 100644 src/Icons/src/Twig/UXIconComponentListener.php delete mode 100644 src/Map/src/Twig/UXMapComponentListener.php diff --git a/src/Icons/composer.json b/src/Icons/composer.json index 14dfadb5adf..469effc3a2f 100644 --- a/src/Icons/composer.json +++ b/src/Icons/composer.json @@ -52,7 +52,8 @@ "sort-packages": true }, "conflict": { - "symfony/flex": "<1.13" + "symfony/flex": "<1.13", + "symfony/ux-twig-component": "<2.21" }, "extra": { "thanks": { diff --git a/src/Icons/config/twig_component.php b/src/Icons/config/twig_component.php index f466f253662..5e27cc81ff7 100644 --- a/src/Icons/config/twig_component.php +++ b/src/Icons/config/twig_component.php @@ -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']) ; diff --git a/src/Icons/src/Twig/UXIconComponentListener.php b/src/Icons/src/Twig/UXIconComponentListener.php deleted file mode 100644 index 8ccf4df1a12..00000000000 --- a/src/Icons/src/Twig/UXIconComponentListener.php +++ /dev/null @@ -1,43 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Symfony\UX\Icons\Twig; - -use Symfony\UX\Icons\IconRendererInterface; -use Symfony\UX\TwigComponent\Event\PreCreateForRenderEvent; - -/** - * @author Simon André - * - * @internal - */ -final class UXIconComponentListener -{ - public function __construct( - private IconRendererInterface $iconRenderer, - ) { - } - - public function onPreCreateForRender(PreCreateForRenderEvent $event): void - { - if ('ux:icon' !== strtolower($event->getName())) { - return; - } - - $attributes = $event->getInputProps(); - $name = (string) $attributes['name']; - unset($attributes['name']); - - $svg = $this->iconRenderer->renderIcon($name, $attributes); - $event->setRenderedString($svg); - $event->stopPropagation(); - } -} diff --git a/src/Icons/src/Twig/UXIconRuntime.php b/src/Icons/src/Twig/UXIconRuntime.php index 7276ac08644..edaffd8bf5b 100644 --- a/src/Icons/src/Twig/UXIconRuntime.php +++ b/src/Icons/src/Twig/UXIconRuntime.php @@ -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); + } } diff --git a/src/Map/composer.json b/src/Map/composer.json index a24b4fd3ca9..52adf9bc983 100644 --- a/src/Map/composer.json +++ b/src/Map/composer.json @@ -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", diff --git a/src/Map/config/twig_component.php b/src/Map/config/twig_component.php index f09999e9b5c..096eec4aea8 100644 --- a/src/Map/config/twig_component.php +++ b/src/Map/config/twig_component.php @@ -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']) ; diff --git a/src/Map/src/Twig/MapRuntime.php b/src/Map/src/Twig/MapRuntime.php index cfb47560bd2..8b9a1ca2036 100644 --- a/src/Map/src/Twig/MapRuntime.php +++ b/src/Map/src/Twig/MapRuntime.php @@ -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); + } } diff --git a/src/Map/src/Twig/UXMapComponentListener.php b/src/Map/src/Twig/UXMapComponentListener.php deleted file mode 100644 index 51034c53b4d..00000000000 --- a/src/Map/src/Twig/UXMapComponentListener.php +++ /dev/null @@ -1,42 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Symfony\UX\Map\Twig; - -use Symfony\UX\TwigComponent\Event\PreCreateForRenderEvent; - -/** - * @author Simon André - * - * @internal - */ -final class UXMapComponentListener -{ - public function __construct( - private MapRuntime $mapRuntime, - ) { - } - - public function onPreCreateForRender(PreCreateForRenderEvent $event): void - { - if ('ux:map' !== strtolower($event->getName())) { - return; - } - - $attributes = $event->getInputProps(); - $map = array_intersect_key($attributes, ['markers' => 0, 'polygons' => 0, 'center' => 1, 'zoom' => 2]); - $attributes = array_diff_key($attributes, $map); - - $html = $this->mapRuntime->renderMap(...$map, attributes: $attributes); - $event->setRenderedString($html); - $event->stopPropagation(); - } -} diff --git a/src/TwigComponent/src/DependencyInjection/TwigComponentExtension.php b/src/TwigComponent/src/DependencyInjection/TwigComponentExtension.php index c0c18b0ec66..aa1419ee88b 100644 --- a/src/TwigComponent/src/DependencyInjection/TwigComponentExtension.php +++ b/src/TwigComponent/src/DependencyInjection/TwigComponentExtension.php @@ -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 @@ -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') ; diff --git a/src/TwigComponent/src/Twig/ComponentRuntime.php b/src/TwigComponent/src/Twig/ComponentRuntime.php index b6157599781..f770ef32162 100644 --- a/src/TwigComponent/src/Twig/ComponentRuntime.php +++ b/src/TwigComponent/src/Twig/ComponentRuntime.php @@ -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; @@ -24,15 +25,13 @@ final class ComponentRuntime { public function __construct( private readonly ComponentRenderer $renderer, + private readonly ServiceLocator $renderers, ) { } - /** - * @param array $props - */ - public function render(string $name, array $props = []): string + public function finishEmbedComponent(): void { - return $this->renderer->createAndRender($name, $props); + $this->renderer->finishEmbeddedComponentRender(); } /** @@ -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 $props * @param array $context @@ -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(); - } }