|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Philae\Middlewares; |
|
4
|
|
|
|
|
5
|
|
|
use Interop\Http\ServerMiddleware\DelegateInterface; |
|
6
|
|
|
use Middlewares\Utils\CallableHandler; |
|
7
|
|
|
use Middlewares\Utils\CallableResolver\CallableResolverInterface; |
|
8
|
|
|
use Philae\DefaultServiceProvider; |
|
9
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
10
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
|
11
|
|
|
|
|
12
|
|
|
class RequestHandler extends AbstractMiddleware |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* @var string Attribute name for handler reference |
|
16
|
|
|
*/ |
|
17
|
|
|
protected $handlerAttribute = 'request-handler'; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @var array Extra arguments passed to the handler |
|
21
|
|
|
*/ |
|
22
|
|
|
protected $arguments = []; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Set the attribute name to store handler reference. |
|
26
|
|
|
* |
|
27
|
|
|
* @param string $handlerAttribute |
|
28
|
|
|
* |
|
29
|
|
|
* @return $this |
|
30
|
|
|
*/ |
|
31
|
|
|
public function handlerAttribute(string $handlerAttribute) |
|
32
|
|
|
{ |
|
33
|
|
|
$this->handlerAttribute = $handlerAttribute; |
|
34
|
|
|
|
|
35
|
|
|
return $this; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Extra arguments passed to the handler. |
|
40
|
|
|
* |
|
41
|
|
|
* @return $this |
|
42
|
|
|
*/ |
|
43
|
21 |
|
public function arguments(...$args) |
|
44
|
|
|
{ |
|
45
|
21 |
|
$this->arguments = $args; |
|
46
|
|
|
|
|
47
|
21 |
|
return $this; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Process a server request and return a response. |
|
52
|
|
|
* |
|
53
|
|
|
* @param ServerRequestInterface $request |
|
54
|
|
|
* @param DelegateInterface $delegate |
|
55
|
|
|
* |
|
56
|
|
|
* @return ResponseInterface |
|
57
|
|
|
*/ |
|
58
|
15 |
|
public function process(ServerRequestInterface $request, DelegateInterface $delegate) |
|
59
|
|
|
{ |
|
60
|
15 |
|
$arguments = array_merge([$request], $this->arguments); |
|
61
|
|
|
|
|
62
|
15 |
|
$handler = $request->getAttribute($this->handlerAttribute); |
|
63
|
|
|
/** @var CallableResolverInterface $resolver */ |
|
64
|
15 |
|
$resolver = $this->getContainer()->get(DefaultServiceProvider::SERVICE_CALLABLE_RESOLVER); |
|
65
|
15 |
|
$callable = $resolver->resolve($handler, $arguments); |
|
66
|
|
|
|
|
67
|
15 |
|
return CallableHandler::execute($callable, $arguments); |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
|