|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Mouf\Mvc\Splash\Routers; |
|
4
|
|
|
|
|
5
|
|
|
use Interop\Http\ServerMiddleware\DelegateInterface; |
|
6
|
|
|
use Interop\Http\ServerMiddleware\MiddlewareInterface; |
|
7
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
8
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
|
9
|
|
|
use Mouf\Utils\Cache\CacheInterface; |
|
10
|
|
|
use Psr\Log\LoggerInterface; |
|
11
|
|
|
use Mouf\Utils\Common\ConditionInterface\ConditionInterface; |
|
12
|
|
|
|
|
13
|
|
|
class CacheRouter implements MiddlewareInterface |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* @var CacheInterface |
|
17
|
|
|
*/ |
|
18
|
|
|
private $cache; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @var ConditionInterface |
|
22
|
|
|
*/ |
|
23
|
|
|
private $cacheCondition; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var LoggerInterface |
|
27
|
|
|
*/ |
|
28
|
|
|
private $log; |
|
29
|
|
|
|
|
30
|
|
|
public function __construct(CacheInterface $cache, LoggerInterface $log, ConditionInterface $cacheCondition) |
|
31
|
|
|
{ |
|
32
|
|
|
$this->cache = $cache; |
|
33
|
|
|
$this->cacheCondition = $cacheCondition; |
|
34
|
|
|
$this->log = $log; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Process an incoming server request and return a response, optionally delegating |
|
39
|
|
|
* to the next middleware component to create the response. |
|
40
|
|
|
* |
|
41
|
|
|
* @param ServerRequestInterface $request |
|
42
|
|
|
* @param DelegateInterface $delegate |
|
43
|
|
|
* |
|
44
|
|
|
* @return ResponseInterface |
|
45
|
|
|
*/ |
|
46
|
|
|
public function process(ServerRequestInterface $request, DelegateInterface $delegate) |
|
47
|
|
|
{ |
|
48
|
|
|
$requestMethod = $request->getMethod(); |
|
49
|
|
|
$key = str_replace(['\\', '/', ':', '*', '?', '"', '<', '>', '|'], '_', $request->getUri()->getPath().'?'.$request->getUri()->getQuery()); |
|
50
|
|
|
|
|
51
|
|
|
if ($this->cacheCondition->isOk() && $requestMethod == 'GET') { |
|
52
|
|
|
$cacheResponse = $this->cache->get($key); |
|
53
|
|
|
if ($cacheResponse) { |
|
54
|
|
|
$this->log->debug("Cache HIT on $key"); |
|
55
|
|
|
|
|
56
|
|
|
return $cacheResponse; |
|
57
|
|
|
} else { |
|
58
|
|
|
$this->log->debug("Cache MISS on key $key"); |
|
59
|
|
|
$response = $delegate->process($request); |
|
60
|
|
|
|
|
61
|
|
|
$noCache = false; |
|
62
|
|
|
if ($response->hasHeader('Mouf-Cache-Control') && $response->getHeader('Mouf-Cache-Control')[0] == 'no-cache') { |
|
63
|
|
|
$noCache = true; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
if ($noCache) { |
|
67
|
|
|
$this->log->debug("Mouf NO CACHE header found, not storing '$key'"); |
|
68
|
|
|
} else { |
|
69
|
|
|
$ttl = null; |
|
70
|
|
|
|
|
71
|
|
|
// TODO: continue here! |
|
72
|
|
|
// Use PSR-7 response to analyze maxage and expires... |
|
73
|
|
|
// ...or... use a completely different HTTP cache implementation!!! |
|
74
|
|
|
// There must be one around for PSR-7! |
|
75
|
|
|
|
|
76
|
|
|
$maxAge = $response->getMaxAge(); |
|
|
|
|
|
|
77
|
|
|
$expires = $response->getExpires(); |
|
|
|
|
|
|
78
|
|
|
if ($maxAge) { |
|
79
|
|
|
$this->log->debug("MaxAge specified : $maxAge"); |
|
80
|
|
|
$ttl = $maxAge; |
|
81
|
|
|
} elseif ($expires) { |
|
82
|
|
|
$this->log->debug("Expires specified : $expires"); |
|
83
|
|
|
$ttl = date_diff($expires, new \DateTime())->s; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
if ($ttl) { |
|
87
|
|
|
$this->log->debug("TTL is : $ttl"); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
// Make sure the response is serializable |
|
91
|
|
|
$serializableResponse = new Response(); |
|
|
|
|
|
|
92
|
|
|
$serializableResponse->headers = $response->headers; |
|
|
|
|
|
|
93
|
|
|
|
|
94
|
|
|
ob_start(); |
|
95
|
|
|
$response->sendContent(); |
|
|
|
|
|
|
96
|
|
|
$content = ob_get_clean(); |
|
97
|
|
|
|
|
98
|
|
|
$serializableResponse->setContent($content); |
|
99
|
|
|
|
|
100
|
|
|
$this->cache->set($key, $serializableResponse, $ttl); |
|
101
|
|
|
$this->log->debug("Cache STORED on key $key"); |
|
102
|
|
|
$response = $serializableResponse; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
return $response; |
|
106
|
|
|
} |
|
107
|
|
|
} else { |
|
108
|
|
|
$this->log->debug("No cache for $key"); |
|
109
|
|
|
|
|
110
|
|
|
return $this->fallBackRouter->handle($request, $type, $catch); |
|
|
|
|
|
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.