From bd329ae819dbd7251cc5a78ab31cdf9f295a5602 Mon Sep 17 00:00:00 2001 From: Maxim Smakouz Date: Wed, 28 Feb 2024 15:57:37 +0200 Subject: [PATCH] Creating activity in temporal.activity scope --- src/Dispatcher.php | 22 ++++++++++++++++------ tests/app/src/SomeActivityWithScope.php | 20 ++++++++++++++++++++ tests/src/DispatcherTest.php | 18 ++++++++++++++++++ 3 files changed, 54 insertions(+), 6 deletions(-) create mode 100644 tests/app/src/SomeActivityWithScope.php diff --git a/src/Dispatcher.php b/src/Dispatcher.php index b30e145..0fc989c 100644 --- a/src/Dispatcher.php +++ b/src/Dispatcher.php @@ -4,10 +4,13 @@ namespace Spiral\TemporalBridge; +use Psr\Container\ContainerInterface; use ReflectionClass; use Spiral\Attribute\DispatcherScope; use Spiral\Boot\DispatcherInterface; -use Spiral\Core\Container; +use Spiral\Core\FactoryInterface; +use Spiral\Core\Scope; +use Spiral\Core\ScopeInterface; use Spiral\Framework\Spiral; use Spiral\RoadRunnerBridge\RoadRunnerMode; use Temporal\Activity\ActivityInterface; @@ -19,8 +22,9 @@ final class Dispatcher implements DispatcherInterface { public function __construct( private readonly RoadRunnerMode $mode, - private readonly Container $container, + private readonly ContainerInterface $container, private readonly DeclarationWorkerResolver $workerResolver, + private readonly ScopeInterface $scope, ) { } @@ -58,10 +62,7 @@ public function serve(): void if ($type === ActivityInterface::class) { // Workflows are stateful. So you need a type to create instances. - $worker->registerActivity( - $declaration->getName(), - fn(ReflectionClass $class): object => $this->container->make($class->getName()), - ); + $worker->registerActivity($declaration->getName(), $this->makeActivity(...)); } $hasDeclarations = true; @@ -75,4 +76,13 @@ public function serve(): void // start primary loop $factory->run(); } + + private function makeActivity(ReflectionClass $class): object + { + /** @psalm-suppress InvalidArgument */ + return $this->scope->runScope( + new Scope(Spiral::TemporalActivity), + static fn(FactoryInterface $factory): object => $factory->make($class->getName()), + ); + } } diff --git a/tests/app/src/SomeActivityWithScope.php b/tests/app/src/SomeActivityWithScope.php new file mode 100644 index 0000000..bd06dff --- /dev/null +++ b/tests/app/src/SomeActivityWithScope.php @@ -0,0 +1,20 @@ + 'foo']), ), + $this->getContainer(), ); } @@ -101,4 +104,19 @@ public function testServeWithDeclarations(): void $this->dispatcher->serve(); } + + public function testScope(): void + { + $binder = $this->getContainer()->getBinder(Spiral::TemporalActivity); + $binder->bind(SomeActivityWithScope::class, SomeActivityWithScope::class); + $binder->bind(\ArrayAccess::class, $this->createMock(\ArrayAccess::class)); + + $ref = new \ReflectionMethod($this->dispatcher, 'makeActivity'); + $ref->invoke($this->dispatcher, new \ReflectionClass(SomeActivityWithScope::class)); + + $this->assertInstanceOf( + SomeActivityWithScope::class, + $ref->invoke($this->dispatcher, new \ReflectionClass(SomeActivityWithScope::class)) + ); + } }