Passed
Branch master (6a40a3)
by Ch
02:38
created
examples/basic/index.php 2 patches
Indentation   +13 added lines, -13 removed lines patch added patch discarded remove patch
@@ -26,19 +26,19 @@
 block discarded – undo
26 26
 <pre>
27 27
  <?php
28 28
  if ($match) {
29
-     foreach ($match as $key => $value) {
30
-         echo '<p>' . $key . ': ';
31
-         if (is_array($value)) {
32
-             echo '<ul>';
33
-             foreach ($value as $k => $v) {
34
-                 echo '<li>'.$k.': '.$v.'</li>';
35
-             }
36
-             echo '</ul>';
37
-         } else {
38
-             echo $value;
39
-         }
40
-         echo '</p>';
41
-     }
29
+	 foreach ($match as $key => $value) {
30
+		 echo '<p>' . $key . ': ';
31
+		 if (is_array($value)) {
32
+			 echo '<ul>';
33
+			 foreach ($value as $k => $v) {
34
+				 echo '<li>'.$k.': '.$v.'</li>';
35
+			 }
36
+			 echo '</ul>';
37
+		 } else {
38
+			 echo $value;
39
+		 }
40
+		 echo '</p>';
41
+	 }
42 42
  }
43 43
  ?>
44 44
  </pre>
Please login to merge, or discard this patch.
Spacing   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -31,7 +31,7 @@
 block discarded – undo
