Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
This repository was archived by the owner on Jan 21, 2020. It is now read-only.

Commit37b14be

Browse files
committed
Merge pull request#12 from corentin-larose/feature/12
[WIP] Route name, controller/action, controller based configurationConflicts:test/HttpCacheListenerTest.php
2 parentsdf5405b +f0d4cc6 commit37b14be

File tree

4 files changed

+172
-20
lines changed

4 files changed

+172
-20
lines changed

‎README.md‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,13 @@ The `config/module.config.php` file contains a self-explanative example of confi
5656

5757
####Key:`controllers`
5858

59-
The`controllers` key is utilized for mapping a combination of a controller and a HTTP method (see below) to a cache header configuration.
59+
The`controllers` key is utilized for mapping (case sensitive) either
60+
- a route name
61+
- or a concatenated`controller::action`
62+
- or a controller
63+
- or a regexp
64+
- or a wildcard
65+
and a HTTP method (see below) to a cache header configuration.
6066

6167
Example:
6268

@@ -80,7 +86,11 @@ Example:
8086

8187
#####Key:`<controller>`
8288

83-
Either a controller name (as returned by`Zend\Mvc\MvcEvent::getRouteMatch()->getParam('controller')`, case-sensitive) or a wildcard.
89+
Either
90+
- a concatenation of`$controller::$action`
91+
- or a controller name (as returned by`Zend\Mvc\MvcEvent::getRouteMatch()->getParam('controller')`, case-sensitive)
92+
- or a regexp (see`<regex_delimiter>` key)
93+
- or a wildcard
8494

8595
A wildcard stands for all the non-specified controllers.
8696

‎config/module.config.php‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@
5858
// /*
5959
// * Regular configuration.
6060
// */
61-
// 'home' => [ //controller name
61+
// 'home::index' => [ //route name
6262
// 'get' => [ // Http method (wildcard '*' supported as whatever method)
6363
// /*
6464
// * General HTTP caching theory: http://www.w3.org/Protocols/rfc2616/rfc2616-sec13.html
@@ -99,6 +99,15 @@
9999
// ],
100100
// ],
101101
// ],
102+
// 'index::index' => [ // controller / action names
103+
// 'get' => [...],
104+
// ],
105+
// 'index' => [ // controller name
106+
// 'get' => [...],
107+
// ],
108+
// '~.*::index~' => [ // regex
109+
// 'get' => [...],
110+
// ],
102111
// ],
103112
//
104113
// /*

‎src/HttpCacheListener.php‎

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -148,16 +148,33 @@ public function onRoute(MvcEvent $e)
148148
}
149149

