1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the FOSHttpCacheBundle package. |
5
|
|
|
* |
6
|
|
|
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace FOS\HttpCacheBundle\EventListener; |
13
|
|
|
|
14
|
|
|
use FOS\HttpCacheBundle\Http\RuleMatcherInterface; |
15
|
|
|
use Symfony\Component\HttpFoundation\Request; |
16
|
|
|
use Symfony\Component\HttpFoundation\Response; |
17
|
|
|
|
18
|
|
|
abstract class AbstractRuleListener |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @var array List of arrays with RuleMatcher, settings array |
22
|
|
|
*/ |
23
|
|
|
private $rulesMap = array(); |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Add a rule matcher with a list of header directives to apply if the |
27
|
|
|
* request and response are matched. |
28
|
|
|
* |
29
|
|
|
* @param RuleMatcherInterface $ruleMatcher The headers apply to responses matched by this matcher |
30
|
|
|
* @param array $settings An array of header configuration |
31
|
|
|
*/ |
32
|
27 |
|
public function addRule( |
33
|
|
|
RuleMatcherInterface $ruleMatcher, |
34
|
|
|
array $settings = array() |
35
|
|
|
) { |
36
|
27 |
|
$this->rulesMap[] = array($ruleMatcher, $settings); |
37
|
27 |
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Return the settings for the current request if any rule matches. |
41
|
|
|
* |
42
|
|
|
* @param Request $request |
43
|
|
|
* @param Response $response |
44
|
|
|
* |
45
|
|
|
* @return array|false Settings to apply or false if no rule matched |
46
|
|
|
*/ |
47
|
22 |
|
protected function matchRule(Request $request, Response $response) |
48
|
|
|
{ |
49
|
22 |
|
foreach ($this->rulesMap as $elements) { |
50
|
19 |
|
if ($elements[0]->matches($request, $response)) { |
51
|
19 |
|
return $elements[1]; |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
|
55
|
21 |
|
return false; |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|