diff --git a/src/LiveComponent/src/DependencyInjection/Compiler/OptionalDependencyPass.php b/src/LiveComponent/src/DependencyInjection/Compiler/OptionalDependencyPass.php new file mode 100644 index 00000000000..8e4531b0bf2 --- /dev/null +++ b/src/LiveComponent/src/DependencyInjection/Compiler/OptionalDependencyPass.php @@ -0,0 +1,32 @@ + + */ +final class OptionalDependencyPass implements CompilerPassInterface +{ + public function process(ContainerBuilder $container): void + { + if ($container->hasDefinition('doctrine')) { + $container->register('ux.live_component.doctrine_entity_property_hydrator', DoctrineEntityPropertyHydrator::class) + ->setArguments([[new Reference('doctrine')]]) + ->addTag('twig.component.property_hydrator', ['priority' => -100]) + ; + } + + if ($container->hasDefinition('serializer')) { + $container->register('ux.live_component.serializer_property_hydrator', NormalizerBridgePropertyHydrator::class) + ->setArguments([new Reference('serializer')]) + ->addTag('twig.component.property_hydrator', ['priority' => -200]) + ; + } + } +} diff --git a/src/LiveComponent/src/DependencyInjection/LiveComponentExtension.php b/src/LiveComponent/src/DependencyInjection/LiveComponentExtension.php index 1ba935ff4b4..fffceedac2b 100644 --- a/src/LiveComponent/src/DependencyInjection/LiveComponentExtension.php +++ b/src/LiveComponent/src/DependencyInjection/LiveComponentExtension.php @@ -22,8 +22,6 @@ use Symfony\UX\LiveComponent\ComponentValidator; use Symfony\UX\LiveComponent\ComponentValidatorInterface; use Symfony\UX\LiveComponent\EventListener\LiveComponentSubscriber; -use Symfony\UX\LiveComponent\Hydrator\DoctrineEntityPropertyHydrator; -use Symfony\UX\LiveComponent\Hydrator\NormalizerBridgePropertyHydrator; use Symfony\UX\LiveComponent\LiveComponentHydrator; use Symfony\UX\LiveComponent\PropertyHydratorInterface; use Symfony\UX\LiveComponent\Twig\LiveComponentExtension as LiveComponentTwigExtension; @@ -60,16 +58,6 @@ function (ChildDefinition $definition, AsLiveComponent $attribute) { ->addTag('twig.component.property_hydrator') ; - $container->register('ux.live_component.doctrine_entity_property_hydrator', DoctrineEntityPropertyHydrator::class) - ->setArguments([[new Reference('doctrine')]]) - ->addTag('twig.component.property_hydrator', ['priority' => -100]) - ; - - $container->register('ux.live_component.serializer_property_hydrator', NormalizerBridgePropertyHydrator::class) - ->setArguments([new Reference('serializer')]) - ->addTag('twig.component.property_hydrator', ['priority' => -200]) - ; - $container->register('ux.live_component.component_hydrator', LiveComponentHydrator::class) ->setArguments([ new TaggedIteratorArgument('twig.component.property_hydrator'), diff --git a/src/LiveComponent/src/LiveComponentBundle.php b/src/LiveComponent/src/LiveComponentBundle.php index e1fd8c6878f..ff3c2f58768 100644 --- a/src/LiveComponent/src/LiveComponentBundle.php +++ b/src/LiveComponent/src/LiveComponentBundle.php @@ -11,7 +11,9 @@ namespace Symfony\UX\LiveComponent; +use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\HttpKernel\Bundle\Bundle; +use Symfony\UX\LiveComponent\DependencyInjection\Compiler\OptionalDependencyPass; /** * @author Kevin Bond @@ -20,4 +22,8 @@ */ final class LiveComponentBundle extends Bundle { + public function build(ContainerBuilder $container): void + { + $container->addCompilerPass(new OptionalDependencyPass()); + } }