|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Psr7Middlewares\Middleware; |
|
4
|
|
|
|
|
5
|
|
|
use Psr7Middlewares\Utils; |
|
6
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
|
7
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Middleware to generate UUID on each request. |
|
11
|
|
|
*/ |
|
12
|
|
|
class Uuid |
|
13
|
|
|
{ |
|
14
|
|
|
use Utils\AttributeTrait; |
|
15
|
|
|
|
|
16
|
|
|
const KEY = 'UUID'; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @var string|false The header name to use |
|
20
|
|
|
*/ |
|
21
|
|
|
private $header = 'X-Uuid'; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @var array The version and arguments needed to generate the Uuid |
|
25
|
|
|
*/ |
|
26
|
|
|
private $version = [1]; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Returns the Uuid instance. |
|
30
|
|
|
* |
|
31
|
|
|
* @param ServerRequestInterface $request |
|
32
|
|
|
* |
|
33
|
|
|
* @return \Ramsey\Uuid\Uuid|null |
|
34
|
|
|
*/ |
|
35
|
|
|
public static function getUuid(ServerRequestInterface $request) |
|
36
|
|
|
{ |
|
37
|
|
|
return self::getAttribute($request, self::KEY); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Constructor. Set the version of UUID. |
|
42
|
|
|
* |
|
43
|
|
|
* @param int|null $version |
|
44
|
|
|
*/ |
|
45
|
|
|
public function __construct($version = null) |
|
46
|
|
|
{ |
|
47
|
|
|
if ($version !== null) { |
|
48
|
|
|
call_user_func_array([$this, 'version'], func_get_args()); |
|
49
|
|
|
} |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Choose the Uuid version. |
|
54
|
|
|
* |
|
55
|
|
|
* @param int $version 1, 3, 4 or 5 |
|
56
|
|
|
* |
|
57
|
|
|
* @return self |
|
58
|
|
|
*/ |
|
59
|
|
|
public function version($version) |
|
60
|
|
|
{ |
|
61
|
|
|
if (!in_array($version, [1, 3, 4, 5])) { |
|
62
|
|
|
throw new \InvalidArgumentException('Only 1, 3, 4 and 5 versions are available'); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
$this->version = func_get_args(); |
|
66
|
|
|
|
|
67
|
|
|
return $this; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Set whether the Uuid is stored in the header. |
|
72
|
|
|
* Set false to do not store. |
|
73
|
|
|
* |
|
74
|
|
|
* @param false|string $header |
|
75
|
|
|
* |
|
76
|
|
|
* @return self |
|
77
|
|
|
*/ |
|
78
|
|
|
public function header($header) |
|
79
|
|
|
{ |
|
80
|
|
|
$this->header = $header; |
|
81
|
|
|
|
|
82
|
|
|
return $this; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Execute the middleware. |
|
87
|
|
|
* |
|
88
|
|
|
* @param ServerRequestInterface $request |
|
89
|
|
|
* @param ResponseInterface $response |
|
90
|
|
|
* @param callable $next |
|
91
|
|
|
* |
|
92
|
|
|
* @return ResponseInterface |
|
93
|
|
|
*/ |
|
94
|
|
|
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next) |
|
95
|
|
|
{ |
|
96
|
|
|
$uuid = $this->generateUuid(); |
|
97
|
|
|
|
|
98
|
|
|
$request = self::setAttribute($request, self::KEY, $uuid); |
|
99
|
|
|
|
|
100
|
|
|
if (empty($this->header)) { |
|
101
|
|
|
return $next($request, $response); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
$request = $request->withHeader($this->header, (string) $uuid); |
|
105
|
|
|
|
|
106
|
|
|
return $next($request, $response)->withHeader($this->header, (string) $uuid); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* Generate the uuid with the current configuration. |
|
111
|
|
|
* |
|
112
|
|
|
* @return string |
|
113
|
|
|
*/ |
|
114
|
|
|
private function generateUuid() |
|
115
|
|
|
{ |
|
116
|
|
|
$args = $this->version; |
|
117
|
|
|
$fn = 'uuid'.array_shift($args); |
|
118
|
|
|
|
|
119
|
|
|
return call_user_func_array('Ramsey\Uuid\Uuid::'.$fn, $args); |
|
120
|
|
|
} |
|
121
|
|
|
} |
|
122
|
|
|
|