Skip to content

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

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

Merged
merged 1 commit into from
Sep 27, 2024
Merged
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
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(),
Copy link
Member

Choose a reason for hiding this comment

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

chiming here a bit late: this doesn't work
a service locator must point to non-null references
when these are turned into nulls, XmlDumper explodes (and that's a symptom of this code, which is not "legal")

Also, from a design pov, this looks super hardcoded. How can one pass more things to the locator?

This should use a tagged locator instead.

Copy link
Member

Choose a reason for hiding this comment

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

]),
])
->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();
}
}
Loading