1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10:
11:
12: namespace Symfony\Component\HttpFoundation;
13:
14: 15: 16: 17: 18: 19: 20:
21: class RequestMatcher implements RequestMatcherInterface
22: {
23: 24: 25:
26: private $path;
27:
28: 29: 30:
31: private $host;
32:
33: 34: 35:
36: private $methods = array();
37:
38: 39: 40:
41: private $ip;
42:
43: 44: 45:
46: private $attributes = array();
47:
48: 49: 50: 51: 52: 53: 54:
55: public function __construct($path = null, $host = null, $methods = null, $ip = null, array $attributes = array())
56: {
57: $this->matchPath($path);
58: $this->matchHost($host);
59: $this->matchMethod($methods);
60: $this->matchIp($ip);
61: foreach ($attributes as $k => $v) {
62: $this->matchAttribute($k, $v);
63: }
64: }
65:
66: 67: 68: 69: 70:
71: public function matchHost($regexp)
72: {
73: $this->host = $regexp;
74: }
75:
76: 77: 78: 79: 80:
81: public function matchPath($regexp)
82: {
83: $this->path = $regexp;
84: }
85:
86: 87: 88: 89: 90:
91: public function matchIp($ip)
92: {
93: $this->ip = $ip;
94: }
95:
96: 97: 98: 99: 100:
101: public function matchMethod($method)
102: {
103: $this->methods = array_map('strtoupper', (array) $method);
104: }
105:
106: 107: 108: 109: 110: 111:
112: public function matchAttribute($key, $regexp)
113: {
114: $this->attributes[$key] = $regexp;
115: }
116:
117: 118: 119: 120: 121:
122: public function matches(Request $request)
123: {
124: if ($this->methods && !in_array($request->getMethod(), $this->methods)) {
125: return false;
126: }
127:
128: foreach ($this->attributes as $key => $pattern) {
129: if (!preg_match('#'.str_replace('#', '\\#', $pattern).'#', $request->attributes->get($key))) {
130: return false;
131: }
132: }
133:
134: if (null !== $this->path) {
135: $path = str_replace('#', '\\#', $this->path);
136:
137: if (!preg_match('#'.$path.'#', rawurldecode($request->getPathInfo()))) {
138: return false;
139: }
140: }
141:
142: if (null !== $this->host && !preg_match('#'.str_replace('#', '\\#', $this->host).'#i', $request->getHost())) {
143: return false;
144: }
145:
146: if (null !== $this->ip && !IpUtils::checkIp($request->getClientIp(), $this->ip)) {
147: return false;
148: }
149:
150: return true;
151: }
152: }
153: