From 77c724c9734048f4d8a8cce0c013b5c23dd6383d Mon Sep 17 00:00:00 2001 From: roxblnfk Date: Fri, 10 Jan 2025 19:40:06 +0400 Subject: [PATCH] Replace handler callable with MethodHandler class --- src/Internal/Declaration/Instance.php | 24 +++------------- .../Declaration/InstanceInterface.php | 10 +------ src/Internal/Declaration/MethodHandler.php | 28 +++++++++++++++++++ src/Internal/Declaration/WorkflowInstance.php | 8 ++---- .../Workflow/Process/DeferredGenerator.php | 7 +++-- src/Internal/Workflow/Process/Process.php | 6 ++-- src/Internal/Workflow/Process/Scope.php | 6 ++-- 7 files changed, 43 insertions(+), 46 deletions(-) create mode 100644 src/Internal/Declaration/MethodHandler.php diff --git a/src/Internal/Declaration/Instance.php b/src/Internal/Declaration/Instance.php index 8b047412e..a55366e04 100644 --- a/src/Internal/Declaration/Instance.php +++ b/src/Internal/Declaration/Instance.php @@ -11,20 +11,12 @@ namespace Temporal\Internal\Declaration; -use Temporal\DataConverter\ValuesInterface; use Temporal\Exception\InstantiationException; -use Temporal\Internal\Declaration\Dispatcher\AutowiredPayloads; use Temporal\Internal\Declaration\Prototype\Prototype; -/** - * @psalm-import-type DispatchableHandler from InstanceInterface - */ abstract class Instance implements InstanceInterface, Destroyable { - /** - * @var \Closure(ValuesInterface): mixed - */ - private \Closure $handler; + private MethodHandler $handler; public function __construct( Prototype $prototype, @@ -47,7 +39,7 @@ public function getContext(): object return $this->context; } - public function getHandler(): callable + public function getHandler(): MethodHandler { return $this->handler; } @@ -57,16 +49,8 @@ public function destroy(): void unset($this->handler, $this->context); } - /** - * @return \Closure(ValuesInterface): mixed - * - * @psalm-return DispatchableHandler - */ - protected function createHandler(\ReflectionFunctionAbstract $func): \Closure + protected function createHandler(\ReflectionFunctionAbstract $func): MethodHandler { - $valueMapper = new AutowiredPayloads($func); - - $context = $this->context; - return static fn(ValuesInterface $values): mixed => $valueMapper->dispatchValues($context, $values); + return new MethodHandler($this->context, $func); } } diff --git a/src/Internal/Declaration/InstanceInterface.php b/src/Internal/Declaration/InstanceInterface.php index d58c714db..708564512 100644 --- a/src/Internal/Declaration/InstanceInterface.php +++ b/src/Internal/Declaration/InstanceInterface.php @@ -11,17 +11,9 @@ namespace Temporal\Internal\Declaration; -use Temporal\DataConverter\ValuesInterface; - -/** - * @psalm-type DispatchableHandler = \Closure(ValuesInterface): mixed - */ interface InstanceInterface { - /** - * @return DispatchableHandler - */ - public function getHandler(): callable; + public function getHandler(): MethodHandler; public function getContext(): object; } diff --git a/src/Internal/Declaration/MethodHandler.php b/src/Internal/Declaration/MethodHandler.php new file mode 100644 index 000000000..d3758af98 --- /dev/null +++ b/src/Internal/Declaration/MethodHandler.php @@ -0,0 +1,28 @@ +dispatcher = new AutowiredPayloads($reflection); + } + + public function __invoke(ValuesInterface $values): mixed + { + return $this->dispatcher->dispatchValues($this->instance, $values); + } +} diff --git a/src/Internal/Declaration/WorkflowInstance.php b/src/Internal/Declaration/WorkflowInstance.php index 0cff3bb88..efc243092 100644 --- a/src/Internal/Declaration/WorkflowInstance.php +++ b/src/Internal/Declaration/WorkflowInstance.php @@ -22,7 +22,6 @@ use Temporal\Internal\Interceptor; /** - * @psalm-import-type DispatchableHandler from InstanceInterface * @psalm-type QueryHandler = \Closure(QueryInput): mixed * @psalm-type UpdateHandler = \Closure(UpdateInput, Deferred): PromiseInterface * @psalm-type ValidateUpdateHandler = \Closure(UpdateInput): void @@ -39,7 +38,7 @@ final class WorkflowInstance extends Instance implements WorkflowInstanceInterfa private array $queryHandlers = []; /** - * @var array + * @var array */ private array $signalHandlers = []; @@ -288,12 +287,9 @@ public function getPrototype(): WorkflowPrototype /** * Make a Closure from a callable. * - * @return \Closure(ValuesInterface): mixed * @throws \ReflectionException - * - * @psalm-return DispatchableHandler */ - protected function createCallableHandler(callable $handler): \Closure + protected function createCallableHandler(callable $handler): MethodHandler { return $this->createHandler( new \ReflectionFunction($handler instanceof \Closure ? $handler : \Closure::fromCallable($handler)), diff --git a/src/Internal/Workflow/Process/DeferredGenerator.php b/src/Internal/Workflow/Process/DeferredGenerator.php index 17d4a40cc..d6265662e 100644 --- a/src/Internal/Workflow/Process/DeferredGenerator.php +++ b/src/Internal/Workflow/Process/DeferredGenerator.php @@ -5,6 +5,7 @@ namespace Temporal\Internal\Workflow\Process; use Temporal\DataConverter\ValuesInterface; +use Temporal\Internal\Declaration\MethodHandler; /** * A wrapper around a generator that doesn't start the wrapped generator ASAP. @@ -23,15 +24,15 @@ final class DeferredGenerator implements \Iterator /** @var array<\Closure(\Throwable): mixed> */ private array $catchers = []; - private \Closure $handler; + private \Closure|MethodHandler $handler; private ValuesInterface $values; private function __construct() {} /** - * @param \Closure(ValuesInterface): mixed $handler + * @param MethodHandler|\Closure(ValuesInterface): mixed $handler */ - public static function fromHandler(\Closure $handler, ValuesInterface $values): self + public static function fromHandler(MethodHandler|\Closure $handler, ValuesInterface $values): self { $self = new self(); $self->handler = $handler; diff --git a/src/Internal/Workflow/Process/Process.php b/src/Internal/Workflow/Process/Process.php index 29cdca489..236a1f4d0 100644 --- a/src/Internal/Workflow/Process/Process.php +++ b/src/Internal/Workflow/Process/Process.php @@ -21,6 +21,7 @@ use Temporal\Interceptor\WorkflowInbound\SignalInput; use Temporal\Interceptor\WorkflowInbound\UpdateInput; use Temporal\Interceptor\WorkflowInboundCallsInterceptor; +use Temporal\Internal\Declaration\MethodHandler; use Temporal\Internal\Declaration\WorkflowInstance; use Temporal\Internal\Declaration\WorkflowInstanceInterface; use Temporal\Internal\ServiceContainer; @@ -172,10 +173,7 @@ function (\Throwable $e): void { ); } - /** - * @param \Closure(ValuesInterface): mixed $handler - */ - public function start(\Closure $handler, ?ValuesInterface $values, bool $deferred): void + public function start(MethodHandler $handler, ?ValuesInterface $values, bool $deferred): void { try { $this->makeCurrent(); diff --git a/src/Internal/Workflow/Process/Scope.php b/src/Internal/Workflow/Process/Scope.php index 1782f94b1..00769f7b0 100644 --- a/src/Internal/Workflow/Process/Scope.php +++ b/src/Internal/Workflow/Process/Scope.php @@ -21,6 +21,7 @@ use Temporal\Exception\InvalidArgumentException; use Temporal\Interceptor\WorkflowInbound\UpdateInput; use Temporal\Internal\Declaration\Destroyable; +use Temporal\Internal\Declaration\MethodHandler; use Temporal\Internal\ServiceContainer; use Temporal\Internal\Transport\Request\Cancel; use Temporal\Internal\Workflow\ScopeContext; @@ -123,10 +124,7 @@ public function getContext(): WorkflowContext return $this->context; } - /** - * @param \Closure(ValuesInterface): mixed $handler - */ - public function start(\Closure $handler, ?ValuesInterface $values, bool $deferred): void + public function start(MethodHandler $handler, ?ValuesInterface $values, bool $deferred): void { // Create a coroutine generator $this->coroutine = DeferredGenerator::fromHandler($handler, $values ?? EncodedValues::empty())