31 31
          if (is_array($value)) {
32 32
              echo '<ul>';
33 33
              foreach ($value as $k => $v) {
34
-                 echo '<li>'.$k.': '.$v.'</li>';
34
+                 echo '<li>' . $k . ': ' . $v . '</li>';
35 35
              }
36 36
              echo '</ul>';
37 37
          } else {
Please login to merge, or discard this patch.
src/Router.php 2 patches
Spacing   +2 added lines, -2 removed lines patch added patch discarded remove patch
@@ -90,7 +90,7 @@  discard block
 block discarded – undo
90 90
 
91 91
             return array(
92 92
                 'target' => $handler[2],
93
-                'params' => array_filter($this->parser->getParams(), function ($k) {
93
+                'params' => array_filter($this->parser->getParams(), function($k) {
94 94
                     return !is_numeric($k);
95 95
                 }, ARRAY_FILTER_USE_KEY),
96 96
                 'name'   => $handler[3]
@@ -108,7 +108,7 @@  discard block
 block discarded – undo
108 108
     public function __call($method, $arguments)
109 109
     {
110 110
         if (!in_array($method, array('get', 'post', 'delete', 'put', 'patch', 'update', 'all'))) {
111
-            throw new RouterException($method . ' not exist in the '. __CLASS__);
111
+            throw new RouterException($method . ' not exist in the ' . __CLASS__);
112 112
         }
113 113
 
114 114
         $methods = $method == 'all' ? implode('|', $this->all) : $method;
Please login to merge, or discard this patch.
Indentation   +211 added lines, -211 removed lines patch added patch discarded remove patch
@@ -5,215 +5,215 @@
 block discarded – undo
5 5
 
6 6
 class Router
7 7
 {
8
-    protected $routes = [];
9
-    protected $namedRoutes = [];
10
-    protected $basePath = '';
11
-    protected $all = ['get', 'post'];
12
-    protected $server;
13
-    /**
14
-     * @var RouterParser
15
-     */
16
-    private $parser;
17
-
18
-    /**
19
-     * Create router in one call from config.
20
-     *
21
-     * @param RouterParser $parser
22
-     * @param array $routes
23
-     * @param string $basePath
24
-     * @param array|null $server
25
-     */
26
-    public function __construct(RouterParser $parser, $routes = [], $basePath = '', $server = null)
27
-    {
28
-        $this->setRoutes($routes);
29
-        $this->setBasePath($basePath);
30
-        $this->parser = $parser;
31
-        $this->server = $server;
32
-    }
33
-
34
-    /**
35
-     * Map a route to a target
36
-     *
37
-     * @param string $method One of 5 HTTP Methods, or a pipe-separated list of multiple HTTP Methods (GET|POST|PATCH|PUT|DELETE)
38
-     * @param string $route The route regex, custom regex must start with an @. You can use multiple pre-set regex filters, like [i:id]
39
-     * @param mixed $target The target where this route should point to. Can be anything.
40
-     * @param string $routeName Optional name of this route. Supply if you want to reverse route this url in your application.
41
-     */
42
-    public function map($method, $route, $target, $routeName = null)
43
-    {
44
-        if (is_string($routeName)) {
45
-            $this->handleException($routeName, "Can not redeclare route '%s'", true);
46
-            $this->namedRoutes[$routeName] = $route;
47
-        }
48
-
49
-        $this->routes[] = array($method, $route, $target, $routeName);
50
-    }
51
-
52
-    /**
53
-     * Reversed routing
54
-     * Generate the URL for a named route. Replace regexes with supplied parameters
55
-     *
56
-     * @param string $routeName The name of the route.
57
-     * @param array $params Associative array of parameters to replace placeholders with.
58
-     *
59
-     * @return string The URL of the route with named parameters in place.
60
-     */
61
-    public function generate($routeName, array $params = [])
62
-    {
63
-        $this->handleException($routeName, "Route '%s' does not exist.", false);
64
-
65
-        $route = $this->namedRoutes[$routeName];
66
-
67
-        return $this->parser->generateUrl($this->basePath, $route, $params);
68
-    }
69
-
70
-    /**
71
-     * Match a given Request Url against stored routes
72
-     * @param string $requestUrl
73
-     * @param string $requestMethod
74
-     *
75
-     * @return array|boolean Array with route information on success, false on failure (no match).
76
-     */
77
-    public function match($requestUrl = null, $requestMethod = null)
78
-    {
79
-        $requestUrl = $this->getRequestUrl($requestUrl);
80
-
81
-        // set Request Method if it isn't passed as a parameter
82
-        if (is_null($requestMethod)) {
83
-            $requestMethod = $this->server['REQUEST_METHOD'];
84
-        }
85
-
86
-        foreach ($this->routes as $handler) {
87
-            if (!$this->parser->methodMatch($handler[0], $requestMethod, $handler[1], $requestUrl)) {
88
-                continue;
89
-            }
90
-
91
-            return array(
92
-                'target' => $handler[2],
93
-                'params' => array_filter($this->parser->getParams(), function ($k) {
94
-                    return !is_numeric($k);
95
-                }, ARRAY_FILTER_USE_KEY),
96
-                'name'   => $handler[3]
97
-            );
98
-        }
99
-
100
-        return false;
101
-    }
102
-
103
-    /**
104
-     * @param $method
105
-     * @param $arguments
106
-     *
107
-     * @throws RouterException
108
-     */
109
-    public function __call($method, $arguments)
110
-    {
111
-        if (!in_array($method, array('get', 'post', 'delete', 'put', 'patch', 'update', 'all'))) {
112
-            throw new RouterException($method . ' not exist in the '. __CLASS__);
113
-        }
114
-
115
-        $methods = $method == 'all' ? implode('|', $this->all) : $method;
116
-
117
-        $route = array_merge([$methods], $arguments);
118
-
119
-        call_user_func_array([$this, 'map'], $route);
120
-    }
121
-
122
-    /**
123
-     * Retrieves all routes.
124
-     * Useful if you want to process or display routes.
125
-     *
126
-     * @return array All routes.
127
-     */
128
-    public function getRoutes()
129
-    {
130
-        return $this->routes;
131
-    }
132
-
133
-    /**
134
-     * Add multiple routes at once from array in the following format:
135
-     *
136
-     *   $routes = array(
137
-     *      array($method, $route, $target, $name)
138
-     *   );
139
-     *
140
-     * @param array|Iterator $routes
141
-     *
142
-     * @return void
143
-     */
144
-    public function setRoutes($routes)
145
-    {
146
-        if (!is_array($routes) && !$routes instanceof Iterator) {
147
-            throw new RouterException('Routes should be an array or an instance of Iterator');
148
-        }
149
-        if (!empty($routes)) {
150
-            foreach ($routes as $route) {
151
-                call_user_func_array(array($this, 'map'), $route);
152
-            }
153
-        }
154
-    }
155
-
156
-    /**
157
-     * Set the base path.
158
-     * Useful if you are running your application from a subdirectory.
159
-     *
160
-     * @param $basePath
161
-     */
162
-    public function setBasePath($basePath)
163
-    {
164
-        $this->basePath = $basePath;
165
-    }
166
-
167
-    /**
168
-     * @param $routeName
169
-     * @param $message
170
-     * @param bool $cmpTo
171
-     *
172
-     * @throws RouterException
173
-     */
174
-    private function handleException($routeName, $message, $cmpTo)
175
-    {
176
-        if (array_key_exists($routeName, $this->namedRoutes) === $cmpTo) {
177
-            throw new RouterException(sprintf($message, $routeName));
178
-        }
179
-    }
180
-
181
-    /**
182
-     * @param $requestUrl
183
-     *
184
-     * @return mixed
185
-     */
186
-    private function getRequestUrl($requestUrl = null)
187
-    {
188
-        // set Request Url if it isn't passed as parameter
189
-        if (is_null($requestUrl)) {
190
-            $requestUrl = parse_url($this->server['REQUEST_URI'], PHP_URL_PATH);
191
-        }
192
-
193
-        return str_replace($this->basePath, '', strtok($requestUrl, '?'));
194
-    }
195
-
196
-    /**
197
-     * @return RouterParser
198
-     */
199
-    public function getParser()
200
-    {
201
-        return $this->parser;
202
-    }
203
-
204
-
205
-    public function getNamedRoutes()
206
-    {
207
-        return $this->namedRoutes;
208
-    }
209
-
210
-    public function getBasePath()
211
-    {
212
-        return $this->basePath;
213
-    }
214
-
215
-    public function setServer($server)
216
-    {
217
-        $this->server = $server;
218
-    }
8
+	protected $routes = [];
9
+	protected $namedRoutes = [];
10
+	protected $basePath = '';
11
+	protected $all = ['get', 'post'];
12
+	protected $server;
13
+	/**
14
+	 * @var RouterParser
15
+	 */
16
+	private $parser;
17
+
18
+	/**
19
+	 * Create router in one call from config.
20
+	 *
21
+	 * @param RouterParser $parser
22
+	 * @param array $routes
23
+	 * @param string $basePath
24
+	 * @param array|null $server
25
+	 */
26
+	public function __construct(RouterParser $parser, $routes = [], $basePath = '', $server = null)
27
+	{
28
+		$this->setRoutes($routes);
29
+		$this->setBasePath($basePath);
30
+		$this->parser = $parser;
31
+		$this->server = $server;
32
+	}
33
+
34
+	/**
35
+	 * Map a route to a target
36
+	 *
37
+	 * @param string $method One of 5 HTTP Methods, or a pipe-separated list of multiple HTTP Methods (GET|POST|PATCH|PUT|DELETE)
38
+	 * @param string $route The route regex, custom regex must start with an @. You can use multiple pre-set regex filters, like [i:id]
39
+	 * @param mixed $target The target where this route should point to. Can be anything.
40
+	 * @param string $routeName Optional name of this route. Supply if you want to reverse route this url in your application.
41
+	 */
42
+	public function map($method, $route, $target, $routeName = null)
43
+	{
44
+		if (is_string($routeName)) {
45
+			$this->handleException($routeName, "Can not redeclare route '%s'", true);
46
+			$this->namedRoutes[$routeName] = $route;
47
+		}
48
+
49
+		$this->routes[] = array($method, $route, $target, $routeName);
50
+	}
51
+
52
+	/**
53
+	 * Reversed routing
54
+	 * Generate the URL for a named route. Replace regexes with supplied parameters
55
+	 *
56
+	 * @param string $routeName The name of the route.
57
+	 * @param array $params Associative array of parameters to replace placeholders with.
58
+	 *
59
+	 * @return string The URL of the route with named parameters in place.
60
+	 */
61
+	public function generate($routeName, array $params = [])
62
+	{
63
+		$this->handleException($routeName, "Route '%s' does not exist.", false);
64
+
65
+		$route = $this->namedRoutes[$routeName];
66
+
67
+		return $this->parser->generateUrl($this->basePath, $route, $params);
68
+	}
69
+
70
+	/**
71
+	 * Match a given Request Url against stored routes
72
+	 * @param string $requestUrl
73
+	 * @param string $requestMethod
74
+	 *
75
+	 * @return array|boolean Array with route information on success, false on failure (no match).
76
+	 */
77
+	public function match($requestUrl = null, $requestMethod = null)
78
+	{
79
+		$requestUrl = $this->getRequestUrl($requestUrl);
80
+
81
+		// set Request Method if it isn't passed as a parameter
82
+		if (is_null($requestMethod)) {
83
+			$requestMethod = $this->server['REQUEST_METHOD'];
84
+		}
85
+
86
+		foreach ($this->routes as $handler) {
87
+			if (!$this->parser->methodMatch($handler[0], $requestMethod, $handler[1], $requestUrl)) {
88
+				continue;
89
+			}
90
+
91
+			return array(
92
+				'target' => $handler[2],
93
+				'params' => array_filter($this->parser->getParams(), function ($k) {
94
+					return !is_numeric($k);
95
+				}, ARRAY_FILTER_USE_KEY),
96
+				'name'   => $handler[3]
97
+			);
98
+		}
99
+
100
+		return false;
101
+	}
102
+
103
+	/**
104
+	 * @param $method
105
+	 * @param $arguments
106
+	 *
107
+	 * @throws RouterException
108
+	 */
109
+	public function __call($method, $arguments)
110
+	{
111
+		if (!in_array($method, array('get', 'post', 'delete', 'put', 'patch', 'update', 'all'))) {
112
+			throw new RouterException($method . ' not exist in the '. __CLASS__);
113
+		}
114
+
115
+		$methods = $method == 'all' ? implode('|', $this->all) : $method;
116
+
117
+		$route = array_merge([$methods], $arguments);
118
+
119
+		call_user_func_array([$this, 'map'], $route);
120
+	}
121
+
122
+	/**
123
+	 * Retrieves all routes.
124
+	 * Useful if you want to process or display routes.
125
+	 *
126
+	 * @return array All routes.
127
+	 */
128
+	public function getRoutes()
129
+	{
130
+		return $this->routes;
131
+	}
132
+
133
+	/**
134
+	 * Add multiple routes at once from array in the following format:
135
+	 *
136
+	 *   $routes = array(
137
+	 *      array($method, $route, $target, $name)
138
+	 *   );
139
+	 *
140
+	 * @param array|Iterator $routes
141
+	 *
142
+	 * @return void
143
+	 */
144
+	public function setRoutes($routes)
145
+	{
146
+		if (!is_array($routes) && !$routes instanceof Iterator) {
147
+			throw new RouterException('Routes should be an array or an instance of Iterator');
148
+		}
149
+		if (!empty($routes)) {
150
+			foreach ($routes as $route) {
151
+				call_user_func_array(array($this, 'map'), $route);
152
+			}
153
+		}
154
+	}
155
+
156
+	/**
157
+	 * Set the base path.
158
+	 * Useful if you are running your application from a subdirectory.
159
+	 *
160
+	 * @param $basePath
161
+	 */
162
+	public function setBasePath($basePath)
163
+	{
164
+		$this->basePath = $basePath;
165
+	}
166
+
167
+	/**
168
+	 * @param $routeName
169
+	 * @param $message
170
+	 * @param bool $cmpTo
171
+	 *
172
+	 * @throws RouterException
173
+	 */
174
+	private function handleException($routeName, $message, $cmpTo)
175
+	{
176
+		if (array_key_exists($routeName, $this->namedRoutes) === $cmpTo) {
177
+			throw new RouterException(sprintf($message, $routeName));
178
+		}
179
+	}
180
+
181
+	/**
182
+	 * @param $requestUrl
183
+	 *
184
+	 * @return mixed
185
+	 */
186
+	private function getRequestUrl($requestUrl = null)
187
+	{
188
+		// set Request Url if it isn't passed as parameter
189
+		if (is_null($requestUrl)) {
190
+			$requestUrl = parse_url($this->server['REQUEST_URI'], PHP_URL_PATH);
191
+		}
192
+
193
+		return str_replace($this->basePath, '', strtok($requestUrl, '?'));
194
+	}
195
+
196
+	/**
197
+	 * @return RouterParser
198
+	 */
199
+	public function getParser()
200
+	{
201
+		return $this->parser;
202
+	}
203
+
204
+
205
+	public function getNamedRoutes()
206
+	{
207
+		return $this->namedRoutes;
208
+	}
209
+
210
+	public function getBasePath()
211
+	{
212
+		return $this->basePath;
213
+	}
214
+
215
+	public function setServer($server)
216
+	{
217
+		$this->server = $server;
218
+	}
219 219
 }
Please login to merge, or discard this patch.
src/RouterParser.php 2 patches
Spacing   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -172,7 +172,7 @@
 block discarded – undo
172 172
     {
173 173
         $cPointer = $nPointer;
174 174
         $regex = in_array($cPointer, array('[', '(', '.'));
175
-        if (!$regex && isset($routeString[$iPointer+1])) {
175
+        if (!$regex && isset($routeString[$iPointer + 1])) {
176 176
             $nPointer = $routeString[$iPointer + 1];
177 177
             $regex = in_array($nPointer, array('?', '+', '*', '{'));
178 178
         }
Please login to merge, or discard this patch.
Indentation   +208 added lines, -208 removed lines patch added patch discarded remove patch
@@ -3,212 +3,212 @@
 block discarded – undo
3 3
 
4 4
 class RouterParser implements RouterParserInterface
5 5
 {
6
-    protected $params = [];
7
-    protected $matchTypes = [
8
-        'i'  => '[0-9]++',
9
-        'a'  => '[0-9A-Za-z]++',
10
-        'h'  => '[0-9A-Fa-f]++',
11
-        '*'  => '.+?',
12
-        '**' => '.++',
13
-        ''   => '[^/\.]++'
14
-    ];
15
-
16
-    /**
17
-     * Create router in one call from config.
18
-     *
19
-     * @param array $matchTypes
20
-     */
21
-    public function __construct($matchTypes = [])
22
-    {
23
-        $this->setMatchTypes($matchTypes);
24
-    }
25
-
26
-    /**
27
-     * Add named match types. It uses array_merge so keys can be overwritten.
28
-     *
29
-     * @param array $matchTypes The key is the name and the value is the regex.
30
-     */
31
-    public function setMatchTypes($matchTypes)
32
-    {
33
-        $this->matchTypes = array_merge($this->matchTypes, $matchTypes);
34
-    }
35
-
36
-    /**
37
-     * Get the url from a route name
38
-     *
39
-     * @param string $basePath
40
-     * @param string $route
41
-     * @param array $params
42
-     *
43
-     * @return string
44
-     */
45
-    public function generateUrl($basePath, $route, array $params)
46
-    {
47
-        $url = $basePath . $route;
48
-
49
-        if (preg_match_all('`(/|\.|)\[([^:\]]*+)(?::([^:\]]*+))?\](\?|)`', $route, $matches, PREG_SET_ORDER)) {
50
-            foreach ($matches as $match) {
51
-                $pre   = $match[1];
52
-                $param = $match[3];
53
-                $block = $pre ? substr($match[0], 1) : $match[0];
54
-
55
-                if (isset($params[$param])) {
56
-                    $url = str_replace($block, $params[$param], $url);
57
-                } elseif ($match[4]) {
58
-                    $url = str_replace($pre . $block, '', $url);
59
-                }
60
-            }
61
-        }
62
-
63
-        return $url;
64
-    }
65
-
66
-    /**
67
-     * @param string $method
68
-     * @param string $requestMethod
69
-     * @param string $routeString
70
-     * @param string $requestUrl
71
-     *
72
-     * @return mixed
73
-     */
74
-    public function methodMatch($method, $requestMethod, $routeString, $requestUrl)
75
-    {
76
-        $methods = explode('|', $method);
77
-
78
-        if (preg_grep("/{$requestMethod}/i", $methods)) {
79
-            if ($routeString == '*') {
80
-                return true;
81
-            } elseif (isset($routeString[0]) && $routeString[0] == '@') {
82
-                return preg_match('`' . substr($routeString, 1) . '`u', $requestUrl, $this->params);
83
-            } elseif (($position = strpos($routeString, '[')) === false) {
84
-                return strcmp($requestUrl, $routeString) === 0;
85
-            }
86
-            if (strncmp($requestUrl, $routeString, $position) !== 0) {
87
-                return false;
88
-            }
89
-
90
-            return preg_match($this->compileRoute($routeString, $requestUrl), $requestUrl, $this->params);
91
-        }
92
-
93
-        return false;
94
-    }
95
-
96
-    /**
97
-     * Compile the regex for a given route (EXPENSIVE)
98
-     *
99
-     * @param $routeString
100
-     * @param $requestUrl
101
-     *
102
-     * @return string
103
-     */
104
-    private function compileRoute($routeString, $requestUrl)
105
-    {
106
-        $route = $this->getRoute($routeString, $requestUrl);
107
-
108
-        if (preg_match_all('`(/|\.|)\[([^:\]]*+)(?::([^:\]]*+))?\](\?|)`', $route, $matches, PREG_SET_ORDER)) {
109
-            $matchTypes = $this->matchTypes;
110
-            foreach ($matches as $match) {
111
-                $pattern = $this->getRoutePattern($matchTypes, $match[1], $match[2], $match[3], $match[4]);
112
-                $route   = str_replace($match[0], $pattern, $route);
113
-            }
114
-        }
115
-
116
-        return "`^$route$`u";
117
-    }
118
-
119
-    /**
120
-     * @param $matchTypes
121
-     * @param $pre
122
-     * @param $type
123
-     * @param $param
124
-     * @param $optional
125
-     *
126
-     * @return string
127
-     */
128
-    private function getRoutePattern($matchTypes, $pre, $type, $param, $optional)
129
-    {
130
-        if (isset($matchTypes[$type])) {
131
-            $type = $matchTypes[$type];
132
-        }
133
-        if ($pre === '.') {
134
-            $pre = '\.';
135
-        }
136
-
137
-        //Older versions of PCRE require the 'P' in (?P<named>)
138
-        return '(?:'
139
-            . (!empty($pre) ? $pre : null)
140
-            . '('
141
-            . (!empty($param) ? "?P<$param>" : null)
142
-            . $type
143
-            . '))'
144
-            . (!empty($optional) ? '?' : null);
145
-    }
146
-
147
-    /**
148
-     * @param $routeString
149
-     * @param $requestUrl
150
-     *
151
-     * @return bool|string
152
-     */
153
-    private function getRoute($routeString, $requestUrl)
154
-    {
155
-        $iPointer = $jPointer = 0;
156
-        $nPointer = isset($routeString[0]) ? $routeString[0] : null;
157
-        $regex = $route = false;
158
-
159
-        // Find the longest non-regex substring and match it against the URI
160
-        while (true) {
161
-            if (!isset($routeString[$iPointer])) {
162
-                break;
163
-            }
164
-            if ($regex === false) {
165
-                if (!$this->isValidRouteRegex($nPointer, $jPointer, $iPointer, $routeString, $requestUrl)) {
166
-                    continue;
167
-                }
168
-                $jPointer++;
169
-            }
170
-            $route .= $routeString[$iPointer++];
171
-        }
172
-
173
-        return $route;
174
-    }
175
-
176
-    /**
177
-     * @param $nPointer
178
-     * @param $jPointer
179
-     * @param $iPointer
180
-     * @param $routeString
181
-     * @param $requestUrl
182
-     *
183
-     * @return bool
184
-     */
185
-    private function isValidRouteRegex($nPointer, $jPointer, $iPointer, $routeString, $requestUrl)
186
-    {
187
-        $cPointer = $nPointer;
188
-        $regex = in_array($cPointer, array('[', '(', '.'));
189
-        if (!$regex && isset($routeString[$iPointer+1])) {
190
-            $nPointer = $routeString[$iPointer + 1];
191
-            $regex = in_array($nPointer, array('?', '+', '*', '{'));
192
-        }
193
-        if (!$regex && $cPointer !== '/' && (!isset($requestUrl[$jPointer]) || $cPointer !== $requestUrl[$jPointer])) {
194
-            return false;
195
-        }
196
-        return true;
197
-    }
198
-
199
-    /**
200
-     * @return array
201
-     */
202
-    public function getParams()
203
-    {
204
-        return $this->params;
205
-    }
206
-
207
-    /**
208
-     * @return array
209
-     */
210
-    public function getMatchTypes()
211
-    {
212
-        return $this->matchTypes;
213
-    }
6
+	protected $params = [];
7
+	protected $matchTypes = [
8
+		'i'  => '[0-9]++',
9
+		'a'  => '[0-9A-Za-z]++',
10
+		'h'  => '[0-9A-Fa-f]++',
11
+		'*'  => '.+?',
12
+		'**' => '.++',
13
+		''   => '[^/\.]++'
14
+	];
15
+
16
+	/**
17
+	 * Create router in one call from config.
18
+	 *
19
+	 * @param array $matchTypes
20
+	 */
21
+	public function __construct($matchTypes = [])
22
+	{
23
+		$this->setMatchTypes($matchTypes);
24
+	}
25
+
26
+	/**
27
+	 * Add named match types. It uses array_merge so keys can be overwritten.
28
+	 *
29
+	 * @param array $matchTypes The key is the name and the value is the regex.
30
+	 */
31
+	public function setMatchTypes($matchTypes)
32
+	{
33
+		$this->matchTypes = array_merge($this->matchTypes, $matchTypes);
34
+	}
35
+
36
+	/**
37
+	 * Get the url from a route name
38
+	 *
39
+	 * @param string $basePath
40
+	 * @param string $route
41
+	 * @param array $params
42
+	 *
43
+	 * @return string
44
+	 */
45
+	public function generateUrl($basePath, $route, array $params)
46
+	{
47
+		$url = $basePath . $route;
48
+
49
+		if (preg_match_all('`(/|\.|)\[([^:\]]*+)(?::([^:\]]*+))?\](\?|)`', $route, $matches, PREG_SET_ORDER)) {
50
+			foreach ($matches as $match) {
51
+				$pre   = $match[1];
52
+				$param = $match[3];
53
+				$block = $pre ? substr($match[0], 1) : $match[0];
54
+
55
+				if (isset($params[$param])) {
56
+					$url = str_replace($block, $params[$param], $url);
57
+				} elseif ($match[4]) {
58
+					$url = str_replace($pre . $block, '', $url);
59
+				}
60
+			}
61
+		}
62
+
63
+		return $url;
64
+	}
65
+
66
+	/**
67
+	 * @param string $method
68
+	 * @param string $requestMethod
69
+	 * @param string $routeString
70
+	 * @param string $requestUrl
71
+	 *
72
+	 * @return mixed
73
+	 */
74
+	public function methodMatch($method, $requestMethod, $routeString, $requestUrl)
75
+	{
76
+		$methods = explode('|', $method);
77
+
78
+		if (preg_grep("/{$requestMethod}/i", $methods)) {
79
+			if ($routeString == '*') {
80
+				return true;
81
+			} elseif (isset($routeString[0]) && $routeString[0] == '@') {
82
+				return preg_match('`' . substr($routeString, 1) . '`u', $requestUrl, $this->params);
83
+			} elseif (($position = strpos($routeString, '[')) === false) {
84
+				return strcmp($requestUrl, $routeString) === 0;
85
+			}
86
+			if (strncmp($requestUrl, $routeString, $position) !== 0) {
87
+				return false;
88
+			}
89
+
90
+			return preg_match($this->compileRoute($routeString, $requestUrl), $requestUrl, $this->params);
91
+		}
92
+
93
+		return false;
94
+	}
95
+
96
+	/**
97
+	 * Compile the regex for a given route (EXPENSIVE)
98
+	 *
99
+	 * @param $routeString
100
+	 * @param $requestUrl
101
+	 *
102
+	 * @return string
103
+	 */
104
+	private function compileRoute($routeString, $requestUrl)
105
+	{
106
+		$route = $this->getRoute($routeString, $requestUrl);
107
+
108
+		if (preg_match_all('`(/|\.|)\[([^:\]]*+)(?::([^:\]]*+))?\](\?|)`', $route, $matches, PREG_SET_ORDER)) {
109
+			$matchTypes = $this->matchTypes;
110
+			foreach ($matches as $match) {
111
+				$pattern = $this->getRoutePattern($matchTypes, $match[1], $match[2], $match[3], $match[4]);
112
+				$route   = str_replace($match[0], $pattern, $route);
113
+			}
114
+		}
115
+
116
+		return "`^$route$`u";
117
+	}
118
+
119
+	/**
120
+	 * @param $matchTypes
121
+	 * @param $pre
122
+	 * @param $type
123
+	 * @param $param
124
+	 * @param $optional
125
+	 *
126
+	 * @return string
127
+	 */
128
+	private function getRoutePattern($matchTypes, $pre, $type, $param, $optional)
129
+	{
130
+		if (isset($matchTypes[$type])) {
131
+			$type = $matchTypes[$type];
132
+		}
133
+		if ($pre === '.') {
134
+			$pre = '\.';
135
+		}
136
+
137
+		//Older versions of PCRE require the 'P' in (?P<named>)
138
+		return '(?:'
139
+			. (!empty($pre) ? $pre : null)
140
+			. '('
141
+			. (!empty($param) ? "?P<$param>" : null)
142
+			. $type
143
+			. '))'
144
+			. (!empty($optional) ? '?' : null);
145
+	}
146
+
147
+	/**
148
+	 * @param $routeString
149
+	 * @param $requestUrl
150
+	 *
151
+	 * @return bool|string
152
+	 */
153
+	private function getRoute($routeString, $requestUrl)
154
+	{
155
+		$iPointer = $jPointer = 0;
156
+		$nPointer = isset($routeString[0]) ? $routeString[0] : null;
157
+		$regex = $route = false;
158
+
159
+		// Find the longest non-regex substring and match it against the URI
160
+		while (true) {
161
+			if (!isset($routeString[$iPointer])) {
162
+				break;
163
+			}
164
+			if ($regex === false) {
165
+				if (!$this->isValidRouteRegex($nPointer, $jPointer, $iPointer, $routeString, $requestUrl)) {
166
+					continue;
167
+				}
168
+				$jPointer++;
169
+			}
170
+			$route .= $routeString[$iPointer++];
171
+		}
172
+
173
+		return $route;
174
+	}
175
+
176
+	/**
177
+	 * @param $nPointer
178
+	 * @param $jPointer
179
+	 * @param $iPointer
180
+	 * @param $routeString
181
+	 * @param $requestUrl
182
+	 *
183
+	 * @return bool
184
+	 */
185
+	private function isValidRouteRegex($nPointer, $jPointer, $iPointer, $routeString, $requestUrl)
186
+	{
187
+		$cPointer = $nPointer;
188
+		$regex = in_array($cPointer, array('[', '(', '.'));
189
+		if (!$regex && isset($routeString[$iPointer+1])) {
190
+			$nPointer = $routeString[$iPointer + 1];
191
+			$regex = in_array($nPointer, array('?', '+', '*', '{'));
192
+		}
193
+		if (!$regex && $cPointer !== '/' && (!isset($requestUrl[$jPointer]) || $cPointer !== $requestUrl[$jPointer])) {
194
+			return false;
195
+		}
196
+		return true;
197
+	}
198
+
199
+	/**
200
+	 * @return array
201
+	 */
202
+	public function getParams()
203
+	{
204
+		return $this->params;
205
+	}
206
+
207
+	/**
208
+	 * @return array
209
+	 */
210
+	public function getMatchTypes()
211
+	{
212
+		return $this->matchTypes;
213
+	}
214 214
 }
Please login to merge, or discard this patch.
src/Tests/SimpleTraversable.php 1 patch
Indentation   +25 added lines, -25 removed lines patch added patch discarded remove patch
@@ -5,31 +5,31 @@
 block discarded – undo
5 5
 
6 6
 class SimpleTraversable implements Iterator
7 7
 {
8
-    protected $position = 0;
8
+	protected $position = 0;
9 9
 
10
-    protected $data = array(
11
-        array('GET', '/foo', 'foo_action', null),
12
-        array('POST', '/bar', 'bar_action', 'second_route')
13
-    );
10
+	protected $data = array(
11
+		array('GET', '/foo', 'foo_action', null),
12
+		array('POST', '/bar', 'bar_action', 'second_route')
13
+	);
14 14
 
15
-    public function current()
16
-    {
17
-        return $this->data[$this->position];
18
-    }
19
-    public function key()
20
-    {
21
-        return $this->position;
22
-    }
23
-    public function next()
24
-    {
25
-        ++$this->position;
26
-    }
27
-    public function rewind()
28
-    {
29
-        $this->position = 0;
30
-    }
31
-    public function valid()
32
-    {
33
-        return isset($this->data[$this->position]);
34
-    }
15
+	public function current()
16
+	{
17
+		return $this->data[$this->position];
18
+	}
19
+	public function key()
20
+	{
21
+		return $this->position;
22
+	}
23
+	public function next()
24
+	{
25
+		++$this->position;
26
+	}
27
+	public function rewind()
28
+	{
29
+		$this->position = 0;
30
+	}
31
+	public function valid()
32
+	{
33
+		return isset($this->data[$this->position]);
34
+	}
35 35
 }
Please login to merge, or discard this patch.
src/RouterParserInterface.php 1 patch
Indentation   +28 added lines, -28 removed lines patch added patch discarded remove patch
@@ -3,34 +3,34 @@
 block discarded – undo
3 3
 
4 4
 interface RouterParserInterface
5 5
 {
6
-    /**
7
-     * Get the route parameters
8
-     *
9
-     * @return array
10
-     */
11
-    public function getParams();
6
+	/**
7
+	 * Get the route parameters
8
+	 *
9
+	 * @return array
10
+	 */
11
+	public function getParams();
12 12
 
13
-    /**
14
-     * Get the url from a route name
15
-     *
16
-     * @param string $basePath
17
-     * @param string $route
18
-     * @param array $params
19
-     *
20
-     * @return string
21
-     */
22
-    public function generateUrl($basePath, $route, array $params);
13
+	/**
14
+	 * Get the url from a route name
15
+	 *
16
+	 * @param string $basePath
17
+	 * @param string $route
18
+	 * @param array $params
19
+	 *
20
+	 * @return string
21
+	 */
22
+	public function generateUrl($basePath, $route, array $params);
23 23
 
24
-    /**
25
-     * Check if the request method match the route methods
26
-     * Update the $params
27
-     *
28
-     * @param string $method
29
-     * @param string $requestMethod
30
-     * @param string $routeString
31
-     * @param string $requestUrl
32
-     *
33
-     * @return mixed
34
-     */
35
-    public function methodMatch($method, $requestMethod, $routeString, $requestUrl);
24
+	/**
25
+	 * Check if the request method match the route methods
26
+	 * Update the $params
27
+	 *
28
+	 * @param string $method
29
+	 * @param string $requestMethod
30
+	 * @param string $routeString
31
+	 * @param string $requestUrl
32
+	 *
33
+	 * @return mixed
34
+	 */
35
+	public function methodMatch($method, $requestMethod, $routeString, $requestUrl);
36 36
 }
Please login to merge, or discard this patch.
src/Tests/RouterTest.php 2 patches
Indentation   +277 added lines, -277 removed lines patch added patch discarded remove patch
@@ -11,336 +11,336 @@
 block discarded – undo
11 11
 
12 12
 class RouterTest extends PHPUnit_Framework_TestCase
13 13
 {
14
-    /**
15
-     * @var Router
16
-     */
17
-    protected $router;
18
-    protected $closure;
19
-    protected $param1;
20
-    protected $param2;
14
+	/**
15
+	 * @var Router
16
+	 */
17
+	protected $router;
18
+	protected $closure;
19
+	protected $param1;
20
+	protected $param2;
21 21
 
22
-    /**
23
-     * Sets up the fixture, for example, opens a network connection.
24
-     * This method is called before a test is executed.
25
-     */
26
-    protected function setUp()
27
-    {
28
-        $parser = new RouterParser();
29
-        $this->router  = new Router($parser, [], '', []);
30
-        $this->param1  = ['controller' => 'test', 'action' => 'someaction'];
31
-        $this->param2  = ['controller' => 'test', 'action' => 'someaction', 'type' => 'json'];
32
-        $this->closure = function () {
33
-        };
34
-    }
22
+	/**
23
+	 * Sets up the fixture, for example, opens a network connection.
24
+	 * This method is called before a test is executed.
25
+	 */
26
+	protected function setUp()
27
+	{
28
+		$parser = new RouterParser();
29
+		$this->router  = new Router($parser, [], '', []);
30
+		$this->param1  = ['controller' => 'test', 'action' => 'someaction'];
31
+		$this->param2  = ['controller' => 'test', 'action' => 'someaction', 'type' => 'json'];
32
+		$this->closure = function () {
33
+		};
34
+	}
35 35
 
36
-    /**
37
-     * @covers Router::getRoutes
38
-     */
39
-    public function testMapAndGetRoutes()
40
-    {
41
-        $route = ['POST', '/[:controller]/[:action]', $this->closure, null];
36
+	/**
37
+	 * @covers Router::getRoutes
38
+	 */
39
+	public function testMapAndGetRoutes()
40
+	{
41
+		$route = ['POST', '/[:controller]/[:action]', $this->closure, null];
42 42
 
43
-        call_user_func_array([$this->router, 'map'], $route);
43
+		call_user_func_array([$this->router, 'map'], $route);
44 44
 
45
-        $routes = $this->router->getRoutes();
45
+		$routes = $this->router->getRoutes();
46 46
 
47
-        $this->assertInternalType('array', $routes);
48
-        $this->assertEquals([$route], $routes);
49
-    }
47
+		$this->assertInternalType('array', $routes);
48
+		$this->assertEquals([$route], $routes);
49
+	}
50 50
 
51
-    /**
52
-     * @covers Router::setRoutes
53
-     */
54
-    public function testAddRoutesWithArrayArgument()
55
-    {
56
-        $route1 = ['POST', '/[:controller]/[:action]', $this->closure, null];
57
-        $route2 = ['POST', '/[:controller]/[:action]', $this->closure, 'second_route'];
51
+	/**
52
+	 * @covers Router::setRoutes
53
+	 */
54
+	public function testAddRoutesWithArrayArgument()
55
+	{
56
+		$route1 = ['POST', '/[:controller]/[:action]', $this->closure, null];
57
+		$route2 = ['POST', '/[:controller]/[:action]', $this->closure, 'second_route'];
58 58
 
59
-        $this->router->setRoutes([$route1, $route2]);
59
+		$this->router->setRoutes([$route1, $route2]);
60 60
         
61
-        $routes = $this->router->getRoutes();
61
+		$routes = $this->router->getRoutes();
62 62
         
63
-        $this->assertEquals($route1, $routes[0]);
64
-        $this->assertEquals($route2, $routes[1]);
65
-    }
63
+		$this->assertEquals($route1, $routes[0]);
64
+		$this->assertEquals($route2, $routes[1]);
65
+	}
66 66
 
67
-    /**
68
-     * @covers Router::setRoutes
69
-     */
70
-    public function testAddRoutesWithTraversableArgument()
71
-    {
72
-        $traversable = new SimpleTraversable();
73
-        $this->router->setRoutes($traversable);
67
+	/**
68
+	 * @covers Router::setRoutes
69
+	 */
70
+	public function testAddRoutesWithTraversableArgument()
71
+	{
72
+		$traversable = new SimpleTraversable();
73
+		$this->router->setRoutes($traversable);
74 74
         
75
-        $traversable->rewind();
75
+		$traversable->rewind();
76 76
         
77
-        $first = $traversable->current();
78
-        $traversable->next();
79
-        $second = $traversable->current();
77
+		$first = $traversable->current();
78
+		$traversable->next();
79
+		$second = $traversable->current();
80 80
         
81
-        $routes = $this->router->getRoutes();
81
+		$routes = $this->router->getRoutes();
82 82
         
83
-        $this->assertEquals($first, $routes[0]);
84
-        $this->assertEquals($second, $routes[1]);
85
-    }
83
+		$this->assertEquals($first, $routes[0]);
84
+		$this->assertEquals($second, $routes[1]);
85
+	}
86 86
 
87
-    /**
88
-     * @covers Router::setRoutes
89
-     */
90
-    public function testAddRoutesWidthInvalidArgument()
91
-    {
92
-        try {
93
-            $this->router->setRoutes(new stdClass);
94
-        } catch (RouterException $e) {
95
-            $this->assertEquals('Routes should be an array or an instance of Iterator', $e->getMessage());
96
-        }
97
-    }
87
+	/**
88
+	 * @covers Router::setRoutes
89
+	 */
90
+	public function testAddRoutesWidthInvalidArgument()
91
+	{
92
+		try {
93
+			$this->router->setRoutes(new stdClass);
94
+		} catch (RouterException $e) {
95
+			$this->assertEquals('Routes should be an array or an instance of Iterator', $e->getMessage());
96
+		}
97
+	}
98 98
 
99
-    /**
100
-     * @covers Router::setBasePath
101
-     */
102
-    public function testSetBasePath()
103
-    {
104
-        $this->router->setBasePath('/some/path');
105
-        $this->assertEquals('/some/path', $this->router->getBasePath());
106
-    }
99
+	/**
100
+	 * @covers Router::setBasePath
101
+	 */
102
+	public function testSetBasePath()
103
+	{
104
+		$this->router->setBasePath('/some/path');
105
+		$this->assertEquals('/some/path', $this->router->getBasePath());
106
+	}
107 107
 
108
-    /**
109
-     * @covers Router::map
110
-     */
111
-    public function testMapWithName()
112
-    {
113
-        $route = ['POST', '/[:controller]/[:action]', $this->closure, 'my_route'];
108
+	/**
109
+	 * @covers Router::map
110
+	 */
111
+	public function testMapWithName()
112
+	{
113
+		$route = ['POST', '/[:controller]/[:action]', $this->closure, 'my_route'];
114 114
 
115
-        call_user_func_array([$this->router, 'map'], $route);
115
+		call_user_func_array([$this->router, 'map'], $route);
116 116
 
117
-        $routes = $this->router->getRoutes();
118
-        $namedRoutes = $this->router->getNamedRoutes();
117
+		$routes = $this->router->getRoutes();
118
+		$namedRoutes = $this->router->getNamedRoutes();
119 119
 
120
-        $this->assertInternalType('array', $routes);
121
-        $this->assertEquals($route, $routes[0]);
120
+		$this->assertInternalType('array', $routes);
121
+		$this->assertEquals($route, $routes[0]);
122 122
 
123
-        $this->assertInternalType('array', $namedRoutes);
124
-        $this->assertEquals('/[:controller]/[:action]', $namedRoutes['my_route']);
125
-    }
123
+		$this->assertInternalType('array', $namedRoutes);
124
+		$this->assertEquals('/[:controller]/[:action]', $namedRoutes['my_route']);
125
+	}
126 126
 
127
-    /**
128
-     * @covers Router::map
129
-     */
130
-    public function testMapWithDuplicatedRouteName()
131
-    {
132
-        try {
133
-            $route = ['POST', '/[:controller]/[:action]', $this->closure, 'my_route'];
134
-            call_user_func_array([$this->router, 'map'], $route);
135
-            call_user_func_array([$this->router, 'map'], $route);
136
-        } catch (Exception $e) {
137
-            $this->assertEquals("Can not redeclare route 'my_route'", $e->getMessage());
138
-        }
139
-    }
127
+	/**
128
+	 * @covers Router::map
129
+	 */
130
+	public function testMapWithDuplicatedRouteName()
131
+	{
132
+		try {
133
+			$route = ['POST', '/[:controller]/[:action]', $this->closure, 'my_route'];
134
+			call_user_func_array([$this->router, 'map'], $route);
135
+			call_user_func_array([$this->router, 'map'], $route);
136
+		} catch (Exception $e) {
137
+			$this->assertEquals("Can not redeclare route 'my_route'", $e->getMessage());
138
+		}
139
+	}
140 140
 
141
-    /**
142
-     * @covers Router::generate
143
-     */
144
-    public function testGenerate()
145
-    {
146
-        $this->router->map('GET', '/[:controller]/[:action]', $this->closure, 'foo_route');
141
+	/**
142
+	 * @covers Router::generate
143
+	 */
144
+	public function testGenerate()
145
+	{
146
+		$this->router->map('GET', '/[:controller]/[:action]', $this->closure, 'foo_route');
147 147
         
148
-        $this->assertEquals('/test/someaction', $this->router->generate('foo_route', $this->param1));
149
-        $this->assertEquals('/test/someaction', $this->router->generate('foo_route', $this->param2));
150
-    }
148
+		$this->assertEquals('/test/someaction', $this->router->generate('foo_route', $this->param1));
149
+		$this->assertEquals('/test/someaction', $this->router->generate('foo_route', $this->param2));
150
+	}
151 151
 
152
-    /**
153
-     * @covers Router::generate
154
-     */
155
-    public function testGenerateWithOptionalUrlParts()
156
-    {
157
-        $this->router->map('GET', '/[:controller]/[:action].[:type]?', $this->closure, 'bar_route');
152
+	/**
153
+	 * @covers Router::generate
154
+	 */
155
+	public function testGenerateWithOptionalUrlParts()
156
+	{
157
+		$this->router->map('GET', '/[:controller]/[:action].[:type]?', $this->closure, 'bar_route');
158 158
         
159
-        $this->assertEquals('/test/someaction', $this->router->generate('bar_route', $this->param1));
160
-        $this->assertEquals('/test/someaction.json', $this->router->generate('bar_route', $this->param2));
161
-    }
159
+		$this->assertEquals('/test/someaction', $this->router->generate('bar_route', $this->param1));
160
+		$this->assertEquals('/test/someaction.json', $this->router->generate('bar_route', $this->param2));
161
+	}
162 162
 
163
-    /**
164
-     * @covers Router::generate
165
-     */
166
-    public function testGenerateWithNonExistingRoute()
167
-    {
168
-        try {
169
-            $this->router->generate('non_existing_route');
170
-        } catch (Exception $e) {
171
-            $this->assertEquals("Route 'non_existing_route' does not exist.", $e->getMessage());
172
-        }
173
-    }
163
+	/**
164
+	 * @covers Router::generate
165
+	 */
166
+	public function testGenerateWithNonExistingRoute()
167
+	{
168
+		try {
169
+			$this->router->generate('non_existing_route');
170
+		} catch (Exception $e) {
171
+			$this->assertEquals("Route 'non_existing_route' does not exist.", $e->getMessage());
172
+		}
173
+	}
174 174
     
175
-    /**
176
-     * @covers Router::match
177
-     * @covers Router::compileRoute
178
-     */
179
-    public function testMatch()
180
-    {
181
-        $params = [
182
-            'target' => 'foo_action',
183
-            'params' => ['controller' => 'test', 'action' => 'do'],
184
-            'name'   => 'foo_route'
185
-        ];
175
+	/**
176
+	 * @covers Router::match
177
+	 * @covers Router::compileRoute
178
+	 */
179
+	public function testMatch()
180
+	{
181
+		$params = [
182
+			'target' => 'foo_action',
183
+			'params' => ['controller' => 'test', 'action' => 'do'],
184
+			'name'   => 'foo_route'
185
+		];
186 186
 
187
-        $this->router->map('GET', '/foo/[:controller]/[:action]', 'foo_action', 'foo_route');
187
+		$this->router->map('GET', '/foo/[:controller]/[:action]', 'foo_action', 'foo_route');
188 188
         
189
-        $this->assertEquals($params, $this->router->match('/foo/test/do', 'GET'));
190
-        $this->assertSame(false, $this->router->match('/foo/test/do', 'POST'));
191
-        $this->assertEquals($params, $this->router->match('/foo/test/do?param=value', 'GET'));
192
-    }
189
+		$this->assertEquals($params, $this->router->match('/foo/test/do', 'GET'));
190
+		$this->assertSame(false, $this->router->match('/foo/test/do', 'POST'));
191
+		$this->assertEquals($params, $this->router->match('/foo/test/do?param=value', 'GET'));
192
+	}
193 193
     
194
-    public function testMatchWithFixedParamValues()
195
-    {
196
-        $params = [
197
-            'target' => 'usersController#doAction',
198
-            'params' => ['id' => 1, 'action' => 'delete'],
199
-            'name' => 'users_do'
200
-        ];
194
+	public function testMatchWithFixedParamValues()
195
+	{
196
+		$params = [
197
+			'target' => 'usersController#doAction',
198
+			'params' => ['id' => 1, 'action' => 'delete'],
199
+			'name' => 'users_do'
200
+		];
201 201
 
202
-        $this->router->map('POST', '/users/[i:id]/[delete|update:action]', 'usersController#doAction', 'users_do');
202
+		$this->router->map('POST', '/users/[i:id]/[delete|update:action]', 'usersController#doAction', 'users_do');
203 203
         
204
-        $this->assertEquals($params, $this->router->match('/users/1/delete', 'POST'));
205
-        $this->assertFalse($this->router->match('/users/1/delete', 'GET'));
206
-        $this->assertFalse($this->router->match('/users/abc/delete', 'POST'));
207
-        $this->assertFalse($this->router->match('/users/1/create', 'GET'));
208
-    }
204
+		$this->assertEquals($params, $this->router->match('/users/1/delete', 'POST'));
205
+		$this->assertFalse($this->router->match('/users/1/delete', 'GET'));
206
+		$this->assertFalse($this->router->match('/users/abc/delete', 'POST'));
207
+		$this->assertFalse($this->router->match('/users/1/create', 'GET'));
208
+	}
209 209
     
210
-    public function testMatchWithServerVars()
211
-    {
212
-        $this->router->map('GET', '/foo/[:controller]/[:action]', 'foo_action', 'foo_route');
210
+	public function testMatchWithServerVars()
211
+	{
212
+		$this->router->map('GET', '/foo/[:controller]/[:action]', 'foo_action', 'foo_route');
213 213
         
214
-        $this->router->setServer([
215
-            'REQUEST_URI' => '/foo/test/do',
216
-            'REQUEST_METHOD' => 'GET'
217
-        ]);
214
+		$this->router->setServer([
215
+			'REQUEST_URI' => '/foo/test/do',
216
+			'REQUEST_METHOD' => 'GET'
217
+		]);
218 218
         
219
-        $this->assertEquals(array(
220
-            'target' => 'foo_action',
221
-            'params' => array(
222
-                'controller' => 'test',
223
-                'action' => 'do'
224
-            ),
225
-            'name' => 'foo_route'
226
-        ), $this->router->match());
227
-    }
219
+		$this->assertEquals(array(
220
+			'target' => 'foo_action',
221
+			'params' => array(
222
+				'controller' => 'test',
223
+				'action' => 'do'
224
+			),
225
+			'name' => 'foo_route'
226
+		), $this->router->match());
227
+	}
228 228
     
229
-    public function testMatchWithOptionalUrlParts()
230
-    {
231
-        $this->router->map('GET', '/bar/[:controller]/[:action].[:type]?', 'bar_action', 'bar_route');
229
+	public function testMatchWithOptionalUrlParts()
230
+	{
231
+		$this->router->map('GET', '/bar/[:controller]/[:action].[:type]?', 'bar_action', 'bar_route');
232 232
         
233
-        $this->assertEquals(array(
234
-            'target' => 'bar_action',
235
-            'params' => array(
236
-                'controller' => 'test',
237
-                'action' => 'do',
238
-                'type' => 'json'
239
-            ),
240
-            'name' => 'bar_route'
241
-        ), $this->router->match('/bar/test/do.json', 'GET'));
242
-    }
233
+		$this->assertEquals(array(
234
+			'target' => 'bar_action',
235
+			'params' => array(
236
+				'controller' => 'test',
237
+				'action' => 'do',
238
+				'type' => 'json'
239
+			),
240
+			'name' => 'bar_route'
241
+		), $this->router->match('/bar/test/do.json', 'GET'));
242
+	}
243 243
     
244
-    public function testMatchWithWildcard()
245
-    {
246
-        $this->router->map('GET', '/a', 'foo_action', 'foo_route');
247
-        $this->router->map('GET', '*', 'bar_action', 'bar_route');
244
+	public function testMatchWithWildcard()
245
+	{
246
+		$this->router->map('GET', '/a', 'foo_action', 'foo_route');
247
+		$this->router->map('GET', '*', 'bar_action', 'bar_route');
248 248
         
249
-        $this->assertEquals(array(
250
-            'target' => 'bar_action',
251
-            'params' => array(),
252
-            'name' => 'bar_route'
253
-        ), $this->router->match('/everything', 'GET'));
254
-    }
249
+		$this->assertEquals(array(
250
+			'target' => 'bar_action',
251
+			'params' => array(),
252
+			'name' => 'bar_route'
253
+		), $this->router->match('/everything', 'GET'));
254
+	}
255 255
     
256
-    public function testMatchWithCustomRegexp()
257
-    {
258
-        $this->router->map('GET', '@^/[a-z]*$', 'bar_action', 'bar_route');
256
+	public function testMatchWithCustomRegexp()
257
+	{
258
+		$this->router->map('GET', '@^/[a-z]*$', 'bar_action', 'bar_route');
259 259
         
260
-        $this->assertEquals(array(
261
-            'target' => 'bar_action',
262
-            'params' => array(),
263
-            'name' => 'bar_route'
264
-        ), $this->router->match('/everything', 'GET'));
260
+		$this->assertEquals(array(
261
+			'target' => 'bar_action',
262
+			'params' => array(),
263
+			'name' => 'bar_route'
264
+		), $this->router->match('/everything', 'GET'));
265 265
         
266
-        $this->assertFalse($this->router->match('/some-other-thing', 'GET'));
267
-    }
266
+		$this->assertFalse($this->router->match('/some-other-thing', 'GET'));
267
+	}
268 268
 
269
-    public function testMatchWithUnicodeRegex()
270
-    {
271
-        $pattern = '/(?<path>[^';
272
-        // Arabic characters
273
-        $pattern .= '\x{0600}-\x{06FF}';
274
-        $pattern .= '\x{FB50}-\x{FDFD}';
275
-        $pattern .= '\x{FE70}-\x{FEFF}';
276
-        $pattern .= '\x{0750}-\x{077F}';
277
-        // Alphanumeric, /, _, - and space characters
278
-        $pattern .= 'a-zA-Z0-9\/_\-\s';
279
-        // 'ZERO WIDTH NON-JOINER'
280
-        $pattern .= '\x{200C}';
281
-        $pattern .= ']+)';
269
+	public function testMatchWithUnicodeRegex()
270
+	{
271
+		$pattern = '/(?<path>[^';
272
+		// Arabic characters
273
+		$pattern .= '\x{0600}-\x{06FF}';
274
+		$pattern .= '\x{FB50}-\x{FDFD}';
275
+		$pattern .= '\x{FE70}-\x{FEFF}';
276
+		$pattern .= '\x{0750}-\x{077F}';
277
+		// Alphanumeric, /, _, - and space characters
278
+		$pattern .= 'a-zA-Z0-9\/_\-\s';
279
+		// 'ZERO WIDTH NON-JOINER'
280
+		$pattern .= '\x{200C}';
281
+		$pattern .= ']+)';
282 282
         
283
-        $this->router->map('GET', '@' . $pattern, 'unicode_action', 'unicode_route');
283
+		$this->router->map('GET', '@' . $pattern, 'unicode_action', 'unicode_route');
284 284
         
285
-        $this->assertEquals(array(
286
-            'target' => 'unicode_action',
287
-            'name' => 'unicode_route',
288
-            'params' => array(
289
-                'path' => '大家好'
290
-            )
291
-        ), $this->router->match('/大家好', 'GET'));
285
+		$this->assertEquals(array(
286
+			'target' => 'unicode_action',
287
+			'name' => 'unicode_route',
288
+			'params' => array(
289
+				'path' => '大家好'
290
+			)
291
+		), $this->router->match('/大家好', 'GET'));
292 292
         
293
-        $this->assertFalse($this->router->match('/﷽‎', 'GET'));
294
-    }
293
+		$this->assertFalse($this->router->match('/﷽‎', 'GET'));
294
+	}
295 295
 
296
-    /**
297
-     * @covers Router::setMatchTypes
298
-     */
299
-    public function testMatchWithCustomNamedRegex()
300
-    {
301
-        $this->router->getParser()->setMatchTypes(array('cId' => '[a-zA-Z]{2}[0-9](?:_[0-9]++)?'));
302
-        $this->router->map('GET', '/bar/[cId:customId]', 'bar_action', 'bar_route');
296
+	/**
297
+	 * @covers Router::setMatchTypes
298
+	 */
299
+	public function testMatchWithCustomNamedRegex()
300
+	{
301
+		$this->router->getParser()->setMatchTypes(array('cId' => '[a-zA-Z]{2}[0-9](?:_[0-9]++)?'));
302
+		$this->router->map('GET', '/bar/[cId:customId]', 'bar_action', 'bar_route');
303 303
         
304
-        $this->assertEquals(array(
305
-            'target' => 'bar_action',
306
-            'params' => array(
307
-                'customId' => 'AB1',
308
-            ),
309
-            'name' => 'bar_route'
310
-        ), $this->router->match('/bar/AB1', 'GET'));
304
+		$this->assertEquals(array(
305
+			'target' => 'bar_action',
306
+			'params' => array(
307
+				'customId' => 'AB1',
308
+			),
309
+			'name' => 'bar_route'
310
+		), $this->router->match('/bar/AB1', 'GET'));
311 311
 
312
-        $this->assertEquals(array(
313
-            'target' => 'bar_action',
314
-            'params' => array(
315
-                'customId' => 'AB1_0123456789',
316
-            ),
317
-            'name' => 'bar_route'
318
-        ), $this->router->match('/bar/AB1_0123456789', 'GET'));
312
+		$this->assertEquals(array(
313
+			'target' => 'bar_action',
314
+			'params' => array(
315
+				'customId' => 'AB1_0123456789',
316
+			),
317
+			'name' => 'bar_route'
318
+		), $this->router->match('/bar/AB1_0123456789', 'GET'));
319 319
         
320
-        $this->assertFalse($this->router->match('/some-other-thing', 'GET'));
321
-    }
320
+		$this->assertFalse($this->router->match('/some-other-thing', 'GET'));
321
+	}
322 322
 
323
-    public function testMatchWithCustomNamedUnicodeRegex()
324
-    {
325
-        $pattern = '[^';
326
-        // Arabic characters
327
-        $pattern .= '\x{0600}-\x{06FF}';
328
-        $pattern .= '\x{FB50}-\x{FDFD}';
329
-        $pattern .= '\x{FE70}-\x{FEFF}';
330
-        $pattern .= '\x{0750}-\x{077F}';
331
-        $pattern .= ']+';
323
+	public function testMatchWithCustomNamedUnicodeRegex()
324
+	{
325
+		$pattern = '[^';
326
+		// Arabic characters
327
+		$pattern .= '\x{0600}-\x{06FF}';
328
+		$pattern .= '\x{FB50}-\x{FDFD}';
329
+		$pattern .= '\x{FE70}-\x{FEFF}';
330
+		$pattern .= '\x{0750}-\x{077F}';
331
+		$pattern .= ']+';
332 332
         
333
-        $this->router->getParser()->setMatchTypes(array('nonArabic' => $pattern));
334
-        $this->router->map('GET', '/bar/[nonArabic:string]', 'non_arabic_action', 'non_arabic_route');
333
+		$this->router->getParser()->setMatchTypes(array('nonArabic' => $pattern));
334
+		$this->router->map('GET', '/bar/[nonArabic:string]', 'non_arabic_action', 'non_arabic_route');
335 335
 
336
-        $this->assertEquals(array(
337
-            'target' => 'non_arabic_action',
338
-            'name'   => 'non_arabic_route',
339
-            'params' => array(
340
-                'string' => 'some-path'
341
-            )
342
-        ), $this->router->match('/bar/some-path', 'GET'));
336
+		$this->assertEquals(array(
337
+			'target' => 'non_arabic_action',
338
+			'name'   => 'non_arabic_route',
339
+			'params' => array(
340
+				'string' => 'some-path'
341
+			)
342
+		), $this->router->match('/bar/some-path', 'GET'));
343 343
         
344
-        $this->assertFalse($this->router->match('/﷽‎', 'GET'));
345
-    }
344
+		$this->assertFalse($this->router->match('/﷽‎', 'GET'));
345
+	}
346 346
 }
Please login to merge, or discard this patch.
Spacing   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -29,7 +29,7 @@
 block discarded – undo
29 29
         $this->router  = new Router($parser, [], '', []);
30 30
         $this->param1  = ['controller' => 'test', 'action' => 'someaction'];
31 31
         $this->param2  = ['controller' => 'test', 'action' => 'someaction', 'type' => 'json'];
32
-        $this->closure = function () {
32
+        $this->closure = function() {
33 33
         };
34 34
     }
35 35
 
Please login to merge, or discard this patch.