-
-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathFailureMiddlewareFactory.php
More file actions
89 lines (77 loc) · 3.34 KB
/
Copy pathFailureMiddlewareFactory.php
File metadata and controls
89 lines (77 loc) · 3.34 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
<?php
declare(strict_types=1);
namespace Yiisoft\Queue\Middleware\FailureHandling;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\ContainerInterface;
use Psr\Container\NotFoundExceptionInterface;
use Yiisoft\Injector\Injector;
use Yiisoft\Queue\Middleware\InvalidMiddlewareDefinitionException;
use Yiisoft\Queue\Middleware\MiddlewareFactory;
/**
* Creates a middleware based on the definition provided.
*
* @template-extends MiddlewareFactory<FailureMiddlewareInterface>
*/
final class FailureMiddlewareFactory extends MiddlewareFactory implements FailureMiddlewareFactoryInterface
{
/**
* @param array|callable|FailureMiddlewareInterface|string $middlewareDefinition Middleware definition in one of
* the following formats:
*
* - A middleware object.
* - A name of a middleware class. The middleware instance will be obtained from container and executed.
* - A callable with `function(ServerRequestInterface $request, RequestHandlerInterface $handler):
* ResponseInterface` signature.
* - A controller handler action in format `[TestController::class, 'index']`. `TestController` instance will
* be created and `index()` method will be executed.
* - A function returning a middleware. The middleware returned will be executed.
*
* For handler action and callable
* typed parameters are automatically injected using dependency injection container.
* Current request and handler could be obtained by type-hinting for {@see ServerRequestInterface}
* and {@see RequestHandlerInterface}.
*
* @throws NotFoundExceptionInterface
* @throws ContainerExceptionInterface
* @throws InvalidMiddlewareDefinitionException
*
* @return FailureMiddlewareInterface
*/
public function createFailureMiddleware(
FailureMiddlewareInterface|callable|array|string $middlewareDefinition,
): FailureMiddlewareInterface {
if ($middlewareDefinition instanceof FailureMiddlewareInterface) {
return $middlewareDefinition;
}
$middleware = $this->create($middlewareDefinition);
return $middleware;
}
protected function getInterfaceName(): string
{
return FailureMiddlewareInterface::class;
}
protected function wrapMiddleware(callable $callback): FailureMiddlewareInterface
{
$container = $this->container;
return new class ($callback, $container) implements FailureMiddlewareInterface {
private $callback;
public function __construct(
callable $callback,
private readonly ContainerInterface $container,
) {
$this->callback = $callback;
}
public function processFailure(FailureHandlingRequest $request, FailureHandlerInterface $handler): FailureHandlingRequest
{
$response = (new Injector($this->container))->invoke($this->callback, [$request, $handler]);
if ($response instanceof FailureHandlingRequest) {
return $response;
}
if ($response instanceof FailureMiddlewareInterface) {
return $response->processFailure($request, $handler);
}
throw new InvalidMiddlewareDefinitionException($this->callback);
}
};
}
}