Skip to content

Commit bc2be3f

Browse files
committed
collect saga handlers dependencies
1 parent 3ee61dc commit bc2be3f

File tree

2 files changed

+62
-22
lines changed

2 files changed

+62
-22
lines changed

src/Configuration/Attributes/SagaAttributeBasedConfigurationLoader.php

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
* @license https://opensource.org/licenses/MIT
99
*/
1010

11-
declare(strict_types = 0);
11+
declare(strict_types=0);
1212

1313
namespace ServiceBus\Sagas\Configuration\Attributes;
1414

@@ -41,8 +41,7 @@ final class SagaAttributeBasedConfigurationLoader implements SagaConfigurationLo
4141
public function __construct(
4242
SagaMessageProcessorFactory $eventListenerProcessorFactory,
4343
?Reader $attributesReader = null
44-
)
45-
{
44+
) {
4645
$this->eventListenerProcessorFactory = $eventListenerProcessorFactory;
4746
$this->attributesReader = $attributesReader ?? new AttributesReader();
4847
}
@@ -79,7 +78,7 @@ classLevelAttributes: $attributes->classLevelCollection
7978
)
8079
);
8180
}
82-
catch(\Throwable $throwable)
81+
catch (\Throwable $throwable)
8382
{
8483
throw InvalidSagaConfiguration::fromThrowable($throwable);
8584
}
@@ -97,15 +96,14 @@ classLevelAttributes: $attributes->classLevelCollection
9796
private function collectSagaEventHandlers(
9897
\SplObjectStorage $methodLevelAttributes,
9998
SagaMetadata $sagaMetadata
100-
): \SplObjectStorage
101-
{
99+
): \SplObjectStorage {
102100
/** @psalm-var \SplObjectStorage<MessageHandler, null> $handlersCollection */
103101
$handlersCollection = new \SplObjectStorage();
104102

105103
/** @var MethodLevel $methodLevelAttribute */
106-
foreach($methodLevelAttributes as $methodLevelAttribute)
104+
foreach ($methodLevelAttributes as $methodLevelAttribute)
107105
{
108-
if($methodLevelAttribute->attribute instanceof SagaEventListener)
106+
if ($methodLevelAttribute->attribute instanceof SagaEventListener)
109107
{
110108
$handlersCollection->attach(
111109
$this->createMessageHandler(
@@ -129,8 +127,7 @@ private function createMessageHandler(
129127
MethodLevel $methodLevelAttribute,
130128
SagaMetadata $sagaMetadata,
131129
SagaMessageHandlerType $handlerType
132-
): MessageHandler
133-
{
130+
): MessageHandler {
134131
/** @var SagaEventListener|SagaInitialHandler $attribute */
135132
$attribute = $methodLevelAttribute->attribute;
136133

@@ -156,7 +153,7 @@ private function createMessageHandler(
156153
SagaMessageHandlerType::EVENT_LISTENER => createEventListenerName($messageClass)
157154
};
158155

159-
if($expectedMethodName === $reflectionMethod->name)
156+
if ($expectedMethodName === $reflectionMethod->name)
160157
{
161158
/** @var callable $processor */
162159
$processor = match ($handlerType)
@@ -201,7 +198,7 @@ private function extractMessageClass(\ReflectionMethod $reflectionMethod): strin
201198
? $reflectionParameters[0]->getType()
202199
: null;
203200

204-
if($firstArgumentType !== null)
201+
if ($firstArgumentType !== null)
205202
{
206203
/** @var \ReflectionNamedType $reflectionType */
207204
$reflectionType = $reflectionParameters[0]->getType();
@@ -210,7 +207,7 @@ private function extractMessageClass(\ReflectionMethod $reflectionMethod): strin
210207
$messageClass = $reflectionType->getName();
211208

212209
/** @psalm-suppress RedundantConditionGivenDocblockType */
213-
if(\class_exists($messageClass))
210+
if (\class_exists($messageClass))
214211
{
215212
return $messageClass;
216213
}
@@ -228,7 +225,7 @@ private function extractMessageClass(\ReflectionMethod $reflectionMethod): strin
228225
*/
229226
private static function createSagaMetadata(string $sagaClass, SagaHeader $sagaHeader): SagaMetadata
230227
{
231-
if(\class_exists($sagaHeader->idClass) === false)
228+
if (\class_exists($sagaHeader->idClass) === false)
232229
{
233230
throw new \InvalidArgumentException(
234231
\sprintf(
@@ -258,20 +255,19 @@ private static function createSagaMetadata(string $sagaClass, SagaHeader $sagaHe
258255
private function findInitialCommandHandlerAttribute(
259256
string $sagaClass,
260257
\SplObjectStorage $methodLevelAttributes
261-
): MethodLevel
262-
{
258+
): MethodLevel {
263259
/** @var MethodLevel[] $commandHandlersAttributes */
264260
$commandHandlersAttributes = \array_filter(
265261
\array_map(
266-
static function(MethodLevel $attribute): ?MethodLevel
262+
static function (MethodLevel $attribute): ?MethodLevel
267263
{
268264
return $attribute->attribute instanceof SagaInitialHandler ? $attribute : null;
269265
},
270266
\iterator_to_array($methodLevelAttributes)
271267
)
272268
);
273269

274-
if(\count($commandHandlersAttributes) === 1)
270+
if (\count($commandHandlersAttributes) === 1)
275271
{
276272
return \end($commandHandlersAttributes);
277273
}
@@ -292,11 +288,11 @@ static function(MethodLevel $attribute): ?MethodLevel
292288
private static function searchSagaHeader(string $sagaClass, \SplObjectStorage $classLevelAttributes): SagaHeader
293289
{
294290
/** @var ClassLevel $attributes */
295-
foreach($classLevelAttributes as $attributes)
291+
foreach ($classLevelAttributes as $attributes)
296292
{
297293
$attributeObject = $attributes->attribute;
298294

299-
if($attributeObject instanceof SagaHeader)
295+
if ($attributeObject instanceof SagaHeader)
300296
{
301297
return $attributeObject;
302298
}

src/Module/SagaModule.php

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212

1313
namespace ServiceBus\Sagas\Module;
1414

15+
use Psr\Log\LoggerInterface;
16+
use Psr\Log\NullLogger;
1517
use ServiceBus\AnnotationsReader\Reader;
1618
use ServiceBus\ArgumentResolver\ChainArgumentResolver;
1719
use ServiceBus\ArgumentResolver\ContainerArgumentResolver;
@@ -186,10 +188,17 @@ public function boot(ContainerBuilder $containerBuilder): void
186188
{
187189
$containerBuilder->setParameter('service_bus.sagas.list', $this->sagasToRegister);
188190

191+
if ($containerBuilder->hasDefinition(LoggerInterface::class) === false)
192+
{
193+
$containerBuilder->addDefinitions([
194+
LoggerInterface::class => new Definition(NullLogger::class)
195+
]);
196+
}
197+
189198
$this->registerDefaultArgumentResolver($containerBuilder);
190199
$this->registerSagaStore($containerBuilder);
191200
$this->registerMutexFactory($containerBuilder);
192-
$this->registerSagasProvider($containerBuilder);
201+
$this->registerSagaFinder($containerBuilder);
193202
$this->registerSagasLifecycleManager($containerBuilder);
194203

195204
if ($this->configurationLoaderServiceId === null)
@@ -200,6 +209,7 @@ public function boot(ContainerBuilder $containerBuilder): void
200209
}
201210

202211
$this->registerRoutesConfigurator($containerBuilder);
212+
$this->collectSagasDependencies($containerBuilder);
203213
}
204214

205215
private function registerSagasLifecycleManager(ContainerBuilder $containerBuilder): void
@@ -256,7 +266,7 @@ private function registerRoutesConfigurator(ContainerBuilder $containerBuilder):
256266
);
257267
}
258268

259-
private function registerSagasProvider(ContainerBuilder $containerBuilder): void
269+
private function registerSagaFinder(ContainerBuilder $containerBuilder): void
260270
{
261271
$sagasFinderDefinition = (new Definition(SagaFinder::class))
262272
->setArguments(
@@ -358,6 +368,40 @@ private function registerDefaultConfigurationLoader(ContainerBuilder $containerB
358368
$this->configurationLoaderServiceId = SagaConfigurationLoader::class;
359369
}
360370

371+
private function collectSagasDependencies(ContainerBuilder $containerBuilder): void
372+
{
373+
$externalDependencies = [];
374+
375+
foreach ($this->sagasToRegister as $sagaClass)
376+
{
377+
$reflectionClass = new \ReflectionClass($sagaClass);
378+
379+
foreach ($reflectionClass->getMethods() as $reflectionMethod)
380+
{
381+
foreach ($reflectionMethod->getParameters() as $reflectionParameter)
382+
{
383+
$reflectionType = $reflectionParameter->getType();
384+
385+
if (($reflectionType instanceof \ReflectionNamedType) === false)
386+
{
387+
continue;
388+
}
389+
390+
$className = $reflectionType->getName();
391+
392+
if ($containerBuilder->hasDefinition($className))
393+
{
394+
$containerBuilder->getDefinition($className)->setPublic(true);
395+
396+
$externalDependencies[] = $className;
397+
}
398+
}
399+
}
400+
}
401+
402+
$containerBuilder->setParameter('saga_dependencies', $externalDependencies);
403+
}
404+
361405
private function __construct(
362406
string $sagaStoreServiceId,
363407
string $databaseAdapterServiceId,

0 commit comments

Comments
 (0)