150150
$cacheConfig =$this->config['controllers'];
151-
$controller =$e
152-
->getRouteMatch()
153-
->getParam('controller');
154-
155-
if (!empty($cacheConfig[$controller])) {
151+
$routeMatch =$e->getRouteMatch();
152+
153+
$action =$routeMatch->getParam('action');
154+
$controller =$routeMatch->getParam('controller');
155+
$routeName =$routeMatch->getMatchedRouteName();
156+
157+
/*
158+
* Searches, case sensitive, in this very order:
159+
* a matching route name in config
160+
* if not found, a matching "controller::action" name
161+
* if not found, a matching "controller" name
162+
* if not found, a matching regex
163+
* if not found, a wildcard (default)
164+
*/
165+
if (!empty($cacheConfig[$routeName])) {
166+
$controllerConfig =$cacheConfig[$routeName];
167+
}elseif (!empty($cacheConfig["$controller::$action"])) {
168+
$controllerConfig =$cacheConfig["$controller::$action"];
169+
}elseif (!empty($cacheConfig[$controller])) {
156170
$controllerConfig =$cacheConfig[$controller];
157171
}elseif (!empty($this->config['regex_delimiter'])) {
158172
foreach ($cacheConfigas$key =>$config) {
159173
if (substr($key,0,1) ===$this->config['regex_delimiter']) {
160-
if (preg_match($key,preg_quote($controller,$this->config['regex_delimiter']))) {
174+
if (preg_match($key,$routeName)
175+
||preg_match($key,"$controller::$action")
176+
||preg_match($key,$controller)
177+
) {
161178
$controllerConfig =$config;
162179
break;
163180
}

‎test/HttpCacheListenerTest.php‎

Lines changed: 127 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -65,14 +65,16 @@ public function configDataProvider()
6565
[
6666
['enable' =>false],
6767
'get',
68-
['controller' =>'foo'],
68+
'my.route.name',
69+
['action' =>'bar','controller' =>'foo'],
6970
[],
7071
],
7172

7273
[
7374
['enable' =>true],
7475
'get',
75-
[],
76+
'my.route.name',
77+
['action' =>'bar','controller' =>'foo'],
7678
[],
7779
],
7880

@@ -91,7 +93,49 @@ public function configDataProvider()
9193
],
9294
],
9395
'get',
94-
['controller' =>'foo'],
96+
'my.route.name',
97+
['action' =>'bar','controller' =>'foo'],
98+
[
99+
'expires' => [
100+
'override' =>true,
101+
'value' =>'+1 day',
102+
],
103+
],
104+
],
105+
106+
[
107+
[
108+
'enable' =>true,
109+
'controllers' => [
110+
'foo' => [
111+
'get' => [
112+
'expires' => [
113+
'override' =>true,
114+
'value' =>'+2 day',
115+
],
116+
],
117+
],
118+
'foo::bar' => [
119+
'get' => [
120+
'expires' => [
121+
'override' =>true,
122+
'value' =>'+2 day',
123+
],
124+
],
125+
],
126+
'my.route.name' => [
127+
'get' => [
128+
'expires' => [
129+
'override' =>true,
130+
'value' =>'+1 day',
131+
],
132+
],
133+
],
134+
],
135+
],
136+
'get',
137+
'my.route.name',
138+
['action' =>'bar','controller' =>'foo'],
95139
[
96140
'expires' => [
97141
'override' =>true,
@@ -115,7 +159,8 @@ public function configDataProvider()
115159
],
116160
],
117161
'head',
118-
['controller' =>'foo'],
162+
'my.route.name',
163+
['action' =>'bar','controller' =>'foo'],
119164
[
120165
'expires' => [
121166
'override' =>true,
@@ -139,7 +184,8 @@ public function configDataProvider()
139184
],
140185
],
141186
'get',
142-
['controller' =>'bar'],
187+
'my.route.name',
188+
['action' =>'baz','controller' =>'bar'],
143189
[
144190
'expires' => [
145191
'override' =>true,
@@ -163,7 +209,8 @@ public function configDataProvider()
163209
],
164210
],
165211
'head',
166-
['controller' =>'baz'],
212+
'my.route.name',
213+
['action' =>'bar','controller' =>'foo'],
167214
[
168215
'expires' => [
169216
'override' =>true,
@@ -195,7 +242,68 @@ public function configDataProvider()
195242
],
196243
],
197244
'head',
198-
['controller' =>'baz'],
245+
'my.route.name',
246+
['action' =>'bar','controller' =>'baz'],
247+
[
248+
'cache-control' => [
249+
'override' =>false,
250+
'value' =>'private',
251+
],
252+
],
253+
],
254+
255+
[
256+
[
257+
'enable' =>true,
258+
'controllers' => [
259+
'~my\.[a-z.]{10}~' => [
260+
'*' => [
261+
'cache-control' => [
262+
'override' =>false,
263+
'value' =>'private',
264+
],
265+
],
266+
],
267+
'*' => [
268+
'get' => [
269+
'cache-control' => [
270+
'override' =>true,
271+
'value' =>'public',
272+
],
273+
],
274+
],
275+
],
276+
'regex_delimiter' =>'~',
277+
],
278+
'head',
279+
'my.route.name',
280+
['action' =>'bar','controller' =>'baz'],
281+
[
282+
'cache-control' => [
283+
'override' =>false,
284+
'value' =>'private',
285+
],
286+
],
287+
],
288+
289+
[
290+
[
291+
'enable' =>true,
292+
'controllers' => [
293+
'~[a-z]{3}::[a-z]{3}~' => [
294+
'*' => [
295+
'cache-control' => [
296+
'override' =>false,
297+
'value' =>'private',
298+
],
299+
],
300+
],
301+
],
302+
'regex_delimiter' =>'~',
303+
],
304+
'head',
305+
'my.route.name',
306+
['action' =>'bar','controller' =>'baz'],
199307
[
200308
'cache-control' => [
201309
'override' =>false,
@@ -228,7 +336,8 @@ public function configDataProvider()
228336
'regex_delimiter' =>'~',
229337
],
230338
'head',
231-
['controller' =>'baz'],
339+
'my.route.name',
340+
['action' =>'bar','controller' =>'baz'],
232341
[
233342
'cache-control' => [
234343
'override' =>false,
@@ -253,7 +362,8 @@ public function configDataProvider()
253362
'regex_delimiter' =>'~',
254363
],
255364
'head',
256-
['controller' =>'baz'],
365+
'my.route.name',
366+
['action' =>'bar','controller' =>'baz'],
257367
[
258368
'cache-control' => [
259369
'override' =>false,
@@ -627,17 +737,23 @@ public function testOnResponse(array $config, $method, array $routeMatch, array
627737
*
628738
* @param array $config
629739
* @param string $method
740+
* @param string $routeName
630741
* @param array $routeMatch
631742
* @param array $exCacheConfig
632743
*/
633-
publicfunctiontestOnRoute(array$config,$method,array$routeMatch,array$exCacheConfig)
744+
publicfunctiontestOnRoute(array$config,$method,$routeName,array$routeMatch,array$exCacheConfig)
634745
{
635746
$request =newHttpRequest();
636747
$request->setMethod($method);
637748

749+
$routeMatch =$this->createRouteMatch($routeMatch);
750+
if ($routeName) {
751+
$routeMatch->setMatchedRouteName($routeName);
752+
}
753+
638754
$event =newMvcEvent();
639755
$event->setRequest($request);
640-
$event->setRouteMatch($this->createRouteMatch($routeMatch));
756+
$event->setRouteMatch($routeMatch);
641757

642758
$this->instance->setConfig($config)
643759
->onRoute($event);

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp