|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
namespace hiapi\Core\Http\Psr15\Middleware; |
|
5
|
|
|
|
|
6
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
|
7
|
|
|
use hiapi\Core\commands\CommandFactory; |
|
8
|
|
|
use hiapi\Core\Endpoint\EndpointProcessor; |
|
9
|
|
|
use hiapi\Core\Endpoint\EndpointRepository; |
|
10
|
|
|
use hiapi\Core\Http\Psr7\Response\FatResponse; |
|
11
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
12
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
|
13
|
|
|
use Psr\Http\Server\MiddlewareInterface; |
|
14
|
|
|
use Psr\Http\Server\RequestHandlerInterface; |
|
15
|
|
|
use yii\base\Arrayable; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Complete endpoint execution: |
|
19
|
|
|
* - resolve endpoint with EndpointRepository |
|
20
|
|
|
* - create command with CommandFactory |
|
21
|
|
|
* - run endpoint with EndpointProcessor |
|
22
|
|
|
* The middleware can be bound to single command with `endpointName`. |
|
23
|
|
|
*/ |
|
24
|
|
|
class EndpointMiddleware implements MiddlewareInterface |
|
25
|
|
|
{ |
|
26
|
|
|
private string $endpointName; |
|
|
|
|
|
|
27
|
|
|
private CommandFactory $commandFactory; |
|
28
|
|
|
private EndpointRepository $endpointRepository; |
|
29
|
|
|
private EndpointProcessor $endpointProcessor; |
|
30
|
|
|
|
|
31
|
|
|
public function __construct( |
|
32
|
|
|
string $enpointName = '', |
|
33
|
|
|
CommandFactory $commandFactory, |
|
34
|
|
|
EndpointRepository $endpointRepository, |
|
35
|
|
|
EndpointProcessor $endpointProcessor |
|
36
|
|
|
) { |
|
37
|
|
|
$this->endpointName = $enpointName; |
|
38
|
|
|
$this->commandFactory = $commandFactory; |
|
39
|
|
|
$this->endpointRepository = $endpointRepository; |
|
40
|
|
|
$this->endpointProcessor = $endpointProcessor; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
public static function for(string $name): \Closure |
|
44
|
|
|
{ |
|
45
|
|
|
return function ( |
|
46
|
|
|
ServerRequestInterface $request, RequestHandlerInterface $handler, |
|
47
|
|
|
CommandFactory $commandFactory, |
|
48
|
|
|
EndpointRepository $endpointRepository, |
|
49
|
|
|
EndpointProcessor $endpointProcessor |
|
50
|
|
|
) use ($name) { |
|
51
|
|
|
$middleware = new self($name, $commandFactory, $endpointRepository, $endpointProcessor); |
|
52
|
|
|
|
|
53
|
|
|
return $middleware->process($request, $handler); |
|
54
|
|
|
}; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface |
|
58
|
|
|
{ |
|
59
|
|
|
$endpoint = $this->endpointRepository->getByName($this->getName($request)); |
|
60
|
|
|
$command = $this->commandFactory->createByEndpoint($endpoint, $request); |
|
61
|
|
|
$result = $this->endpointProcessor->__invoke($command, $endpoint); |
|
62
|
|
|
|
|
63
|
|
|
if ($result instanceof ResponseInterface) { |
|
64
|
|
|
return $result; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
return FatResponse::create($this->transformResult($result), $request); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
private function getName(ServerRequestInterface $request): string |
|
71
|
|
|
{ |
|
72
|
|
|
return $this->endpointName ?: trim($request->getUri()->getPath(), '/'); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* TODO: Re-implement as a ResourceTransformer, that can serialize Result to an Http\Response |
|
77
|
|
|
* It should be useful to serialize file to StreamResponse, or search result to Reponse with |
|
78
|
|
|
* pagination headers. |
|
79
|
|
|
* |
|
80
|
|
|
* @param array|ArrayCollection|object|mixed $result |
|
81
|
|
|
* @return array|string|null|boolean |
|
82
|
|
|
*/ |
|
83
|
|
|
private function transformResult($result) |
|
84
|
|
|
{ |
|
85
|
|
|
if ($result instanceof ArrayCollection) { |
|
86
|
|
|
return array_map(function ($item) { |
|
87
|
|
|
return $this->transformResult($item); |
|
88
|
|
|
}, $result->toArray()); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
if ($result instanceof Arrayable) { |
|
92
|
|
|
return $result->toArray(); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
return $result; |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
|