|
1
|
|
|
<?php |
|
2
|
|
|
namespace Shlinkio\Shlink\Rest\Action; |
|
3
|
|
|
|
|
4
|
|
|
use Psr\Http\Message\ResponseInterface as Response; |
|
5
|
|
|
use Psr\Http\Message\ServerRequestInterface as Request; |
|
6
|
|
|
use Psr\Log\LoggerInterface; |
|
7
|
|
|
use Psr\Log\NullLogger; |
|
8
|
|
|
use Zend\Stratigility\MiddlewareInterface; |
|
9
|
|
|
|
|
10
|
|
|
abstract class AbstractRestAction implements MiddlewareInterface |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* @var LoggerInterface |
|
14
|
|
|
*/ |
|
15
|
|
|
protected $logger; |
|
16
|
|
|
|
|
17
|
14 |
|
public function __construct(LoggerInterface $logger = null) |
|
18
|
|
|
{ |
|
19
|
14 |
|
$this->logger = $logger ?: new NullLogger(); |
|
20
|
14 |
|
} |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Process an incoming request and/or response. |
|
24
|
|
|
* |
|
25
|
|
|
* Accepts a server-side request and a response instance, and does |
|
26
|
|
|
* something with them. |
|
27
|
|
|
* |
|
28
|
|
|
* If the response is not complete and/or further processing would not |
|
29
|
|
|
* interfere with the work done in the middleware, or if the middleware |
|
30
|
|
|
* wants to delegate to another process, it can use the `$out` callable |
|
31
|
|
|
* if present. |
|
32
|
|
|
* |
|
33
|
|
|
* If the middleware does not return a value, execution of the current |
|
34
|
|
|
* request is considered complete, and the response instance provided will |
|
35
|
|
|
* be considered the response to return. |
|
36
|
|
|
* |
|
37
|
|
|
* Alternately, the middleware may return a response instance. |
|
38
|
|
|
* |
|
39
|
|
|
* Often, middleware will `return $out();`, with the assumption that a |
|
40
|
|
|
* later middleware will return a response. |
|
41
|
|
|
* |
|
42
|
|
|
* @param Request $request |
|
43
|
|
|
* @param Response $response |
|
44
|
|
|
* @param null|callable $out |
|
45
|
|
|
* @return null|Response |
|
46
|
|
|
*/ |
|
47
|
17 |
|
public function __invoke(Request $request, Response $response, callable $out = null) |
|
48
|
|
|
{ |
|
49
|
17 |
|
if ($request->getMethod() === 'OPTIONS') { |
|
50
|
|
|
return $response; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
17 |
|
return $this->dispatch($request, $response, $out); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @param Request $request |
|
58
|
|
|
* @param Response $response |
|
59
|
|
|
* @param callable|null $out |
|
60
|
|
|
* @return null|Response |
|
61
|
|
|
*/ |
|
62
|
|
|
abstract protected function dispatch(Request $request, Response $response, callable $out = null); |
|
63
|
|
|
} |
|
64
|
|
|
|