Skip to content

Commit 4662b60

Browse files
committed
alias named events even if they are not in annotation finder scope
1 parent 477d5c9 commit 4662b60

File tree

7 files changed

+68
-32
lines changed

7 files changed

+68
-32
lines changed

packages/Ecotone/src/Messaging/Config/Annotation/ModuleConfiguration/AsynchronousModule.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ public function resolveChannels(array $extensionObjects): array
255255
return $endpointChannels;
256256
}
257257

258-
public function handleRoutingEvent(RoutingEvent $event, ?Configuration $messagingConfiguration = null): void
258+
public function handleRoutingEvent(RoutingEvent $event): void
259259
{
260260
$registration = $event->getRegistration();
261261
$isAsynchronous = $registration->hasMethodAnnotation(Asynchronous::class);

packages/Ecotone/src/Modelling/Config/AggregrateModule.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -496,14 +496,16 @@ public function registerBusinessRepositories(InterfaceToCallRegistry $interfaceT
496496
}
497497
}
498498

499-
public function handleRoutingEvent(RoutingEvent $event, ?Configuration $messagingConfiguration = null): void
499+
public function handleRoutingEvent(RoutingEvent $event): void
500500
{
501501
$registration = $event->getRegistration();
502502

503503
if (! $registration->hasAnnotation(Aggregate::class)) {
504504
return;
505505
}
506506

507+
$messagingConfiguration = $event->getBusRoutingMapBuilder()->getMessagingConfiguration();
508+
507509
Assert::notNull($messagingConfiguration, 'RoutingEvent should be handled with messaging configuration, but it is null. Did you forget to pass it?');
508510

509511
if ($registration->isMagicMethod()) {

packages/Ecotone/src/Modelling/Config/Routing/BusRoutingMapBuilder.php

Lines changed: 54 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77

88
namespace Ecotone\Modelling\Config\Routing;
99

10+
use Ecotone\Messaging\Handler\Type;
11+
use Ecotone\Messaging\Handler\TypeDescriptor;
12+
use Ecotone\Modelling\Attribute\NamedEvent;
1013
use function array_map;
1114
use function array_unique;
1215

@@ -49,19 +52,15 @@ public function getRoutesFromAnnotatedFinding(AnnotatedFinding $registration, In
4952
return [$routingKey];
5053
}
5154

52-
$interfaceToCall = $interfaceToCallRegistry->getFor($registration->getClassName(), $registration->getMethodName());
53-
if ($interfaceToCall->hasNoParameters()) {
55+
$type = $this->getFirstParameterType($interfaceToCallRegistry, $registration);
56+
if (!$type) {
5457
return [];
55-
}
56-
$type = $interfaceToCall->getFirstParameter()->getTypeDescriptor();
57-
if ($type->isUnionType()) {
58+
} else {
5859
$routes = [];
5960
foreach ($type->getUnionTypes() as $unionType) {
6061
$routes[] = (string) $unionType;
6162
}
6263
return $routes;
63-
} else {
64-
return [(string) $type];
6564
}
6665
}
6766

@@ -76,9 +75,36 @@ public function addRoutesFromAnnotatedFinding(AnnotatedFinding $registration, In
7675
$routes = $this->getRoutesFromAnnotatedFinding($registration, $interfaceToCallRegistry);
7776
$priority = PriorityBasedOnType::fromAnnotatedFinding($registration);
7877

79-
$routingEvent = new RoutingEvent($registration, $destinationChannel, $routes, $priority->getPriorityArray());
78+
$routingEvent = new RoutingEvent($this, $registration, $destinationChannel, $routes, $priority->getPriorityArray());
79+
80+
$this->dispatchRoutingEvent($routingEvent);
81+
82+
if ($routingEvent->isCanceled()) {
83+
return null; // event is canceled, no routing
84+
}
85+
$destinationChannel = $routingEvent->getDestinationChannel();
86+
$priority = $routingEvent->getPriority();
87+
88+
foreach ($routingEvent->getRoutingKeys() as $routingKey) {
89+
$this->addRoute($routingKey, $destinationChannel, $priority);
90+
}
91+
92+
// add object alias if the routing key is a class and has a NamedEvent annotation
93+
$type = $this->getFirstParameterType($interfaceToCallRegistry, $registration);
94+
if ($type) {
95+
foreach ($type->getUnionTypes() as $unionType) {
96+
$className = (string) $unionType;
97+
if (class_exists($className)) {
98+
$classDefinition = $interfaceToCallRegistry->getClassDefinitionFor(TypeDescriptor::create($className));
99+
if ($classDefinition->hasClassAnnotation(TypeDescriptor::create(NamedEvent::class))) {
100+
$namedEvent = $classDefinition->getSingleClassAnnotation(TypeDescriptor::create(NamedEvent::class));
101+
$this->addObjectAlias($className, $namedEvent->getName());
102+
}
103+
}
104+
}
105+
}
80106

81-
return $this->dispatchRoutingEvent($routingEvent);
107+
return $destinationChannel;
82108
}
83109

84110
/**
@@ -166,6 +192,9 @@ public function addNamedRoute(string $routeName, string $channel, int|array $pri
166192
public function addObjectAlias(string $class, string $routingKey): void
167193
{
168194
if (isset($this->classToNameAliases[$class])) {
195+
if ($this->classToNameAliases[$class] === $routingKey) {
196+
return; // already registered
197+
}
169198
throw ConfigurationException::create("Class $class already has an alias registered: " . $this->classToNameAliases[$class]);
170199
}
171200
if (isset($this->nameToClassAliases[$routingKey])) {
@@ -243,6 +272,11 @@ public function compile(): Definition
243272
]);
244273
}
245274

275+
public function getMessagingConfiguration(): ?Configuration
276+
{
277+
return $this->messagingConfiguration;
278+
}
279+
246280
private function channelName(string $channel): string
247281
{
248282
if (isset($this->channelsName[$channel])) {
@@ -263,25 +297,22 @@ private function uniqueRoutedChannels(array $routes): array
263297
/**
264298
* @return ?string the destination channel name or null if the event is canceled
265299
*/
266-
private function dispatchRoutingEvent(RoutingEvent $routingEvent): ?string
300+
private function dispatchRoutingEvent(RoutingEvent $routingEvent): void
267301
{
268302
foreach ($this->routingEventHandlers as $routingEventHandler) {
269-
$routingEventHandler->handleRoutingEvent($routingEvent, $this->messagingConfiguration);
270-
if ($routingEvent->isCanceled()) {
271-
return null;
272-
}
273-
if ($routingEvent->isPropagationStopped()) {
274-
break;
303+
$routingEventHandler->handleRoutingEvent($routingEvent);
304+
if ($routingEvent->isCanceled() || $routingEvent->isPropagationStopped()) {
305+
return;
275306
}
276307
}
308+
}
277309

278-
$destinationChannel = $routingEvent->getDestinationChannel();
279-
$priority = $routingEvent->getPriority();
280-
281-
foreach ($routingEvent->getRoutingKeys() as $routingKey) {
282-
$this->addRoute($routingKey, $destinationChannel, $priority);
310+
private function getFirstParameterType(InterfaceToCallRegistry $interfaceToCallRegistry, AnnotatedFinding $registration): ?Type
311+
{
312+
$interfaceToCall = $interfaceToCallRegistry->getFor($registration->getClassName(), $registration->getMethodName());
313+
if ($interfaceToCall->hasNoParameters()) {
314+
return null;
283315
}
284-
285-
return $destinationChannel;
316+
return $interfaceToCall->getFirstParameter()->getTypeDescriptor();
286317
}
287318
}

packages/Ecotone/src/Modelling/Config/Routing/RoutingEvent.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ class RoutingEvent
1919
* @param int|int[] $priority
2020
*/
2121
public function __construct(
22+
private BusRoutingMapBuilder $busRoutingMapBuilder,
2223
private readonly AnnotatedFinding $registration,
2324
private string $destinationChannel,
2425
private array $routingKeys,
@@ -83,4 +84,9 @@ public function stopPropagation(): void
8384
{
8485
$this->isPropagationStopped = true;
8586
}
87+
88+
public function getBusRoutingMapBuilder(): BusRoutingMapBuilder
89+
{
90+
return $this->busRoutingMapBuilder;
91+
}
8692
}

packages/Ecotone/src/Modelling/Config/Routing/RoutingEventHandler.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,7 @@
77

88
namespace Ecotone\Modelling\Config\Routing;
99

10-
use Ecotone\Messaging\Config\Configuration;
11-
1210
interface RoutingEventHandler
1311
{
14-
public function handleRoutingEvent(RoutingEvent $event, ?Configuration $messagingConfiguration = null): void;
12+
public function handleRoutingEvent(RoutingEvent $event): void;
1513
}

packages/Ecotone/src/Modelling/Config/ServiceHandlerModule.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public function prepare(Configuration $messagingConfiguration, array $moduleExte
6262
{
6363
}
6464

65-
public function handleRoutingEvent(RoutingEvent $event, ?Configuration $messagingConfiguration = null): void
65+
public function handleRoutingEvent(RoutingEvent $event): void
6666
{
6767
$registration = $event->getRegistration();
6868
if ($registration->hasClassAnnotation(Aggregate::class)) {
@@ -80,7 +80,7 @@ public function handleRoutingEvent(RoutingEvent $event, ?Configuration $messagin
8080
? TransformerBuilder::create(AnnotatedDefinitionReference::getReferenceFor($registration), $this->interfaceToCallRegistry->getFor($registration->getClassName(), $registration->getMethodName()))
8181
: ServiceActivatorBuilder::create(AnnotatedDefinitionReference::getReferenceFor($registration), $this->interfaceToCallRegistry->getFor($registration->getClassName(), $registration->getMethodName()));
8282

83-
$messagingConfiguration->registerMessageHandler(
83+
$event->getBusRoutingMapBuilder()->getMessagingConfiguration()->registerMessageHandler(
8484
$handler
8585
->withInputChannelName($event->getDestinationChannel())
8686
->withOutputMessageChannel($methodAnnotation->getOutputChannelName())

packages/PdoEventSourcing/src/Config/EventSourcingModuleRoutingExtension.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
namespace Ecotone\EventSourcing\Config;
99

1010
use Ecotone\EventSourcing\Attribute\Projection;
11-
use Ecotone\Messaging\Config\Configuration;
1211
use Ecotone\Modelling\Attribute\CommandHandler;
1312
use Ecotone\Modelling\Attribute\EventHandler;
1413
use Ecotone\Modelling\Config\Routing\RoutingEvent;
@@ -26,7 +25,7 @@ public function __construct(private array $pollingProjectionNames)
2625
{
2726
}
2827

29-
public function handleRoutingEvent(RoutingEvent $event, ?Configuration $messagingConfiguration = null): void
28+
public function handleRoutingEvent(RoutingEvent $event): void
3029
{
3130
$registration = $event->getRegistration();
3231
$isCommandOrEventHandler = $registration->hasAnnotation(CommandHandler::class) || $registration->hasAnnotation(EventHandler::class);

0 commit comments

Comments
 (0)