|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of Guzzle HTTP JSON-RPC |
|
5
|
|
|
* |
|
6
|
|
|
* Copyright (c) 2014 Nature Delivered Ltd. <http://graze.com> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
* |
|
11
|
|
|
* @see http://github.com/graze/guzzle-jsonrpc/blob/master/LICENSE |
|
12
|
|
|
* @link http://github.com/graze/guzzle-jsonrpc |
|
13
|
|
|
*/ |
|
14
|
|
|
|
|
15
|
|
|
namespace Graze\GuzzleHttp\JsonRpc\Middleware; |
|
16
|
|
|
|
|
17
|
|
|
use Psr\Http\Message\RequestInterface as HttpRequestInterface; |
|
18
|
|
|
use Psr\Http\Message\ResponseInterface as HttpResponseInterface; |
|
19
|
|
|
|
|
20
|
|
|
abstract class AbstractMiddleware |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* @param callable $fn |
|
24
|
|
|
* |
|
25
|
|
|
* @return \Closure |
|
26
|
|
|
*/ |
|
27
|
|
|
public function __invoke(callable $fn) |
|
28
|
|
|
{ |
|
29
|
|
|
return function (HttpRequestInterface $request, array $options) use ($fn) { |
|
30
|
|
|
return $fn(call_user_func([$this, 'applyRequest'], $request, $options), $options)->then( |
|
31
|
|
|
function (HttpResponseInterface $response) use ($request, $options) { |
|
32
|
|
|
return call_user_func([$this, 'applyResponse'], $request, $response, $options); |
|
33
|
|
|
} |
|
34
|
|
|
); |
|
35
|
|
|
}; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @param HttpRequestInterface $request |
|
40
|
|
|
* @param array $options |
|
41
|
|
|
* |
|
42
|
|
|
* @return HttpRequestInterface |
|
43
|
|
|
*/ |
|
44
|
|
|
public function applyRequest(HttpRequestInterface $request, array $options) |
|
45
|
|
|
{ |
|
46
|
|
|
return $request; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @param HttpRequestInterface $request |
|
51
|
|
|
* @param HttpResponseInterface $response |
|
52
|
|
|
* @param array $options |
|
53
|
|
|
* |
|
54
|
|
|
* @return HttpResponseInterface |
|
55
|
|
|
*/ |
|
56
|
|
|
public function applyResponse(HttpRequestInterface $request, HttpResponseInterface $response, array $options) |
|
57
|
|
|
{ |
|
58
|
|
|
return $response; |
|
59
|
|
|
} |
|
60
|
|
|
} |
|
61
|
|
|
|