|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Listener that invalidates (= removes) matching stuff from the Schema cache when appropriate |
|
4
|
|
|
*/ |
|
5
|
|
|
|
|
6
|
|
|
namespace Graviton\SchemaBundle\Listener; |
|
7
|
|
|
|
|
8
|
|
|
use Doctrine\Common\Cache\CacheProvider; |
|
9
|
|
|
use Graviton\RestBundle\Controller\RestController; |
|
10
|
|
|
use Graviton\RestBundle\Event\RestEvent; |
|
11
|
|
|
use Graviton\RestBundle\Model\DocumentModel; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* @author List of contributors <https://github.com/libgraviton/graviton/graphs/contributors> |
|
15
|
|
|
* @license http://opensource.org/licenses/gpl-license.php GNU Public License |
|
16
|
|
|
* @link http://swisscom.ch |
|
17
|
|
|
*/ |
|
18
|
|
|
class SchemaCacheInvalidationResponseListener |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* @var CacheProvider |
|
22
|
|
|
*/ |
|
23
|
|
|
private $cache; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var string |
|
27
|
|
|
*/ |
|
28
|
|
|
private $cacheInvalidationMapKey; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Constructor |
|
32
|
|
|
* |
|
33
|
|
|
* @param CacheProvider $cache cache |
|
34
|
|
|
* @param string $cacheInvalidationMapKey cache key of invalidation map |
|
35
|
|
|
*/ |
|
36
|
4 |
|
public function __construct(CacheProvider $cache, $cacheInvalidationMapKey) |
|
37
|
|
|
{ |
|
38
|
4 |
|
$this->cache = $cache; |
|
39
|
4 |
|
$this->cacheInvalidationMapKey = $cacheInvalidationMapKey; |
|
40
|
4 |
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Invalidates stuff if where are in the invalidation map |
|
44
|
|
|
* |
|
45
|
|
|
* @param RestEvent $event rest event |
|
46
|
|
|
* |
|
47
|
|
|
* @return void |
|
48
|
|
|
*/ |
|
49
|
4 |
|
public function onRestRequest(RestEvent $event) |
|
50
|
|
|
{ |
|
51
|
4 |
|
if ($event->getRequest()->getMethod() == 'GET') { |
|
52
|
4 |
|
return; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
4 |
|
$controller = $event->getController(); |
|
56
|
|
|
|
|
57
|
4 |
|
if ($controller instanceof RestController && |
|
58
|
4 |
|
$controller->getModel() instanceof DocumentModel && |
|
59
|
4 |
|
$this->cache->contains($this->cacheInvalidationMapKey) |
|
60
|
2 |
|
) { |
|
61
|
2 |
|
$className = $controller->getModel()->getEntityClass(); |
|
62
|
2 |
|
$invalidationMap = $this->cache->fetch($this->cacheInvalidationMapKey); |
|
63
|
|
|
|
|
64
|
2 |
|
if (!isset($invalidationMap[$className])) { |
|
65
|
2 |
|
return; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
foreach ($invalidationMap[$className] as $cacheKey) { |
|
69
|
|
|
$this->cache->delete($cacheKey); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
unset($invalidationMap[$className]); |
|
73
|
|
|
|
|
74
|
|
|
$this->cache->save($this->cacheInvalidationMapKey, $invalidationMap); |
|
75
|
|
|
} |
|
76
|
2 |
|
} |
|
77
|
|
|
} |
|
78
|
|
|
|