|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of the tarantool/client package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Eugene Leonovich <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
declare(strict_types=1); |
|
13
|
|
|
|
|
14
|
|
|
namespace Tarantool\Client\Handler; |
|
15
|
|
|
|
|
16
|
|
|
use Tarantool\Client\Connection\Connection; |
|
17
|
|
|
use Tarantool\Client\Middleware\Middleware; |
|
18
|
|
|
use Tarantool\Client\Packer\Packer; |
|
19
|
|
|
use Tarantool\Client\Request\Request; |
|
20
|
|
|
use Tarantool\Client\Response; |
|
21
|
|
|
|
|
22
|
|
|
final class MiddlewareHandler implements Handler |
|
23
|
|
|
{ |
|
24
|
|
|
/** @var Handler */ |
|
25
|
|
|
private $handler; |
|
26
|
|
|
|
|
27
|
|
|
/** @var Middleware[] */ |
|
28
|
|
|
private $middleware; |
|
29
|
|
|
|
|
30
|
|
|
/** @var int */ |
|
31
|
|
|
private $index = 0; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @param Handler $handler |
|
35
|
|
|
* @param Middleware[] $middleware |
|
36
|
|
|
*/ |
|
37
|
75 |
|
private function __construct($handler, $middleware) |
|
38
|
|
|
{ |
|
39
|
75 |
|
$this->handler = $handler; |
|
40
|
75 |
|
$this->middleware = $middleware; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @param Middleware[] $middleware |
|
45
|
|
|
*/ |
|
46
|
72 |
|
public static function append(Handler $handler, array $middleware) : Handler |
|
47
|
|
|
{ |
|
48
|
72 |
|
if (!$handler instanceof self) { |
|
49
|
72 |
|
return new self($handler, $middleware); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
6 |
|
$handler = clone $handler; |
|
53
|
6 |
|
$handler->middleware = \array_merge($handler->middleware, $middleware); |
|
54
|
|
|
|
|
55
|
6 |
|
return $handler; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @param Middleware[] $middleware |
|
60
|
|
|
*/ |
|
61
|
6 |
|
public static function prepend(Handler $handler, array $middleware) : Handler |
|
62
|
|
|
{ |
|
63
|
6 |
|
if (!$handler instanceof self) { |
|
64
|
3 |
|
return new self($handler, $middleware); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
6 |
|
$handler = clone $handler; |
|
68
|
6 |
|
$handler->middleware = \array_merge($middleware, $handler->middleware); |
|
69
|
|
|
|
|
70
|
6 |
|
return $handler; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
75 |
|
public function handle(Request $request) : Response |
|
74
|
|
|
{ |
|
75
|
75 |
|
if (!isset($this->middleware[$this->index])) { |
|
76
|
60 |
|
return $this->handler->handle($request); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
75 |
|
$new = clone $this; |
|
80
|
75 |
|
++$new->index; |
|
81
|
|
|
|
|
82
|
75 |
|
return $this->middleware[$this->index]->process($request, $new); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
39 |
|
public function getConnection() : Connection |
|
86
|
|
|
{ |
|
87
|
39 |
|
return $this->handler->getConnection(); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
3 |
|
public function getPacker() : Packer |
|
91
|
|
|
{ |
|
92
|
3 |
|
return $this->handler->getPacker(); |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|