1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
/* |
3
|
|
|
* This file is part of the KleijnWeb\PhpApi\Descriptions package. |
4
|
|
|
* |
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
6
|
|
|
* file that was distributed with this source code. |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace KleijnWeb\PhpApi\Middleware; |
10
|
|
|
|
11
|
|
|
use Interop\Http\ServerMiddleware\MiddlewareInterface; |
12
|
|
|
use KleijnWeb\PhpApi\Descriptions\Description\Description; |
13
|
|
|
use KleijnWeb\PhpApi\Descriptions\Description\Operation; |
14
|
|
|
use KleijnWeb\PhpApi\Descriptions\Description\Path; |
15
|
|
|
use Middlewares\Utils\Factory\StreamFactory; |
16
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
17
|
|
|
use Zend\Diactoros\Stream; |
18
|
|
|
|
19
|
|
|
abstract class PhpApiMiddleware implements MiddlewareInterface |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var StreamFactory |
23
|
|
|
*/ |
24
|
|
|
private $streamFactory; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @param string $string |
28
|
|
|
* @return Stream |
29
|
|
|
*/ |
30
|
|
|
protected function createStringStream(string $string): Stream |
31
|
|
|
{ |
32
|
|
|
$stream = $this->getStreamFactory()->createStream($string); |
33
|
|
|
$stream->rewind(); |
34
|
|
|
|
35
|
|
|
return $stream; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param ServerRequestInterface $request |
40
|
|
|
* @return Description |
41
|
|
|
*/ |
42
|
|
|
protected function getDescription(ServerRequestInterface $request): Description |
43
|
|
|
{ |
44
|
|
|
return $this->getMeta($request)->getDescription(); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param ServerRequestInterface $request |
49
|
|
|
* @return Path |
50
|
|
|
*/ |
51
|
|
|
protected function getPath(ServerRequestInterface $request): Path |
52
|
|
|
{ |
53
|
|
|
return $this->getMeta($request)->getPath(); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param ServerRequestInterface $request |
58
|
|
|
* @return Operation |
59
|
|
|
*/ |
60
|
|
|
protected function getOperation(ServerRequestInterface $request): Operation |
61
|
|
|
{ |
62
|
|
|
return $this->getMeta($request)->getOperation(); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @return StreamFactory |
67
|
|
|
*/ |
68
|
|
|
protected function getStreamFactory(): StreamFactory |
69
|
|
|
{ |
70
|
|
|
if (!$this->streamFactory) { |
71
|
|
|
$this->streamFactory = new StreamFactory(); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
return $this->streamFactory; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param ServerRequestInterface $request |
79
|
|
|
* @return Meta |
80
|
|
|
*/ |
81
|
|
|
private function getMeta(ServerRequestInterface $request): Meta |
82
|
|
|
{ |
83
|
|
|
return Meta::getFromRequest($request); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|