diff --git a/src/LiveComponent/src/DependencyInjection/Compiler/AddControllerTagToLiveComponentPass.php b/src/LiveComponent/src/DependencyInjection/Compiler/AddControllerTagToLiveComponentPass.php new file mode 100644 index 00000000000..7042ddc80a0 --- /dev/null +++ b/src/LiveComponent/src/DependencyInjection/Compiler/AddControllerTagToLiveComponentPass.php @@ -0,0 +1,33 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\UX\LiveComponent\DependencyInjection\Compiler; + +use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; +use Symfony\Component\DependencyInjection\ContainerBuilder; + +final class AddControllerTagToLiveComponentPass implements CompilerPassInterface +{ + public function process(ContainerBuilder $container): void + { + foreach ($container->findTaggedServiceIds('twig.component') as $id => $component) { + if (!($component[0]['live'] ?? false)) { + continue; + } + + $definition = $container->getDefinition($id); + + if (!$definition->hasTag('controller.service_arguments')) { + $definition->addTag('controller.service_arguments'); + } + } + } +} diff --git a/src/LiveComponent/src/DependencyInjection/LiveComponentExtension.php b/src/LiveComponent/src/DependencyInjection/LiveComponentExtension.php index 91da00e4e20..041e1e4d5c1 100644 --- a/src/LiveComponent/src/DependencyInjection/LiveComponentExtension.php +++ b/src/LiveComponent/src/DependencyInjection/LiveComponentExtension.php @@ -93,7 +93,6 @@ public function load(array $configs, ContainerBuilder $container): void function (ChildDefinition $definition, AsLiveComponent $attribute) { $definition ->addTag('twig.component', array_filter($attribute->serviceConfig(), static fn ($v) => null !== $v && '' !== $v)) - ->addTag('controller.service_arguments') ; } ); diff --git a/src/LiveComponent/src/LiveComponentBundle.php b/src/LiveComponent/src/LiveComponentBundle.php index ba5af470ae7..ce8506e3b64 100644 --- a/src/LiveComponent/src/LiveComponentBundle.php +++ b/src/LiveComponent/src/LiveComponentBundle.php @@ -14,6 +14,7 @@ use Symfony\Component\DependencyInjection\Compiler\PassConfig; use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\HttpKernel\Bundle\Bundle; +use Symfony\UX\LiveComponent\DependencyInjection\Compiler\AddControllerTagToLiveComponentPass; use Symfony\UX\LiveComponent\DependencyInjection\Compiler\ComponentDefaultActionPass; use Symfony\UX\LiveComponent\DependencyInjection\Compiler\OptionalDependencyPass; @@ -28,6 +29,7 @@ public function build(ContainerBuilder $container): void { // must run before Symfony\Component\Serializer\DependencyInjection\SerializerPass $container->addCompilerPass(new OptionalDependencyPass(), PassConfig::TYPE_BEFORE_OPTIMIZATION, 100); + $container->addCompilerPass(new AddControllerTagToLiveComponentPass(), PassConfig::TYPE_BEFORE_OPTIMIZATION, 1); $container->addCompilerPass(new ComponentDefaultActionPass()); } diff --git a/src/LiveComponent/src/Util/LiveControllerAttributesCreator.php b/src/LiveComponent/src/Util/LiveControllerAttributesCreator.php index 64d7866eb4f..78f90d29298 100644 --- a/src/LiveComponent/src/Util/LiveControllerAttributesCreator.php +++ b/src/LiveComponent/src/Util/LiveControllerAttributesCreator.php @@ -57,7 +57,11 @@ public function attributesForRendering(MountedComponent $mounted, ComponentMetad $attributesCollection = $this->attributeHelper->create(); $attributesCollection->setLiveController($mounted->getName()); - $url = $this->urlGenerator->generate($metadata->get('route'), ['_live_component' => $mounted->getName()], $metadata->get('url_reference_type')); + $url = $this->urlGenerator->generate( + $metadata->get('route') ?? 'ux_live_component', + ['_live_component' => $mounted->getName()], + $metadata->get('url_reference_type') ?? UrlGeneratorInterface::ABSOLUTE_PATH + ); $attributesCollection->setUrl($url); $liveListeners = AsLiveComponent::liveListeners($mounted->getComponent());