From 1859b0ff3ecf91cae4d2dc03381a45c9ab9d2c2d 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 | 5 ++-- src/Internal/Workflow/Process/Scope.php | 7 +++-- 7 files changed, 46 insertions(+), 43 deletions(-) create mode 100644 src/Internal/Declaration/MethodHandler.php diff --git a/src/Internal/Declaration/Instance.php b/src/Internal/Declaration/Instance.php index 8b047412..a55366e0 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 d58c714d..70856451 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 00000000..d3758af9 --- /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 0cff3bb8..efc24309 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 17d4a40c..d6265662 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 29cdca48..ad6ae41b 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; @@ -173,9 +174,9 @@ function (\Throwable $e): void { } /** - * @param \Closure(ValuesInterface): mixed $handler + * @param MethodHandler|\Closure(ValuesInterface): mixed $handler */ - public function start(\Closure $handler, ?ValuesInterface $values, bool $deferred): void + public function start(MethodHandler|\Closure $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 1782f94b..e432e6ff 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; @@ -124,9 +125,9 @@ public function getContext(): WorkflowContext } /** - * @param \Closure(ValuesInterface): mixed $handler + * @param MethodHandler|\Closure(ValuesInterface): mixed $handler */ - public function start(\Closure $handler, ?ValuesInterface $values, bool $deferred): void + public function start(MethodHandler|\Closure $handler, ?ValuesInterface $values, bool $deferred): void { // Create a coroutine generator $this->coroutine = DeferredGenerator::fromHandler($handler, $values ?? EncodedValues::empty()) @@ -233,7 +234,7 @@ public function cancel(?\Throwable $reason = null): void public function startScope(callable $handler, bool $detached, ?string $layer = null): CancellationScopeInterface { $scope = $this->createScope($detached, $layer); - $scope->start($handler, null, false); + $scope->start($handler(...), null, false); return $scope; }