Skip to content

Commit

Permalink
Replace handler callable with MethodHandler class
Browse files Browse the repository at this point in the history
  • Loading branch information
roxblnfk committed Jan 10, 2025
1 parent bf33938 commit 77c724c
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 46 deletions.
24 changes: 4 additions & 20 deletions src/Internal/Declaration/Instance.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -47,7 +39,7 @@ public function getContext(): object
return $this->context;
}

public function getHandler(): callable
public function getHandler(): MethodHandler
{
return $this->handler;
}
Expand All @@ -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);
}
}
10 changes: 1 addition & 9 deletions src/Internal/Declaration/InstanceInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
28 changes: 28 additions & 0 deletions src/Internal/Declaration/MethodHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace Temporal\Internal\Declaration;

use Temporal\DataConverter\ValuesInterface;
use Temporal\Internal\Declaration\Dispatcher\AutowiredPayloads;

/**
* @internal
*/
final class MethodHandler
{
private readonly AutowiredPayloads $dispatcher;

public function __construct(
private readonly object $instance,
\ReflectionFunctionAbstract $reflection,
) {
$this->dispatcher = new AutowiredPayloads($reflection);
}

public function __invoke(ValuesInterface $values): mixed
{
return $this->dispatcher->dispatchValues($this->instance, $values);
}
}
8 changes: 2 additions & 6 deletions src/Internal/Declaration/WorkflowInstance.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -39,7 +38,7 @@ final class WorkflowInstance extends Instance implements WorkflowInstanceInterfa
private array $queryHandlers = [];

/**
* @var array<non-empty-string, DispatchableHandler>
* @var array<non-empty-string, MethodHandler>
*/
private array $signalHandlers = [];

Expand Down Expand Up @@ -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)),
Expand Down
7 changes: 4 additions & 3 deletions src/Internal/Workflow/Process/DeferredGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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;
Expand Down
6 changes: 2 additions & 4 deletions src/Internal/Workflow/Process/Process.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down
6 changes: 2 additions & 4 deletions src/Internal/Workflow/Process/Scope.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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())
Expand Down

0 comments on commit 77c724c

Please sign in to comment.