|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Yiisoft\Router; |
|
6
|
|
|
|
|
7
|
|
|
use InvalidArgumentException; |
|
8
|
|
|
use Psr\Container\ContainerInterface; |
|
9
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
10
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
|
11
|
|
|
use Psr\Http\Server\MiddlewareInterface; |
|
12
|
|
|
use Psr\Http\Server\RequestHandlerInterface; |
|
13
|
|
|
use Yiisoft\Injector\Injector; |
|
14
|
|
|
|
|
15
|
|
|
final class MiddlewareFactory implements MiddlewareFactoryInterface |
|
16
|
|
|
{ |
|
17
|
|
|
private ContainerInterface $container; |
|
18
|
|
|
|
|
19
|
25 |
|
public function __construct(ContainerInterface $container) |
|
20
|
|
|
{ |
|
21
|
25 |
|
$this->container = $container; |
|
22
|
25 |
|
} |
|
23
|
|
|
|
|
24
|
22 |
|
public function create($middlewareDefinition): MiddlewareInterface |
|
25
|
|
|
{ |
|
26
|
22 |
|
return $this->createMiddleware($middlewareDefinition); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @param callable|string|array $middlewareDefinition |
|
31
|
|
|
* @return MiddlewareInterface |
|
32
|
|
|
*/ |
|
33
|
22 |
|
private function createMiddleware($middlewareDefinition): MiddlewareInterface |
|
34
|
|
|
{ |
|
35
|
22 |
|
$this->validateMiddleware($middlewareDefinition); |
|
36
|
|
|
|
|
37
|
15 |
|
if (is_string($middlewareDefinition)) { |
|
38
|
1 |
|
return $this->container->get($middlewareDefinition); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
14 |
|
return $this->wrapCallable($middlewareDefinition); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
14 |
|
private function wrapCallable($callback): MiddlewareInterface |
|
45
|
|
|
{ |
|
46
|
14 |
|
if (is_array($callback) && !is_object($callback[0])) { |
|
47
|
4 |
|
[$controller, $action] = $callback; |
|
48
|
4 |
|
return new class($controller, $action, $this->container) implements MiddlewareInterface { |
|
49
|
|
|
private string $class; |
|
50
|
|
|
private string $method; |
|
51
|
|
|
private ContainerInterface $container; |
|
52
|
|
|
|
|
53
|
|
|
public function __construct(string $class, string $method, ContainerInterface $container) |
|
54
|
|
|
{ |
|
55
|
4 |
|
$this->class = $class; |
|
56
|
4 |
|
$this->method = $method; |
|
57
|
4 |
|
$this->container = $container; |
|
58
|
4 |
|
} |
|
59
|
|
|
|
|
60
|
|
|
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface |
|
61
|
|
|
{ |
|
62
|
3 |
|
$controller = $this->container->get($this->class); |
|
63
|
3 |
|
return (new Injector($this->container))->invoke([$controller, $this->method], [$request, $handler]); |
|
64
|
|
|
} |
|
65
|
|
|
}; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
10 |
|
return new class($callback, $this->container) implements MiddlewareInterface { |
|
69
|
|
|
private ContainerInterface $container; |
|
70
|
|
|
private $callback; |
|
71
|
|
|
|
|
72
|
|
|
public function __construct(callable $callback, ContainerInterface $container) |
|
73
|
|
|
{ |
|
74
|
10 |
|
$this->callback = $callback; |
|
75
|
10 |
|
$this->container = $container; |
|
76
|
10 |
|
} |
|
77
|
|
|
|
|
78
|
|
|
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface |
|
79
|
|
|
{ |
|
80
|
9 |
|
$response = (new Injector($this->container))->invoke($this->callback, [$request, $handler]); |
|
81
|
9 |
|
return $response instanceof MiddlewareInterface ? $response->process($request, $handler) : $response; |
|
82
|
|
|
} |
|
83
|
|
|
}; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* @param callable|string|array $middlewareDefinition |
|
88
|
|
|
*/ |
|
89
|
22 |
|
private function validateMiddleware($middlewareDefinition): void |
|
90
|
|
|
{ |
|
91
|
22 |
|
if (is_string($middlewareDefinition) && is_subclass_of($middlewareDefinition, MiddlewareInterface::class)) { |
|
92
|
1 |
|
return; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
21 |
|
if ($this->isCallable($middlewareDefinition) && (!is_array($middlewareDefinition) || !is_object($middlewareDefinition[0]))) { |
|
96
|
14 |
|
return; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
7 |
|
throw new InvalidArgumentException('Parameter should be either PSR middleware class name or a callable.'); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
21 |
|
private function isCallable($definition): bool |
|
103
|
|
|
{ |
|
104
|
21 |
|
if (is_callable($definition)) { |
|
105
|
15 |
|
return is_object($definition) |
|
106
|
11 |
|
? !$definition instanceof MiddlewareInterface |
|
107
|
15 |
|
: true; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
6 |
|
return is_array($definition) |
|
111
|
6 |
|
&& array_keys($definition) === [0, 1] |
|
112
|
1 |
|
&& in_array( |
|
113
|
1 |
|
$definition[1], |
|
114
|
1 |
|
class_exists($definition[0]) ? get_class_methods($definition[0]) : [], |
|
115
|
6 |
|
true |
|
116
|
|
|
); |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
|