|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of the BEAR.QueryRepository package. |
|
4
|
|
|
* |
|
5
|
|
|
* @license http://opensource.org/licenses/MIT MIT |
|
6
|
|
|
*/ |
|
7
|
|
|
namespace BEAR\QueryRepository; |
|
8
|
|
|
|
|
9
|
|
|
use BEAR\RepositoryModule\Annotation\Cacheable; |
|
10
|
|
|
use BEAR\Resource\ResourceObject; |
|
11
|
|
|
use Doctrine\Common\Annotations\Reader; |
|
12
|
|
|
use Ray\Aop\MethodInterceptor; |
|
13
|
|
|
use Ray\Aop\MethodInvocation; |
|
14
|
|
|
|
|
15
|
|
|
class CacheInterceptor implements MethodInterceptor |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* @var QueryRepositoryInterface |
|
19
|
|
|
*/ |
|
20
|
|
|
private $repository; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var EtagSetterInterface |
|
24
|
|
|
*/ |
|
25
|
|
|
private $setEtag; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var Reader |
|
29
|
|
|
*/ |
|
30
|
|
|
private $reader; |
|
31
|
|
|
|
|
32
|
|
|
public function __construct( |
|
33
|
14 |
|
QueryRepositoryInterface $repository, |
|
34
|
|
|
EtagSetterInterface $setEtag, |
|
35
|
|
|
Reader $annotationReader |
|
36
|
|
|
) { |
|
37
|
|
|
$this->repository = $repository; |
|
38
|
14 |
|
$this->setEtag = $setEtag; |
|
39
|
14 |
|
$this->reader = $annotationReader; |
|
40
|
14 |
|
} |
|
41
|
14 |
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* {@inheritdoc} |
|
44
|
|
|
*/ |
|
45
|
|
|
public function invoke(MethodInvocation $invocation) |
|
46
|
12 |
|
{ |
|
47
|
|
|
/** @var ResourceObject $ro */ |
|
48
|
12 |
|
$ro = $invocation->getThis(); |
|
49
|
|
|
$stored = $this->repository->get($ro->uri); |
|
50
|
12 |
|
if ($stored) { |
|
51
|
12 |
|
list($ro->code, $ro->headers, $ro->body, $ro->view) = $stored; |
|
52
|
9 |
|
|
|
53
|
|
|
return $ro; |
|
54
|
9 |
|
} |
|
55
|
|
|
/* @var $cacheable Cacheable */ |
|
56
|
|
|
try { |
|
57
|
|
|
/** @var ResourceObject $ro */ |
|
58
|
12 |
|
$ro = $invocation->proceed(); |
|
59
|
12 |
|
$ro->code === 200 ? $this->repository->put($ro) : $this->repository->purge($ro->uri); |
|
60
|
|
|
} catch (\Exception $e) { |
|
61
|
|
|
$this->repository->purge($ro->uri); |
|
62
|
|
|
throw $e; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
12 |
|
return $ro; |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
|