Skip to content

Commit d6c45ba

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

File tree

4 files changed

+40
-2
lines changed

4 files changed

+40
-2
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\UX\LiveComponent\DependencyInjection\Compiler;
13+
14+
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
15+
use Symfony\Component\DependencyInjection\ContainerBuilder;
16+
17+
final class AddControllerTagToLiveComponentPass implements CompilerPassInterface
18+
{
19+
public function process(ContainerBuilder $container): void
20+
{
21+
foreach ($container->findTaggedServiceIds('twig.component') as $id => $component) {
22+
if (!($component[0]['live'] ?? false)) {
23+
continue;
24+
}
25+
26+
$definition = $container->getDefinition($id);
27+
28+
if (!$definition->hasTag('controller.service_arguments')) {
29+
$definition->addTag('controller.service_arguments');
30+
}
31+
}
32+
}
33+
}

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)