-
-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathHandlerResolver.php
More file actions
145 lines (122 loc) · 4.78 KB
/
Copy pathHandlerResolver.php
File metadata and controls
145 lines (122 loc) · 4.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
<?php
declare(strict_types=1);
namespace Yiisoft\Queue\Message\Handler;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\ContainerInterface;
use Yiisoft\Injector\Injector;
use Yiisoft\Queue\Message\MessageInterface;
use Yiisoft\Queue\Middleware\CallableFactory;
use Yiisoft\Queue\Middleware\InvalidCallableConfigurationException;
use function array_key_exists;
use function is_callable;
use function is_string;
use function sprintf;
/**
* Resolves message handlers from configuration, a DI container, or a callable factory.
*/
final class HandlerResolver
{
/**
* @var HandlerInterface[] Cache of resolved handlers.
* @psalm-var array<non-empty-string, HandlerInterface>
*/
private array $cache = [];
private readonly Injector $injector;
private readonly CallableFactory $callableFactory;
/**
* @param (array|callable|HandlerInterface|string)[] $handlers Handler definitions indexed by message type.
* @param ContainerInterface $container Container used to resolve handlers.
* @param ContainerInterface|null $callableDependencyContainer Container used to resolve callable handler
* dependencies. If not set, the main container is used.
*
* @psalm-param array<non-empty-string, array|callable|HandlerInterface|string> $handlers
*/
public function __construct(
private readonly array $handlers,
private readonly ContainerInterface $container,
?ContainerInterface $callableDependencyContainer = null,
) {
$this->injector = new Injector($callableDependencyContainer ?? $this->container);
$this->callableFactory = new CallableFactory($this->container);
}
/**
* Get a handler for the given message type.
*
* @param string $messageType Message type. Must be a non-empty string.
*
* @psalm-param non-empty-string $messageType
*
* @throws HandlerNotFoundException If no handler exists for the message type.
* @throws InvalidHandlerConfigurationException If the handler definition is configured incorrectly.
* @throws ContainerExceptionInterface Error while retrieving the entry from container.
*/
public function resolve(string $messageType): HandlerInterface
{
if (array_key_exists($messageType, $this->cache)) {
return $this->cache[$messageType];
}
$this->cache[$messageType] = $this->internalResolve($messageType);
return $this->cache[$messageType];
}
/**
* @throws HandlerNotFoundException
* @throws InvalidHandlerConfigurationException
* @throws ContainerExceptionInterface
*/
private function internalResolve(string $messageType): HandlerInterface
{
$definition = $this->handlers[$messageType] ?? $messageType;
if ($definition instanceof HandlerInterface) {
return $definition;
}
if (is_callable($definition)) {
return $this->createCallableHandler($messageType, $definition);
}
if (is_string($definition)) {
return $this->getHandlerFromContainer($messageType, $definition);
}
return $this->createCallableHandler($messageType, $definition);
}
/**
* @throws HandlerNotFoundException
* @throws InvalidHandlerConfigurationException
* @throws ContainerExceptionInterface
*/
private function getHandlerFromContainer(string $messageType, string $id): HandlerInterface
{
if (!$this->container->has($id)) {
throw new HandlerNotFoundException($messageType);
}
$handler = $this->container->get($id);
if ($handler instanceof HandlerInterface) {
return $handler;
}
if (is_callable($handler)) {
return $this->createCallableHandler($messageType, $handler);
}
throw new InvalidHandlerConfigurationException(
$messageType,
sprintf(
'Resolved from container handler should be an instance of "%s" or callable, got "%s".',
HandlerInterface::class,
get_debug_type($handler),
),
);
}
/**
* @throws InvalidHandlerConfigurationException
* @throws ContainerExceptionInterface
*/
private function createCallableHandler(string $messageType, mixed $definition): CallableHandler
{
try {
$callable = $this->callableFactory->create($definition);
} catch (InvalidCallableConfigurationException $exception) {
throw new InvalidHandlerConfigurationException($messageType, $exception->getMessage(), $exception);
}
$callable = function (MessageInterface $message) use ($callable): void {
$this->injector->invoke($callable, [$message]);
};
return new CallableHandler($callable);
}
}