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