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\Http\Request as HttpRequest; |
11
|
|
|
use Zend\Mvc\MvcEvent; |
12
|
|
|
use Zend\Router\RouteMatch; |
13
|
|
|
|
14
|
|
|
class Route extends AbstractStrategy |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var array |
18
|
|
|
*/ |
19
|
|
|
protected $routes; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* {@inheritDoc} |
23
|
|
|
*/ |
24
|
|
|
public function shouldCache(MvcEvent $event) |
25
|
|
|
{ |
26
|
|
|
$routeMatch = $event->getRouteMatch(); |
27
|
|
|
if ($routeMatch === null) { |
28
|
|
|
return false; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
$routeName = $event->getRouteMatch()->getMatchedRouteName(); |
32
|
|
|
if (!array_key_exists($routeName, $this->getRoutes()) && !in_array($routeName, $this->getRoutes())) { |
33
|
|
|
return false; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
$routeConfig = $this->getRouteConfig($routeName); |
37
|
|
|
|
38
|
|
|
if ( |
39
|
|
|
!$this->checkParams($routeMatch, $routeConfig) || |
40
|
|
|
!$this->checkHttpMethod($event, $routeConfig) |
41
|
|
|
) { |
42
|
|
|
return false; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
return true; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Check if we should cache the request based on the params in the routematch |
50
|
|
|
* |
51
|
|
|
* @param RouteMatch $match |
52
|
|
|
* @param array $routeConfig |
53
|
|
|
* @return bool |
54
|
|
|
*/ |
55
|
|
|
protected function checkParams(RouteMatch $match, $routeConfig) |
56
|
|
|
{ |
57
|
|
|
if (!isset($routeConfig['params'])) { |
58
|
|
|
return true; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
$params = (array) $routeConfig['params']; |
62
|
|
|
foreach ($params as $name => $value) { |
63
|
|
|
|
64
|
|
|
$param = $match->getParam($name, null); |
65
|
|
|
if (null === $param) { |
66
|
|
|
continue; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
if (!$this->checkParam($param, $value)) { |
70
|
|
|
return false; |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
return true; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param $actualValue |
79
|
|
|
* @param $checkValue |
80
|
|
|
* @return bool |
81
|
|
|
*/ |
82
|
|
|
protected function checkParam($actualValue, $checkValue) |
83
|
|
|
{ |
84
|
|
|
if (is_array($checkValue)) { |
85
|
|
|
return in_array($actualValue, $checkValue); |
86
|
|
|
} elseif (preg_match('/^\/.*\//', $checkValue)) { |
87
|
|
|
$regex = $checkValue; |
88
|
|
|
if (!preg_match($regex, $actualValue)) { |
89
|
|
|
return false; |
90
|
|
|
} |
91
|
|
|
} elseif ($checkValue != $actualValue) { |
92
|
|
|
return false; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
return true; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Check if we should cache the request based on http method requested |
100
|
|
|
* |
101
|
|
|
* @param MvcEvent $event |
102
|
|
|
* @param $routeConfig |
103
|
|
|
* @return bool |
104
|
|
|
*/ |
105
|
|
|
protected function checkHttpMethod(MvcEvent $event, $routeConfig) |
106
|
|
|
{ |
107
|
|
|
$request = $event->getRequest(); |
108
|
|
|
if (!$request instanceof HttpRequest) { |
109
|
|
|
return false; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
if (isset($routeConfig['http_methods'])) { |
113
|
|
|
$methods = (array) $routeConfig['http_methods']; |
114
|
|
|
if (!in_array($request->getMethod(), $methods)) { |
115
|
|
|
return false; |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
return true; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @param $routeName |
124
|
|
|
* @return array |
125
|
|
|
*/ |
126
|
|
|
protected function getRouteConfig($routeName) |
127
|
|
|
{ |
128
|
|
|
$routes = $this->getRoutes(); |
129
|
|
|
if (!isset($routes[$routeName])) { |
130
|
|
|
return []; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
return (array) $routes[$routeName]; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @return array |
138
|
|
|
*/ |
139
|
|
|
public function getRoutes() |
140
|
|
|
{ |
141
|
|
|
return $this->routes; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* @param array $routes |
146
|
|
|
*/ |
147
|
|
|
public function setRoutes(array $routes) |
148
|
|
|
{ |
149
|
|
|
$this->routes = $routes; |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
|