1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Columnis\Model; |
4
|
|
|
|
5
|
|
|
use Zend\EventManager\AbstractListenerAggregate; |
6
|
|
|
use Zend\EventManager\EventManagerInterface; |
7
|
|
|
use Zend\Mvc\MvcEvent; |
8
|
|
|
use Zend\Mvc\Router\RouteMatch; |
9
|
|
|
|
10
|
|
|
class CacheListener extends AbstractListenerAggregate |
11
|
|
|
{ |
12
|
|
|
|
13
|
|
|
protected $listeners = array(); |
14
|
|
|
protected $cacheService; |
15
|
|
|
|
16
|
1 |
|
public function __construct(HtmlCache $cacheService) |
17
|
|
|
{ |
18
|
|
|
// We store the cache service generated by Zend\Cache from the service manager |
19
|
|
|
//$cacheService->setExtension(''); |
20
|
1 |
|
$this->cacheService = $cacheService; |
21
|
1 |
|
} |
22
|
|
|
|
23
|
1 |
|
public function attach(EventManagerInterface $events) |
24
|
|
|
{ |
25
|
|
|
// The AbstractListenerAggregate we are extending from allows us to attach our even listeners |
26
|
1 |
|
$this->listeners[] = $events->attach(MvcEvent::EVENT_ROUTE, array($this, 'getCache'), -1000); |
27
|
1 |
|
$this->listeners[] = $events->attach(MvcEvent::EVENT_RENDER, array($this, 'saveCache'), -10000); |
28
|
1 |
|
} |
29
|
|
|
|
30
|
|
|
public function getCache(MvcEvent $event) |
31
|
|
|
{ |
32
|
|
|
$match = $event->getRouteMatch(); |
33
|
|
|
|
34
|
|
|
// is valid route? |
35
|
|
|
if (!$match) { |
36
|
|
|
return; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
// does our route have the cache flag set to true? |
40
|
|
|
if ($match->getParam('cache')) { |
41
|
|
|
$cacheKey = $this->genCacheName($match); |
42
|
|
|
|
43
|
|
|
// get the cache page for this route |
44
|
|
|
$data = $this->cacheService->getItem($cacheKey); |
45
|
|
|
|
46
|
|
|
// ensure we have found something valid |
47
|
|
|
if ($data !== null) { |
48
|
|
|
$response = $event->getResponse(); |
49
|
|
|
$response->setContent($data); |
50
|
|
|
|
51
|
|
|
return $response; |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function saveCache(MvcEvent $event) |
57
|
|
|
{ |
58
|
|
|
$match = $event->getRouteMatch(); |
59
|
|
|
|
60
|
|
|
// is valid route? |
61
|
|
|
if (!$match) { |
62
|
|
|
return; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
// does our route have the cache flag set to true? |
66
|
|
|
if ($match->getParam('cache')) { |
67
|
|
|
$response = $event->getResponse(); |
68
|
|
|
$data = $response->getContent(); |
69
|
|
|
|
70
|
|
|
$this->setCacheDir($match); |
71
|
|
|
$cacheKey = $this->genCacheName($match); |
72
|
|
|
$this->cacheService->setItem($cacheKey, $data); |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function setCacheDir(RouteMatch $match) |
77
|
|
|
{ |
78
|
|
|
$params = $match->getParams(); |
79
|
|
|
$lang = $params['lang']; |
80
|
|
|
$options = $this->cacheService->getOptions(); |
81
|
|
|
$options->setNamespace($lang); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @param \Zend\Mvc\Router\RouteMatch $match |
86
|
|
|
*/ |
87
|
|
|
public function genCacheName(RouteMatch $match) |
88
|
|
|
{ |
89
|
|
|
$params = $match->getParams(); |
90
|
|
|
$pageId = (int)$params['pageId']; |
91
|
|
|
return $pageId; |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|