|
1
|
|
|
<?php |
|
2
|
|
|
namespace Shlinkio\Shlink\Core\Middleware; |
|
3
|
|
|
|
|
4
|
|
|
use Acelaya\ZsmAnnotatedServices\Annotation\Inject; |
|
5
|
|
|
use Doctrine\Common\Cache\Cache; |
|
6
|
|
|
use Psr\Http\Message\ResponseInterface as Response; |
|
7
|
|
|
use Psr\Http\Message\ServerRequestInterface as Request; |
|
8
|
|
|
use Zend\Stratigility\MiddlewareInterface; |
|
9
|
|
|
|
|
10
|
|
|
class QrCodeCacheMiddleware implements MiddlewareInterface |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* @var Cache |
|
14
|
|
|
*/ |
|
15
|
|
|
private $cache; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* QrCodeCacheMiddleware constructor. |
|
19
|
|
|
* @param Cache $cache |
|
20
|
|
|
* |
|
21
|
|
|
* @Inject({Cache::class}) |
|
22
|
|
|
*/ |
|
23
|
2 |
|
public function __construct(Cache $cache) |
|
24
|
|
|
{ |
|
25
|
2 |
|
$this->cache = $cache; |
|
26
|
2 |
|
} |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Process an incoming request and/or response. |
|
30
|
|
|
* |
|
31
|
|
|
* Accepts a server-side request and a response instance, and does |
|
32
|
|
|
* something with them. |
|
33
|
|
|
* |
|
34
|
|
|
* If the response is not complete and/or further processing would not |
|
35
|
|
|
* interfere with the work done in the middleware, or if the middleware |
|
36
|
|
|
* wants to delegate to another process, it can use the `$out` callable |
|
37
|
|
|
* if present. |
|
38
|
|
|
* |
|
39
|
|
|
* If the middleware does not return a value, execution of the current |
|
40
|
|
|
* request is considered complete, and the response instance provided will |
|
41
|
|
|
* be considered the response to return. |
|
42
|
|
|
* |
|
43
|
|
|
* Alternately, the middleware may return a response instance. |
|
44
|
|
|
* |
|
45
|
|
|
* Often, middleware will `return $out();`, with the assumption that a |
|
46
|
|
|
* later middleware will return a response. |
|
47
|
|
|
* |
|
48
|
|
|
* @param Request $request |
|
49
|
|
|
* @param Response $response |
|
50
|
|
|
* @param null|callable $out |
|
51
|
|
|
* @return null|Response |
|
52
|
|
|
*/ |
|
53
|
2 |
|
public function __invoke(Request $request, Response $response, callable $out = null) |
|
54
|
|
|
{ |
|
55
|
2 |
|
$cacheKey = $request->getUri()->getPath(); |
|
56
|
|
|
|
|
57
|
|
|
// If this QR code is already cached, just return it |
|
58
|
2 |
|
if ($this->cache->contains($cacheKey)) { |
|
59
|
1 |
|
$qrData = $this->cache->fetch($cacheKey); |
|
60
|
1 |
|
$response->getBody()->write($qrData['body']); |
|
61
|
1 |
|
return $response->withHeader('Content-Type', $qrData['content-type']); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
// If not, call the next middleware and cache it |
|
65
|
|
|
/** @var Response $resp */ |
|
66
|
1 |
|
$resp = $out($request, $response); |
|
67
|
1 |
|
$this->cache->save($cacheKey, [ |
|
68
|
1 |
|
'body' => $resp->getBody()->__toString(), |
|
69
|
1 |
|
'content-type' => $resp->getHeaderLine('Content-Type'), |
|
70
|
1 |
|
]); |
|
71
|
1 |
|
return $resp; |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
|