diff --git a/packages/framework-extra-bundle/DependencyInjection/Compiler/EmailWriterCompilerPass.php b/packages/framework-extra-bundle/DependencyInjection/Compiler/EmailWriterCompilerPass.php index 816d296a..44b0e646 100644 --- a/packages/framework-extra-bundle/DependencyInjection/Compiler/EmailWriterCompilerPass.php +++ b/packages/framework-extra-bundle/DependencyInjection/Compiler/EmailWriterCompilerPass.php @@ -32,9 +32,10 @@ public function process(ContainerBuilder $container): void $priority = 0; } - $emailType = (new \ReflectionMethod($class, $methodName))->getParameters()[0]->getClass()->name; - $emailWriterListenerDefinition - ->addMethodCall('addWriter', [$emailType, $id, $methodName, $priority]); + foreach ($this->getClasses((new \ReflectionMethod($class, $methodName))->getParameters()[0]) as $emailType) { + $emailWriterListenerDefinition + ->addMethodCall('addWriter', [$emailType, $id, $methodName, $priority]); + } } } @@ -44,4 +45,27 @@ public function process(ContainerBuilder $container): void ServiceLocatorTagPass::register($container, $writers) ); } + + /** + * Extract classes base on union and name type. + * + * @return array + */ + private function getClasses(\ReflectionParameter $reflectionParameter): array + { + $type = $reflectionParameter->getType(); + + if ($type instanceof \ReflectionNamedType) { + return [$type->getName()]; + } + + if ($type instanceof \ReflectionUnionType) { + return array_map( + fn (\ReflectionNamedType $type) => $type->getName(), + $type->getTypes() + ); + } + + throw new \InvalidArgumentException('Unable to extract classes from parameter. Only named type and union type are supported.'); + } }