1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @author Bram Gerritsen [email protected] |
4
|
|
|
* @copyright (c) Bram Gerritsen 2013 |
5
|
|
|
* @license http://opensource.org/licenses/mit-license.php |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace StrokerCache\Strategy; |
9
|
|
|
|
10
|
|
|
use Zend\Mvc\MvcEvent; |
11
|
|
|
|
12
|
|
|
use StrokerCache\Exception\BadConfigurationException; |
13
|
|
|
|
14
|
|
|
class CacheAllExcept extends AbstractStrategy |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var array |
18
|
|
|
*/ |
19
|
|
|
protected $except; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* {@inheritDoc} |
23
|
|
|
*/ |
24
|
|
|
public function shouldCache(MvcEvent $event) |
25
|
|
|
{ |
26
|
|
|
$except = $this->getExcept(); |
27
|
|
|
|
28
|
|
|
if (!isset($except['namespaces']) && !isset($except['controllers']) && !isset($except['actions'])) { |
29
|
|
|
throw new BadConfigurationException( |
30
|
|
|
"At least one of ['namespaces', 'controllers', 'actions'] keys has to be set in the " |
31
|
|
|
. "\$config['strokercache']['strategies']['enabled']['" . __CLASS__ . "']['except'][] " |
32
|
|
|
. "configuration array." |
33
|
|
|
); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
$routeMatch = $event->getRouteMatch(); |
37
|
|
|
|
38
|
|
|
if (null === $routeMatch) { |
39
|
|
|
return false; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
$controller = $this->normalize($routeMatch->getParam('controller')); |
43
|
|
|
$action = $this->normalize($routeMatch->getParam('action')); |
44
|
|
|
|
45
|
|
|
$shouldCache = true; |
46
|
|
|
|
47
|
|
|
if (true === $shouldCache && isset($except['namespaces'])) { |
48
|
|
|
foreach ($except['namespaces'] as $exceptNamespace) { |
49
|
|
|
if (0 === strpos($controller, $this->normalize($exceptNamespace))) { |
50
|
|
|
$shouldCache = false; |
51
|
|
|
break 1; |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
if (true === $shouldCache && isset($except['controllers'])) { |
57
|
|
|
foreach ($except['controllers'] as $exceptController) { |
58
|
|
|
if ($controller === $this->normalize($exceptController)) { |
59
|
|
|
$shouldCache = false; |
60
|
|
|
break 1; |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
if (true === $shouldCache && isset($except['actions'])) { |
66
|
|
|
foreach ($except['actions'] as $exceptController => $exceptActions) { |
67
|
|
|
if ($controller === $this->normalize($exceptController)) { |
68
|
|
|
foreach ($exceptActions as $exceptAction) { |
69
|
|
|
if ($action === $this->normalize($exceptAction)) { |
70
|
|
|
$shouldCache = false; |
71
|
|
|
break 2; |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
return $shouldCache; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @return array |
83
|
|
|
*/ |
84
|
|
|
public function getExcept() |
85
|
|
|
{ |
86
|
|
|
return $this->except; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @param array $except |
91
|
|
|
* @return $this |
92
|
|
|
*/ |
93
|
|
|
public function setExcept(array $except) |
94
|
|
|
{ |
95
|
|
|
$this->except = $except; |
96
|
|
|
|
97
|
|
|
return $this; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Normalize names before comparing |
102
|
|
|
* |
103
|
|
|
* @param string $string |
104
|
|
|
* @return string |
105
|
|
|
*/ |
106
|
|
|
protected function normalize($string) |
107
|
|
|
{ |
108
|
|
|
$string = trim($string); |
109
|
|
|
|
110
|
|
|
$string = strtolower($string); |
111
|
|
|
|
112
|
|
|
return $string; |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|