Skip to content

Commit

Permalink
collect saga handlers dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
mmasiukevich committed Apr 5, 2022
1 parent 3ee61dc commit bc2be3f
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* @license https://opensource.org/licenses/MIT
*/

declare(strict_types = 0);
declare(strict_types=0);

namespace ServiceBus\Sagas\Configuration\Attributes;

Expand Down Expand Up @@ -41,8 +41,7 @@ final class SagaAttributeBasedConfigurationLoader implements SagaConfigurationLo
public function __construct(
SagaMessageProcessorFactory $eventListenerProcessorFactory,
?Reader $attributesReader = null
)
{
) {
$this->eventListenerProcessorFactory = $eventListenerProcessorFactory;
$this->attributesReader = $attributesReader ?? new AttributesReader();
}
Expand Down Expand Up @@ -79,7 +78,7 @@ classLevelAttributes: $attributes->classLevelCollection
)
);
}
catch(\Throwable $throwable)
catch (\Throwable $throwable)
{
throw InvalidSagaConfiguration::fromThrowable($throwable);
}
Expand All @@ -97,15 +96,14 @@ classLevelAttributes: $attributes->classLevelCollection
private function collectSagaEventHandlers(
\SplObjectStorage $methodLevelAttributes,
SagaMetadata $sagaMetadata
): \SplObjectStorage
{
): \SplObjectStorage {
/** @psalm-var \SplObjectStorage<MessageHandler, null> $handlersCollection */
$handlersCollection = new \SplObjectStorage();

/** @var MethodLevel $methodLevelAttribute */
foreach($methodLevelAttributes as $methodLevelAttribute)
foreach ($methodLevelAttributes as $methodLevelAttribute)
{
if($methodLevelAttribute->attribute instanceof SagaEventListener)
if ($methodLevelAttribute->attribute instanceof SagaEventListener)
{
$handlersCollection->attach(
$this->createMessageHandler(
Expand All @@ -129,8 +127,7 @@ private function createMessageHandler(
MethodLevel $methodLevelAttribute,
SagaMetadata $sagaMetadata,
SagaMessageHandlerType $handlerType
): MessageHandler
{
): MessageHandler {
/** @var SagaEventListener|SagaInitialHandler $attribute */
$attribute = $methodLevelAttribute->attribute;

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

if($expectedMethodName === $reflectionMethod->name)
if ($expectedMethodName === $reflectionMethod->name)
{
/** @var callable $processor */
$processor = match ($handlerType)
Expand Down Expand Up @@ -201,7 +198,7 @@ private function extractMessageClass(\ReflectionMethod $reflectionMethod): strin
? $reflectionParameters[0]->getType()
: null;

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

/** @psalm-suppress RedundantConditionGivenDocblockType */
if(\class_exists($messageClass))
if (\class_exists($messageClass))
{
return $messageClass;
}
Expand All @@ -228,7 +225,7 @@ private function extractMessageClass(\ReflectionMethod $reflectionMethod): strin
*/
private static function createSagaMetadata(string $sagaClass, SagaHeader $sagaHeader): SagaMetadata
{
if(\class_exists($sagaHeader->idClass) === false)
if (\class_exists($sagaHeader->idClass) === false)
{
throw new \InvalidArgumentException(
\sprintf(
Expand Down Expand Up @@ -258,20 +255,19 @@ private static function createSagaMetadata(string $sagaClass, SagaHeader $sagaHe
private function findInitialCommandHandlerAttribute(
string $sagaClass,
\SplObjectStorage $methodLevelAttributes
): MethodLevel
{
): MethodLevel {
/** @var MethodLevel[] $commandHandlersAttributes */
$commandHandlersAttributes = \array_filter(
\array_map(
static function(MethodLevel $attribute): ?MethodLevel
static function (MethodLevel $attribute): ?MethodLevel
{
return $attribute->attribute instanceof SagaInitialHandler ? $attribute : null;
},
\iterator_to_array($methodLevelAttributes)
)
);

if(\count($commandHandlersAttributes) === 1)
if (\count($commandHandlersAttributes) === 1)
{
return \end($commandHandlersAttributes);
}
Expand All @@ -292,11 +288,11 @@ static function(MethodLevel $attribute): ?MethodLevel
private static function searchSagaHeader(string $sagaClass, \SplObjectStorage $classLevelAttributes): SagaHeader
{
/** @var ClassLevel $attributes */
foreach($classLevelAttributes as $attributes)
foreach ($classLevelAttributes as $attributes)
{
$attributeObject = $attributes->attribute;

if($attributeObject instanceof SagaHeader)
if ($attributeObject instanceof SagaHeader)
{
return $attributeObject;
}
Expand Down
48 changes: 46 additions & 2 deletions src/Module/SagaModule.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

namespace ServiceBus\Sagas\Module;

use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
use ServiceBus\AnnotationsReader\Reader;
use ServiceBus\ArgumentResolver\ChainArgumentResolver;
use ServiceBus\ArgumentResolver\ContainerArgumentResolver;
Expand Down Expand Up @@ -186,10 +188,17 @@ public function boot(ContainerBuilder $containerBuilder): void
{
$containerBuilder->setParameter('service_bus.sagas.list', $this->sagasToRegister);

if ($containerBuilder->hasDefinition(LoggerInterface::class) === false)
{
$containerBuilder->addDefinitions([
LoggerInterface::class => new Definition(NullLogger::class)
]);
}

$this->registerDefaultArgumentResolver($containerBuilder);
$this->registerSagaStore($containerBuilder);
$this->registerMutexFactory($containerBuilder);
$this->registerSagasProvider($containerBuilder);
$this->registerSagaFinder($containerBuilder);
$this->registerSagasLifecycleManager($containerBuilder);

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

$this->registerRoutesConfigurator($containerBuilder);
$this->collectSagasDependencies($containerBuilder);
}

private function registerSagasLifecycleManager(ContainerBuilder $containerBuilder): void
Expand Down Expand Up @@ -256,7 +266,7 @@ private function registerRoutesConfigurator(ContainerBuilder $containerBuilder):
);
}

private function registerSagasProvider(ContainerBuilder $containerBuilder): void
private function registerSagaFinder(ContainerBuilder $containerBuilder): void
{
$sagasFinderDefinition = (new Definition(SagaFinder::class))
->setArguments(
Expand Down Expand Up @@ -358,6 +368,40 @@ private function registerDefaultConfigurationLoader(ContainerBuilder $containerB
$this->configurationLoaderServiceId = SagaConfigurationLoader::class;
}

private function collectSagasDependencies(ContainerBuilder $containerBuilder): void
{
$externalDependencies = [];

foreach ($this->sagasToRegister as $sagaClass)
{
$reflectionClass = new \ReflectionClass($sagaClass);

foreach ($reflectionClass->getMethods() as $reflectionMethod)
{
foreach ($reflectionMethod->getParameters() as $reflectionParameter)
{
$reflectionType = $reflectionParameter->getType();

if (($reflectionType instanceof \ReflectionNamedType) === false)
{
continue;
}

$className = $reflectionType->getName();

if ($containerBuilder->hasDefinition($className))
{
$containerBuilder->getDefinition($className)->setPublic(true);

$externalDependencies[] = $className;
}
}
}
}

$containerBuilder->setParameter('saga_dependencies', $externalDependencies);
}

private function __construct(
string $sagaStoreServiceId,
string $databaseAdapterServiceId,
Expand Down

0 comments on commit bc2be3f

Please sign in to comment.