Skip to content

Commit b5dd556

Browse files
committed
[LiveComponent] add controller tag and default metadata values for all service declaration types
1 parent cd4dbe3 commit b5dd556

File tree

4 files changed

+33
-2
lines changed

4 files changed

+33
-2
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Symfony\UX\LiveComponent\DependencyInjection\Compiler;
6+
7+
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
8+
use Symfony\Component\DependencyInjection\ContainerBuilder;
9+
10+
final class AddControllerTagToLiveComponentPass implements CompilerPassInterface
11+
{
12+
public function process(ContainerBuilder $container): void
13+
{
14+
foreach ($container->findTaggedServiceIds('twig.component') as $id => $component) {
15+
if (!($component[0]['live'] ?? false)) {
16+
continue;
17+
}
18+
19+
$definition = $container->getDefinition($id);
20+
21+
if (!$definition->hasTag('controller.service_arguments')) {
22+
$definition->addTag('controller.service_arguments');
23+
}
24+
}
25+
}
26+
}

src/LiveComponent/src/DependencyInjection/LiveComponentExtension.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,6 @@ public function load(array $configs, ContainerBuilder $container): void
9393
function (ChildDefinition $definition, AsLiveComponent $attribute) {
9494
$definition
9595
->addTag('twig.component', array_filter($attribute->serviceConfig(), static fn ($v) => null !== $v && '' !== $v))
96-
->addTag('controller.service_arguments')
9796
;
9897
}
9998
);

src/LiveComponent/src/LiveComponentBundle.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Symfony\Component\DependencyInjection\Compiler\PassConfig;
1515
use Symfony\Component\DependencyInjection\ContainerBuilder;
1616
use Symfony\Component\HttpKernel\Bundle\Bundle;
17+
use Symfony\UX\LiveComponent\DependencyInjection\Compiler\AddControllerTagToLiveComponentPass;
1718
use Symfony\UX\LiveComponent\DependencyInjection\Compiler\ComponentDefaultActionPass;
1819
use Symfony\UX\LiveComponent\DependencyInjection\Compiler\OptionalDependencyPass;
1920

@@ -28,6 +29,7 @@ public function build(ContainerBuilder $container): void
2829
{
2930
// must run before Symfony\Component\Serializer\DependencyInjection\SerializerPass
3031
$container->addCompilerPass(new OptionalDependencyPass(), PassConfig::TYPE_BEFORE_OPTIMIZATION, 100);
32+
$container->addCompilerPass(new AddControllerTagToLiveComponentPass(), PassConfig::TYPE_BEFORE_OPTIMIZATION, 1);
3133
$container->addCompilerPass(new ComponentDefaultActionPass());
3234
}
3335

src/LiveComponent/src/Util/LiveControllerAttributesCreator.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,11 @@ public function attributesForRendering(MountedComponent $mounted, ComponentMetad
5757
$attributesCollection = $this->attributeHelper->create();
5858
$attributesCollection->setLiveController($mounted->getName());
5959

60-
$url = $this->urlGenerator->generate($metadata->get('route'), ['_live_component' => $mounted->getName()], $metadata->get('url_reference_type'));
60+
$url = $this->urlGenerator->generate(
61+
$metadata->get('route') ?? 'ux_live_component',
62+
['_live_component' => $mounted->getName()],
63+
$metadata->get('url_reference_type') ?? UrlGeneratorInterface::ABSOLUTE_PATH
64+
);
6165
$attributesCollection->setUrl($url);
6266

6367
$liveListeners = AsLiveComponent::liveListeners($mounted->getComponent());

0 commit comments

Comments
 (0)