Passed
Branch dev (473855)
by Alex
02:25
created
src/Collectors/ControllerCollectorTrait.php 3 patches
Unused Use Statements   -1 removed lines patch added patch discarded remove patch
@@ -15,7 +15,6 @@
 block discarded – undo
15 15
 use ReflectionClass;
16 16
 use ReflectionMethod;
17 17
 use ReflectionParameter;
18
-use Reflector;
19 18
 
20 19
 /**
21 20
  * Methods for enable the collector to make routes from a controller.
Please login to merge, or discard this patch.
Indentation   +218 added lines, -218 removed lines patch added patch discarded remove patch
@@ -26,224 +26,224 @@
 block discarded – undo
26 26
 trait ControllerCollectorTrait
27 27
 {
28 28
 
29
-    abstract public function getWildcards();
30
-    abstract public function set($method, $pattern, $action);
31
-
32
-    /**
33
-     * Define how controller actions names will be joined to form the route pattern.
34
-     *
35
-     * @var string
36
-     */
37
-
38
-    protected $controllerActionJoin = "/";
39
-
40
-    /**
41
-     * Maps all the controller methods that begins with a HTTP method, and maps the rest of
42
-     * name as a path. The path will be the method name with slashes before every camelcased 
43
-     * word and without the HTTP method prefix, and the controller name will be used to prefix
44
-     * the route pattern. e.g. ArticlesController::getCreate will generate a route to: GET articles/create
45
-     *
46
-     * @param string $controller The controller name
47
-     * @param string $prefix
48
-     *
49
-     * @throws \ReflectionException
50
-     * @return Group
51
-     */
52
-
53
-    public function controller($controller, $prefix = null)
54
-    {
55
-        $controller = new ReflectionClass($controller);
56
-        $prefix     = $prefix === null ? $this->getControllerPrefix($controller) : $prefix;
57
-        $methods    = $controller->getMethods(ReflectionMethod::IS_PUBLIC);
58
-        return $this->collectControllerRoutes($controller, $methods, "/$prefix/");
59
-    }
60
-
61
-    /**
62
-     * Maps several controllers at same time.
63
-     *
64
-     * @param string[] $controllers Controllers name.
65
-     * @throws \ReflectionException
66
-     * @return Group
67
-     */
68
-
69
-    public function controllers(array $controllers)
70
-    {
71
-        $group = new Group;
72
-        foreach ($controllers as $controller)
73
-            $group->set($this->controller($controller));
74
-        return $group;
75
-    }
76
-
77
-    /**
78
-     * Alias for Collector::controller but maps a controller without using the controller name as prefix.
79
-     *
80
-     * @param string $controller The controller name
81
-     * @throws \ReflectionException
82
-     * @return Group
83
-     */
84
-
85
-    public function controllerWithoutPrefix($controller)
86
-    {
87
-        $controller = new ReflectionClass($controller);
88
-        $methods = $controller->getMethods(ReflectionMethod::IS_PUBLIC);
89
-        return $this->collectControllerRoutes($controller, $methods, "/");
90
-    }
91
-
92
-    /**
93
-     * Alias for Collector::controllers but maps a controller without using the controller name as prefix.
94
-     *
95
-     * @param string[] $controllers
96
-     * @throws \ReflectionException
97
-     * @return Group
98
-     */
99
-
100
-    public function controllersWithoutPrefix(array $controllers)
101
-    {
102
-        $group = new Group;
103
-        foreach ($controllers as $controller)
104
-            $group->set($this->controllerWithoutPrefix($controller));
105
-        return $group;
106
-    }
107
-
108
-    /**
109
-     * @param ReflectionClass $controller
110
-     * @param string[] $methods
111
-     * @param string $prefix
112
-     *
113
-     * @return Group
114
-     */
115
-
116
-    protected function collectControllerRoutes(ReflectionClass $controller, array $methods, $prefix)
117
-    {
118
-        $group = new Group;
119
-        $controllerDefaultStrategy = $this->getAnnotatedStrategy($controller);
120
-
121
-        /** @var ReflectionMethod $method */
122
-        foreach ($methods as $method) {
123
-            $name = preg_split("~(?=[A-Z])~", $method->name);
124
-            $http = $name[0];
125
-            unset($name[0]);
29
+	abstract public function getWildcards();
30
+	abstract public function set($method, $pattern, $action);
31
+
32
+	/**
33
+	 * Define how controller actions names will be joined to form the route pattern.
34
+	 *
35
+	 * @var string
36
+	 */
37
+
38
+	protected $controllerActionJoin = "/";
39
+
40
+	/**
41
+	 * Maps all the controller methods that begins with a HTTP method, and maps the rest of
42
+	 * name as a path. The path will be the method name with slashes before every camelcased 
43
+	 * word and without the HTTP method prefix, and the controller name will be used to prefix
44
+	 * the route pattern. e.g. ArticlesController::getCreate will generate a route to: GET articles/create
45
+	 *
46
+	 * @param string $controller The controller name
47
+	 * @param string $prefix
48
+	 *
49
+	 * @throws \ReflectionException
50
+	 * @return Group
51
+	 */
52
+
53
+	public function controller($controller, $prefix = null)
54
+	{
55
+		$controller = new ReflectionClass($controller);
56
+		$prefix     = $prefix === null ? $this->getControllerPrefix($controller) : $prefix;
57
+		$methods    = $controller->getMethods(ReflectionMethod::IS_PUBLIC);
58
+		return $this->collectControllerRoutes($controller, $methods, "/$prefix/");
59
+	}
60
+
61
+	/**
62
+	 * Maps several controllers at same time.
63
+	 *
64
+	 * @param string[] $controllers Controllers name.
65
+	 * @throws \ReflectionException
66
+	 * @return Group
67
+	 */
68
+
69
+	public function controllers(array $controllers)
70
+	{
71
+		$group = new Group;
72
+		foreach ($controllers as $controller)
73
+			$group->set($this->controller($controller));
74
+		return $group;
75
+	}
76
+
77
+	/**
78
+	 * Alias for Collector::controller but maps a controller without using the controller name as prefix.
79
+	 *
80
+	 * @param string $controller The controller name
81
+	 * @throws \ReflectionException
82
+	 * @return Group
83
+	 */
84
+
85
+	public function controllerWithoutPrefix($controller)
86
+	{
87
+		$controller = new ReflectionClass($controller);
88
+		$methods = $controller->getMethods(ReflectionMethod::IS_PUBLIC);
89
+		return $this->collectControllerRoutes($controller, $methods, "/");
90
+	}
91
+
92
+	/**
93
+	 * Alias for Collector::controllers but maps a controller without using the controller name as prefix.
94
+	 *
95
+	 * @param string[] $controllers
96
+	 * @throws \ReflectionException
97
+	 * @return Group
98
+	 */
99
+
100
+	public function controllersWithoutPrefix(array $controllers)
101
+	{
102
+		$group = new Group;
103
+		foreach ($controllers as $controller)
104
+			$group->set($this->controllerWithoutPrefix($controller));
105
+		return $group;
106
+	}
107
+
108
+	/**
109
+	 * @param ReflectionClass $controller
110
+	 * @param string[] $methods
111
+	 * @param string $prefix
112
+	 *
113
+	 * @return Group
114
+	 */
115
+
116
+	protected function collectControllerRoutes(ReflectionClass $controller, array $methods, $prefix)
117
+	{
118
+		$group = new Group;
119
+		$controllerDefaultStrategy = $this->getAnnotatedStrategy($controller);
120
+
121
+		/** @var ReflectionMethod $method */
122
+		foreach ($methods as $method) {
123
+			$name = preg_split("~(?=[A-Z])~", $method->name);
124
+			$http = $name[0];
125
+			unset($name[0]);
126 126
  
127
-            if (strpos(Collector::HTTP_METHODS, $http) !== false) {
128
-                $action   = $prefix . strtolower(implode($this->controllerActionJoin, $name));
129
-                $dynamic  = $this->getMethodConstraints($method);
130
-                $strategy = $this->getAnnotatedStrategy($method);
131
-
132
-                /** @var \Codeburner\Router\Route $route */
133
-                $route = $this->set($http, "$action$dynamic", [$controller->name, $method->name]);
134
-
135
-                if ($strategy !== null) {
136
-                       $route->setStrategy($strategy);
137
-                } else $route->setStrategy($controllerDefaultStrategy);
138
-
139
-                $group->set($route);
140
-            }
141
-        }
142
-
143
-        return $group;
144
-    }
145
-
146
-    /**
147
-     * @param ReflectionClass $controller
148
-     *
149
-     * @return string
150
-     */
151
-
152
-    protected function getControllerPrefix(ReflectionClass $controller)
153
-    {
154
-        preg_match("~\@prefix\s([a-zA-Z\\\_]+)~i", (string) $controller->getDocComment(), $prefix);
155
-        return isset($prefix[1]) ? $prefix[1] : str_replace("controller", "", strtolower($controller->getShortName()));
156
-    }
157
-
158
-    /**
159
-     * @param \ReflectionMethod
160
-     * @return string
161
-     */
162
-
163
-    protected function getMethodConstraints(ReflectionMethod $method)
164
-    {
165
-        $beginPath = "";
166
-        $endPath = "";
167
-
168
-        if ($parameters = $method->getParameters()) {
169
-            $types = $this->getParamsConstraint($method);
170
-
171
-            foreach ($parameters as $parameter) {
172
-                if ($parameter->isOptional()) {
173
-                    $beginPath .= "[";
174
-                    $endPath .= "]";
175
-                }
176
-
177
-                $beginPath .= $this->getPathConstraint($parameter, $types);
178
-            }
179
-        }
180
-
181
-        return $beginPath . $endPath;
182
-    }
183
-
184
-    /**
185
-     * @param ReflectionParameter $parameter
186
-     * @param string[] $types
187
-     * @return string
188
-     */
189
-
190
-    protected function getPathConstraint(ReflectionParameter $parameter, $types)
191
-    {
192
-        $name = $parameter->name;
193
-        $path = "/{" . $name;
194
-        return isset($types[$name]) ? "$path:{$types[$name]}}" : "$path}";
195
-    }
196
-
197
-    /**
198
-     * @param ReflectionMethod $method
199
-     * @return string[]
200
-     */
201
-
202
-    protected function getParamsConstraint(ReflectionMethod $method)
203
-    {
204
-        $params = [];
205
-        preg_match_all("~\@param\s(" . implode("|", array_keys($this->getWildcards())) . "|\(.+\))\s\\$([a-zA-Z0-1_]+)~i",
206
-            $method->getDocComment(), $types, PREG_SET_ORDER);
207
-
208
-        foreach ((array) $types as $type) {
209
-            // if a pattern is defined on Match take it otherwise take the param type by PHPDoc.
210
-            $params[$type[2]] = isset($type[4]) ? $type[4] : $type[1];
211
-        }
212
-
213
-        return $params;
214
-    }
215
-
216
-    /**
217
-     * @param ReflectionClass|ReflectionMethod $reflector
218
-     * @return string|null
219
-     */
220
-
221
-    protected function getAnnotatedStrategy($reflector)
222
-    {
223
-        preg_match("~\@strategy\s([a-zA-Z\\\_]+)~i", (string) $reflector->getDocComment(), $strategy);
224
-        return isset($strategy[1]) ? $strategy[1] : null;
225
-    }
226
-
227
-    /**
228
-     * Define how controller actions names will be joined to form the route pattern.
229
-     * Defaults to "/" so actions like "getMyAction" will be "/my/action". If changed to
230
-     * "-" the new pattern will be "/my-action".
231
-     *
232
-     * @param string $join
233
-     */
234
-
235
-    public function setControllerActionJoin($join)
236
-    {
237
-        $this->controllerActionJoin = $join;
238
-    }
239
-
240
-    /**
241
-     * @return string
242
-     */
243
-
244
-    public function getControllerActionJoin()
245
-    {
246
-        return $this->controllerActionJoin;
247
-    }
127
+			if (strpos(Collector::HTTP_METHODS, $http) !== false) {
128
+				$action   = $prefix . strtolower(implode($this->controllerActionJoin, $name));
129
+				$dynamic  = $this->getMethodConstraints($method);
130
+				$strategy = $this->getAnnotatedStrategy($method);
131
+
132
+				/** @var \Codeburner\Router\Route $route */
133
+				$route = $this->set($http, "$action$dynamic", [$controller->name, $method->name]);
134
+
135
+				if ($strategy !== null) {
136
+					   $route->setStrategy($strategy);
137
+				} else $route->setStrategy($controllerDefaultStrategy);
138
+
139
+				$group->set($route);
140
+			}
141
+		}
142
+
143
+		return $group;
144
+	}
145
+
146
+	/**
147
+	 * @param ReflectionClass $controller
148
+	 *
149
+	 * @return string
150
+	 */
151
+
152
+	protected function getControllerPrefix(ReflectionClass $controller)
153
+	{
154
+		preg_match("~\@prefix\s([a-zA-Z\\\_]+)~i", (string) $controller->getDocComment(), $prefix);
155
+		return isset($prefix[1]) ? $prefix[1] : str_replace("controller", "", strtolower($controller->getShortName()));
156
+	}
157
+
158
+	/**
159
+	 * @param \ReflectionMethod
160
+	 * @return string
161
+	 */
162
+
163
+	protected function getMethodConstraints(ReflectionMethod $method)
164
+	{
165
+		$beginPath = "";
166
+		$endPath = "";
167
+
168
+		if ($parameters = $method->getParameters()) {
169
+			$types = $this->getParamsConstraint($method);
170
+
171
+			foreach ($parameters as $parameter) {
172
+				if ($parameter->isOptional()) {
173
+					$beginPath .= "[";
174
+					$endPath .= "]";
175
+				}
176
+
177
+				$beginPath .= $this->getPathConstraint($parameter, $types);
178
+			}
179
+		}
180
+
181
+		return $beginPath . $endPath;
182
+	}
183
+
184
+	/**
185
+	 * @param ReflectionParameter $parameter
186
+	 * @param string[] $types
187
+	 * @return string
188
+	 */
189
+
190
+	protected function getPathConstraint(ReflectionParameter $parameter, $types)
191
+	{
192
+		$name = $parameter->name;
193
+		$path = "/{" . $name;
194
+		return isset($types[$name]) ? "$path:{$types[$name]}}" : "$path}";
195
+	}
196
+
197
+	/**
198
+	 * @param ReflectionMethod $method
199
+	 * @return string[]
200
+	 */
201
+
202
+	protected function getParamsConstraint(ReflectionMethod $method)
203
+	{
204
+		$params = [];
205
+		preg_match_all("~\@param\s(" . implode("|", array_keys($this->getWildcards())) . "|\(.+\))\s\\$([a-zA-Z0-1_]+)~i",
206
+			$method->getDocComment(), $types, PREG_SET_ORDER);
207
+
208
+		foreach ((array) $types as $type) {
209
+			// if a pattern is defined on Match take it otherwise take the param type by PHPDoc.
210
+			$params[$type[2]] = isset($type[4]) ? $type[4] : $type[1];
211
+		}
212
+
213
+		return $params;
214
+	}
215
+
216
+	/**
217
+	 * @param ReflectionClass|ReflectionMethod $reflector
218
+	 * @return string|null
219
+	 */
220
+
221
+	protected function getAnnotatedStrategy($reflector)
222
+	{
223
+		preg_match("~\@strategy\s([a-zA-Z\\\_]+)~i", (string) $reflector->getDocComment(), $strategy);
224
+		return isset($strategy[1]) ? $strategy[1] : null;
225
+	}
226
+
227
+	/**
228
+	 * Define how controller actions names will be joined to form the route pattern.
229
+	 * Defaults to "/" so actions like "getMyAction" will be "/my/action". If changed to
230
+	 * "-" the new pattern will be "/my-action".
231
+	 *
232
+	 * @param string $join
233
+	 */
234
+
235
+	public function setControllerActionJoin($join)
236
+	{
237
+		$this->controllerActionJoin = $join;
238
+	}
239
+
240
+	/**
241
+	 * @return string
242
+	 */
243
+
244
+	public function getControllerActionJoin()
245
+	{
246
+		return $this->controllerActionJoin;
247
+	}
248 248
 
249 249
 }
Please login to merge, or discard this patch.
Braces   +9 added lines, -5 removed lines patch added patch discarded remove patch
@@ -69,8 +69,9 @@  discard block
 block discarded – undo
69 69
     public function controllers(array $controllers)
70 70
     {
71 71
         $group = new Group;
72
-        foreach ($controllers as $controller)
73
-            $group->set($this->controller($controller));
72
+        foreach ($controllers as $controller) {
73
+                    $group->set($this->controller($controller));
74
+        }
74 75
         return $group;
75 76
     }
76 77
 
@@ -100,8 +101,9 @@  discard block
 block discarded – undo
100 101
     public function controllersWithoutPrefix(array $controllers)
101 102
     {
102 103
         $group = new Group;
103
-        foreach ($controllers as $controller)
104
-            $group->set($this->controllerWithoutPrefix($controller));
104
+        foreach ($controllers as $controller) {
105
+                    $group->set($this->controllerWithoutPrefix($controller));
106
+        }
105 107
         return $group;
106 108
     }
107 109
 
@@ -134,7 +136,9 @@  discard block
 block discarded – undo
134 136
 
135 137
                 if ($strategy !== null) {
136 138
                        $route->setStrategy($strategy);
137
-                } else $route->setStrategy($controllerDefaultStrategy);
139
+                } else {
140
+                	$route->setStrategy($controllerDefaultStrategy);
141
+                }
138 142
 
139 143
                 $group->set($route);
140 144
             }
Please login to merge, or discard this patch.
src/Exceptions/BadRouteException.php 1 patch
Indentation   +4 added lines, -4 removed lines patch added patch discarded remove patch
@@ -20,9 +20,9 @@
 block discarded – undo
20 20
 class BadRouteException extends \Exception
21 21
 {
22 22
 
23
-    const OPTIONAL_SEGMENTS_ON_MIDDLE    = "Optional segments can only occur at the end of a route.";
24
-    const UNCLOSED_OPTIONAL_SEGMENTS     = "Number of opening [ and closing ] does not match.";
25
-    const EMPTY_OPTIONAL_PARTS           = "Empty optional part.";
26
-    const WRONG_CONTROLLER_CREATION_FUNC = "The controller creation function passed is not callable.";
23
+	const OPTIONAL_SEGMENTS_ON_MIDDLE    = "Optional segments can only occur at the end of a route.";
24
+	const UNCLOSED_OPTIONAL_SEGMENTS     = "Number of opening [ and closing ] does not match.";
25
+	const EMPTY_OPTIONAL_PARTS           = "Empty optional part.";
26
+	const WRONG_CONTROLLER_CREATION_FUNC = "The controller creation function passed is not callable.";
27 27
 
28 28
 }
Please login to merge, or discard this patch.
src/Route.php 3 patches
Braces   +6 added lines, -2 removed lines patch added patch discarded remove patch
@@ -212,7 +212,9 @@  discard block
 block discarded – undo
212 212
             if (is_string($callable[0])) {
213 213
                    $callable[0] = $this->parseCallableController($callable[0]);
214 214
                    $callable[1] = $this->parseCallablePlaceholders($callable[1]);
215
-            } else $callable[1] = $this->parseCallablePlaceholders($callable[1]);
215
+            } else {
216
+            	$callable[1] = $this->parseCallablePlaceholders($callable[1]);
217
+            }
216 218
         }
217 219
 
218 220
         return $callable;
@@ -231,7 +233,9 @@  discard block
 block discarded – undo
231 233
 
232 234
         if ($this->controllerCreationFunction === null) {
233 235
                return new $controller;
234
-        } else return call_user_func($this->controllerCreationFunction, $controller);
236
+        } else {
237
+        	return call_user_func($this->controllerCreationFunction, $controller);
238
+        }
235 239
     }
236 240
 
237 241
     /**
Please login to merge, or discard this patch.
Indentation   +600 added lines, -600 removed lines patch added patch discarded remove patch
@@ -23,613 +23,613 @@
 block discarded – undo
23 23
 class Route
24 24
 {
25 25
 
26
-    /**
27
-     * @var Collector
28
-     */
26
+	/**
27
+	 * @var Collector
28
+	 */
29 29
 
30
-    protected $collector;
30
+	protected $collector;
31 31
     
32
-    /**
33
-     * @var string
34
-     */
32
+	/**
33
+	 * @var string
34
+	 */
35 35
 
36
-    protected $method;
36
+	protected $method;
37 37
     
38
-    /**
39
-     * @var string
40
-     */
38
+	/**
39
+	 * @var string
40
+	 */
41 41
 
42
-    protected $pattern;
42
+	protected $pattern;
43 43
     
44
-    /**
45
-     * @var string|array|\Closure
46
-     */
44
+	/**
45
+	 * @var string|array|\Closure
46
+	 */
47 47
 
48
-    protected $action;
48
+	protected $action;
49 49
     
50
-    /**
51
-     * @var string
52
-     */
53
-
54
-    protected $namespace = "";
55
-
56
-    /**
57
-     * @var string[]
58
-     */
59
-
60
-    protected $params = [];
61
-
62
-    /**
63
-     * Defaults are parameters set by the user, and don't
64
-     * appear on the pattern.
65
-     *
66
-     * @var array
67
-     */
68
-
69
-    protected $defaults = [];
70
-
71
-    /**
72
-     * Metadata can be set to be used on filters, dispatch strategies
73
-     * or anywhere the route object is used.
74
-     *
75
-     * @var array
76
-     */
77
-
78
-    protected $metadata = [];
79
-
80
-    /**
81
-     * @var string|StrategyInterface
82
-     */
83
-
84
-    protected $strategy;
85
-
86
-    /**
87
-     * Blocked routes are dynamic routes selected to pass by the matcher.
88
-     *
89
-     * @var boolean
90
-     */
91
-
92
-    protected $blocked = false;
93
-
94
-    /**
95
-     * The matcher that dispatched this route.
96
-     *
97
-     * @var Matcher $matcher
98
-     */
99
-
100
-    protected $matcher;
101
-
102
-    /**
103
-     * The function used to create controllers from name.
104
-     *
105
-     * @var string|array|\Closure
106
-     */
107
-
108
-    protected $controllerCreationFunction;
109
-
110
-    /**
111
-     * @param Collector $collector
112
-     * @param string $method
113
-     * @param string $pattern
114
-     * @param string|array|\Closure $action
115
-     */
116
-
117
-    public function __construct(Collector $collector, $method, $pattern, $action)
118
-    {
119
-        $this->collector = $collector;
120
-        $this->method    = $method;
121
-        $this->pattern   = $pattern;
122
-        $this->action    = $action;
123
-    }
124
-
125
-    /**
126
-     * Clone this route and set it into the collector.
127
-     *
128
-     * @return Route
129
-     */
130
-
131
-    public function reset()
132
-    {
133
-        return $this->collector->set($this->method, $this->pattern, $this->action)->nth(0)
134
-                               ->setStrategy($this->strategy)->setParams($this->params)
135
-                               ->setDefaults($this->defaults)->setMetadataArray($this->metadata);
136
-    }
137
-
138
-    /**
139
-     * Remove this route from the collector.
140
-     *
141
-     * @return self
142
-     */
143
-
144
-    public function forget()
145
-    {
146
-        $this->collector->forget($this->method, $this->pattern);
147
-        return $this;
148
-    }
149
-
150
-    /**
151
-     * Execute the route action, if no strategy was provided the action
152
-     * will be executed by the call_user_func PHP function.
153
-     *
154
-     * @throws BadRouteException
155
-     * @return mixed
156
-     */
157
-
158
-    public function call()
159
-    {
160
-        $this->action = $this->parseCallable($this->action);
161
-
162
-        if ($this->strategy === null) {
163
-            return call_user_func_array($this->action, array_merge($this->defaults, $this->params));
164
-        }
165
-
166
-        if (!is_object($this->strategy)) {
167
-            $this->strategy = new $this->strategy;
168
-        }
169
-
170
-        return $this->callWithStrategy();
171
-    }
172
-
173
-    /**
174
-     * Seek for dynamic content on callables. eg. routes action controller#action
175
-     * syntax allow to use the variables to build the string like: {controller}@{action}
176
-     *
177
-     * @param string|array|\Closure $callable
178
-     * @return string|array|\Closure
179
-     */
180
-
181
-    private function parseCallable($callable)
182
-    {
183
-        if (is_string($callable) && strpos($callable, "@")) {
184
-            $callable = explode("@", $callable);
185
-        }
186
-
187
-        if (is_array($callable)) {
188
-            if (is_string($callable[0])) {
189
-                   $callable[0] = $this->parseCallableController($callable[0]);
190
-                   $callable[1] = $this->parseCallablePlaceholders($callable[1]);
191
-            } else $callable[1] = $this->parseCallablePlaceholders($callable[1]);
192
-        }
193
-
194
-        return $callable;
195
-    }
196
-
197
-    /**
198
-     * Get the controller object.
199
-     *
200
-     * @param string $controller
201
-     * @return Object
202
-     */
203
-
204
-    private function parseCallableController($controller)
205
-    {
206
-        $controller  = rtrim($this->namespace, "\\") . "\\" . $this->parseCallablePlaceholders($controller);
207
-
208
-        if ($this->controllerCreationFunction === null) {
209
-               return new $controller;
210
-        } else return call_user_func($this->controllerCreationFunction, $controller);
211
-    }
212
-
213
-    /**
214
-     * Parse and replace dynamic content on route action.
215
-     *
216
-     * @param  string $fragment Part of callable
217
-     * @return string
218
-     */
219
-
220
-    private function parseCallablePlaceholders($fragment)
221
-    {
222
-        if (strpos($fragment, "{") !== false) {
223
-            foreach ($this->params as $placeholder => $value) {
224
-                if (strpos($fragment, "{" . $placeholder . "}") !== false) {
225
-                    $fragment = str_replace("{" . $placeholder . "}", ucwords(str_replace("-", " ", $value)), $fragment);
226
-                }
227
-            }
228
-        }
229
-
230
-        return $fragment;
231
-    }
232
-
233
-    /**
234
-     * Execute the route action with the given strategy.
235
-     *
236
-     * @throws BadRouteException
237
-     * @return mixed
238
-     */
239
-
240
-    private function callWithStrategy()
241
-    {
242
-        if ($this->strategy instanceof StrategyInterface) {
243
-            if ($this->strategy instanceof MatcherAwareInterface) {
244
-                $this->strategy->setMatcher($this->matcher);
245
-            }
246
-
247
-            return $this->strategy->call($this);
248
-        }
249
-
250
-        $strategy = get_class($this->strategy);
251
-        throw new BadRouteException("`$strategy` is not a valid route dispatch strategy, ".
252
-            "it must implement the `Codeburner\\Router\\Strategies\\StrategyInterface` interface.");
253
-    }
254
-
255
-    /**
256
-     * @return Collector
257
-     */
258
-
259
-    public function getCollector()
260
-    {
261
-        return $this->collector;
262
-    }
263
-
264
-    /**
265
-     * @return string
266
-     */
267
-
268
-    public function getMethod()
269
-    {
270
-        return $this->method;
271
-    }
272
-
273
-    /**
274
-     * @return string
275
-     */
276
-
277
-    public function getPattern()
278
-    {
279
-        return $this->pattern;
280
-    }
281
-
282
-    /**
283
-     * @return string[]
284
-     */
285
-
286
-    public function getSegments()
287
-    {
288
-        return explode("/", $this->pattern);
289
-    }
290
-
291
-    /**
292
-     * @return string|array|\Closure
293
-     */
294
-
295
-    public function getAction()
296
-    {
297
-        return $this->action;
298
-    }
299
-
300
-    /**
301
-     * @return string
302
-     */
303
-
304
-    public function getNamespace()
305
-    {
306
-        return $this->namespace;
307
-    }
308
-
309
-    /**
310
-     * @return string[]
311
-     */
312
-
313
-    public function getParams()
314
-    {
315
-        return $this->params;
316
-    }
317
-
318
-    /**
319
-     * @param string $key
320
-     * @return string
321
-     */
322
-
323
-    public function getParam($key)
324
-    {
325
-        return $this->params[$key];
326
-    }
327
-
328
-    /**
329
-     * @return array
330
-     */
331
-
332
-    public function getDefaults()
333
-    {
334
-        return $this->defaults;
335
-    }
336
-
337
-    /**
338
-     * @param string $key
339
-     * @return mixed
340
-     */
341
-
342
-    public function getDefault($key)
343
-    {
344
-        return $this->defaults[$key];
345
-    }
346
-
347
-    /**
348
-     * @return array
349
-     */
350
-
351
-    public function getMetadataArray()
352
-    {
353
-        return $this->metadata;
354
-    }
355
-
356
-    /**
357
-     * @param string $key
358
-     * @return mixed
359
-     */
360
-
361
-    public function getMetadata($key)
362
-    {
363
-        return $this->metadata[$key];
364
-    }
365
-
366
-    /**
367
-     * @return string|null
368
-     */
369
-
370
-    public function getStrategy()
371
-    {
372
-        if ($this->strategy instanceof StrategyInterface) {
373
-            return get_class($this->strategy);
374
-        }
375
-
376
-        return $this->strategy;
377
-    }
378
-
379
-    /**
380
-     * @inheritdoc
381
-     */
382
-
383
-    public function getMatcher()
384
-    {
385
-        return $this->matcher;
386
-    }
387
-
388
-    /**
389
-     * Verify if a Route have already been blocked.
390
-     *
391
-     * @return boolean
392
-     */
393
-
394
-    public function getBlock()
395
-    {
396
-        return $this->blocked;
397
-    }
398
-
399
-    /**
400
-     * Blocking a route indicate that that route have been selected and
401
-     * parsed, now it will be given to the matcher.
402
-     *
403
-     * @param bool $blocked
404
-     * @return self
405
-     */
406
-
407
-    public function setBlock($blocked)
408
-    {
409
-        $this->blocked = $blocked;
410
-        return $this;
411
-    }
412
-
413
-    /**
414
-     * @param string $method
415
-     * @return Route
416
-     */
417
-
418
-    public function setMethod($method)
419
-    {
420
-        $this->forget();
421
-        $this->method = $method;
422
-        return $this->reset();
423
-    }
424
-
425
-    /**
426
-     * @param string $pattern
427
-     * @return Route
428
-     */
429
-
430
-    public function setPattern($pattern)
431
-    {
432
-        $this->forget();
433
-        $this->pattern = $pattern;
434
-        return $this->reset();
435
-    }
436
-
437
-    /**
438
-     * @param string $pattern
439
-     * @return self
440
-     */
441
-
442
-    public function setPatternWithoutReset($pattern)
443
-    {
444
-        $this->pattern = $pattern;
445
-        return $this;
446
-    }
447
-
448
-    /**
449
-     * @param string $action
450
-     * @return self
451
-     */
452
-
453
-    public function setAction($action)
454
-    {
455
-        $this->action = $action;
456
-        return $this;
457
-    }
458
-
459
-    /**
460
-     * @param string $namespace
461
-     * @return self
462
-     */
463
-
464
-    public function setNamespace($namespace)
465
-    {
466
-        $this->namespace = $namespace;
467
-        return $this;
468
-    }
469
-
470
-    /**
471
-     * @param string[] $params
472
-     * @return self
473
-     */
474
-
475
-    public function setParams(array $params)
476
-    {
477
-        $this->params = $params;
478
-        return $this;
479
-    }
480
-
481
-    /**
482
-     * @param string $key
483
-     * @param string $value
484
-     *
485
-     * @return self
486
-     */
487
-
488
-    public function setParam($key, $value)
489
-    {
490
-        $this->params[$key] = $value;
491
-        return $this;
492
-    }
493
-
494
-    /**
495
-     * @param mixed[] $defaults
496
-     * @return self
497
-     */
498
-
499
-    public function setDefaults(array $defaults)
500
-    {
501
-        $this->defaults = $defaults;
502
-        return $this;
503
-    }
504
-
505
-    /**
506
-     * @param string $key
507
-     * @param mixed $value
508
-     *
509
-     * @return self
510
-     */
511
-
512
-    public function setDefault($key, $value)
513
-    {
514
-        $this->defaults[$key] = $value;
515
-        return $this;
516
-    }
517
-
518
-    /**
519
-     * @param mixed[] $metadata
520
-     * @return self
521
-     */
522
-
523
-    public function setMetadataArray(array $metadata)
524
-    {
525
-        $this->metadata = $metadata;
526
-        return $this;
527
-    }
528
-
529
-    /**
530
-     * @param string $key
531
-     * @param mixed $value
532
-     *
533
-     * @return $this
534
-     */
535
-
536
-    public function setMetadata($key, $value)
537
-    {
538
-        $this->metadata[$key] = $value;
539
-        return $this;
540
-    }
541
-
542
-    /**
543
-     * @param null|string|StrategyInterface $strategy
544
-     * @return self
545
-     */
546
-
547
-    public function setStrategy($strategy)
548
-    {
549
-        $this->strategy = $strategy;
550
-        return $this;
551
-    }
552
-
553
-    /**
554
-     * @inheritdoc
555
-     */
556
-
557
-    public function setMatcher(Matcher $matcher)
558
-    {
559
-        $this->matcher = $matcher;
560
-        return $this;
561
-    }
562
-
563
-    /**
564
-     * Set a constraint to a token in the route pattern.
565
-     *
566
-     * @param string $token
567
-     * @param string $regex
568
-     *
569
-     * @return self
570
-     */
571
-
572
-    public function setConstraint($token, $regex)
573
-    {
574
-        $initPos = strpos($this->pattern, "{" . $token);
575
-
576
-        if ($initPos !== false) {
577
-            $endPos = strpos($this->pattern, "}", $initPos);
578
-            $newPattern = substr_replace($this->pattern, "{" . "$token:$regex" . "}", $initPos, $endPos - $initPos + 1);
579
-            $wildcards = $this->collector->getWildcardTokens();
580
-            $newPattern = str_replace(array_keys($wildcards), $wildcards, $newPattern);
581
-            $this->setPatternWithoutReset($newPattern);
582
-        }
583
-
584
-        return $this;
585
-    }
586
-
587
-    /**
588
-     * Set a function to create controllers.
589
-     *
590
-     * @param string|array|\Closure $callable
591
-     * @throws BadRouteException
592
-     * @return self
593
-     */
594
-
595
-    public function setControllerCreationFunction($callable)
596
-    {
597
-        if (!is_callable($callable)) {
598
-            throw new BadRouteException(BadRouteException::WRONG_CONTROLLER_CREATION_FUNC);
599
-        }
600
-
601
-        $this->controllerCreationFunction = $this->parseCallable($callable);
602
-        return $this;
603
-    }
604
-
605
-    /**
606
-     * @param string $key
607
-     * @return bool
608
-     */
609
-
610
-    public function hasParam($key)
611
-    {
612
-        return isset($this->params[$key]);
613
-    }
614
-
615
-    /**
616
-     * @param string $key
617
-     * @return bool
618
-     */
619
-
620
-    public function hasDefault($key)
621
-    {
622
-        return isset($this->defaults[$key]);
623
-    }
624
-
625
-    /**
626
-     * @param string $key
627
-     * @return bool
628
-     */
629
-
630
-    public function hasMetadata($key)
631
-    {
632
-        return isset($this->metadata[$key]);
633
-    }
50
+	/**
51
+	 * @var string
52
+	 */
53
+
54
+	protected $namespace = "";
55
+
56
+	/**
57
+	 * @var string[]
58
+	 */
59
+
60
+	protected $params = [];
61
+
62
+	/**
63
+	 * Defaults are parameters set by the user, and don't
64
+	 * appear on the pattern.
65
+	 *
66
+	 * @var array
67
+	 */
68
+
69
+	protected $defaults = [];
70
+
71
+	/**
72
+	 * Metadata can be set to be used on filters, dispatch strategies
73
+	 * or anywhere the route object is used.
74
+	 *
75
+	 * @var array
76
+	 */
77
+
78
+	protected $metadata = [];
79
+
80
+	/**
81
+	 * @var string|StrategyInterface
82
+	 */
83
+
84
+	protected $strategy;
85
+
86
+	/**
87
+	 * Blocked routes are dynamic routes selected to pass by the matcher.
88
+	 *
89
+	 * @var boolean
90
+	 */
91
+
92
+	protected $blocked = false;
93
+
94
+	/**
95
+	 * The matcher that dispatched this route.
96
+	 *
97
+	 * @var Matcher $matcher
98
+	 */
99
+
100
+	protected $matcher;
101
+
102
+	/**
103
+	 * The function used to create controllers from name.
104
+	 *
105
+	 * @var string|array|\Closure
106
+	 */
107
+
108
+	protected $controllerCreationFunction;
109
+
110
+	/**
111
+	 * @param Collector $collector
112
+	 * @param string $method
113
+	 * @param string $pattern
114
+	 * @param string|array|\Closure $action
115
+	 */
116
+
117
+	public function __construct(Collector $collector, $method, $pattern, $action)
118
+	{
119
+		$this->collector = $collector;
120
+		$this->method    = $method;
121
+		$this->pattern   = $pattern;
122
+		$this->action    = $action;
123
+	}
124
+
125
+	/**
126
+	 * Clone this route and set it into the collector.
127
+	 *
128
+	 * @return Route
129
+	 */
130
+
131
+	public function reset()
132
+	{
133
+		return $this->collector->set($this->method, $this->pattern, $this->action)->nth(0)
134
+							   ->setStrategy($this->strategy)->setParams($this->params)
135
+							   ->setDefaults($this->defaults)->setMetadataArray($this->metadata);
136
+	}
137
+
138
+	/**
139
+	 * Remove this route from the collector.
140
+	 *
141
+	 * @return self
142
+	 */
143
+
144
+	public function forget()
145
+	{
146
+		$this->collector->forget($this->method, $this->pattern);
147
+		return $this;
148
+	}
149
+
150
+	/**
151
+	 * Execute the route action, if no strategy was provided the action
152
+	 * will be executed by the call_user_func PHP function.
153
+	 *
154
+	 * @throws BadRouteException
155
+	 * @return mixed
156
+	 */
157
+
158
+	public function call()
159
+	{
160
+		$this->action = $this->parseCallable($this->action);
161
+
162
+		if ($this->strategy === null) {
163
+			return call_user_func_array($this->action, array_merge($this->defaults, $this->params));
164
+		}
165
+
166
+		if (!is_object($this->strategy)) {
167
+			$this->strategy = new $this->strategy;
168
+		}
169
+
170
+		return $this->callWithStrategy();
171
+	}
172
+
173
+	/**
174
+	 * Seek for dynamic content on callables. eg. routes action controller#action
175
+	 * syntax allow to use the variables to build the string like: {controller}@{action}
176
+	 *
177
+	 * @param string|array|\Closure $callable
178
+	 * @return string|array|\Closure
179
+	 */
180
+
181
+	private function parseCallable($callable)
182
+	{
183
+		if (is_string($callable) && strpos($callable, "@")) {
184
+			$callable = explode("@", $callable);
185
+		}
186
+
187
+		if (is_array($callable)) {
188
+			if (is_string($callable[0])) {
189
+				   $callable[0] = $this->parseCallableController($callable[0]);
190
+				   $callable[1] = $this->parseCallablePlaceholders($callable[1]);
191
+			} else $callable[1] = $this->parseCallablePlaceholders($callable[1]);
192
+		}
193
+
194
+		return $callable;
195
+	}
196
+
197
+	/**
198
+	 * Get the controller object.
199
+	 *
200
+	 * @param string $controller
201
+	 * @return Object
202
+	 */
203
+
204
+	private function parseCallableController($controller)
205
+	{
206
+		$controller  = rtrim($this->namespace, "\\") . "\\" . $this->parseCallablePlaceholders($controller);
207
+
208
+		if ($this->controllerCreationFunction === null) {
209
+			   return new $controller;
210
+		} else return call_user_func($this->controllerCreationFunction, $controller);
211
+	}
212
+
213
+	/**
214
+	 * Parse and replace dynamic content on route action.
215
+	 *
216
+	 * @param  string $fragment Part of callable
217
+	 * @return string
218
+	 */
219
+
220
+	private function parseCallablePlaceholders($fragment)
221
+	{
222
+		if (strpos($fragment, "{") !== false) {
223
+			foreach ($this->params as $placeholder => $value) {
224
+				if (strpos($fragment, "{" . $placeholder . "}") !== false) {
225
+					$fragment = str_replace("{" . $placeholder . "}", ucwords(str_replace("-", " ", $value)), $fragment);
226
+				}
227
+			}
228
+		}
229
+
230
+		return $fragment;
231
+	}
232
+
233
+	/**
234
+	 * Execute the route action with the given strategy.
235
+	 *
236
+	 * @throws BadRouteException
237
+	 * @return mixed
238
+	 */
239
+
240
+	private function callWithStrategy()
241
+	{
242
+		if ($this->strategy instanceof StrategyInterface) {
243
+			if ($this->strategy instanceof MatcherAwareInterface) {
244
+				$this->strategy->setMatcher($this->matcher);
245
+			}
246
+
247
+			return $this->strategy->call($this);
248
+		}
249
+
250
+		$strategy = get_class($this->strategy);
251
+		throw new BadRouteException("`$strategy` is not a valid route dispatch strategy, ".
252
+			"it must implement the `Codeburner\\Router\\Strategies\\StrategyInterface` interface.");
253
+	}
254
+
255
+	/**
256
+	 * @return Collector
257
+	 */
258
+
259
+	public function getCollector()
260
+	{
261
+		return $this->collector;
262
+	}
263
+
264
+	/**
265
+	 * @return string
266
+	 */
267
+
268
+	public function getMethod()
269
+	{
270
+		return $this->method;
271
+	}
272
+
273
+	/**
274
+	 * @return string
275
+	 */
276
+
277
+	public function getPattern()
278
+	{
279
+		return $this->pattern;
280
+	}
281
+
282
+	/**
283
+	 * @return string[]
284
+	 */
285
+
286
+	public function getSegments()
287
+	{
288
+		return explode("/", $this->pattern);
289
+	}
290
+
291
+	/**
292
+	 * @return string|array|\Closure
293
+	 */
294
+
295
+	public function getAction()
296
+	{
297
+		return $this->action;
298
+	}
299
+
300
+	/**
301
+	 * @return string
302
+	 */
303
+
304
+	public function getNamespace()
305
+	{
306
+		return $this->namespace;
307
+	}
308
+
309
+	/**
310
+	 * @return string[]
311
+	 */
312
+
313
+	public function getParams()
314
+	{
315
+		return $this->params;
316
+	}
317
+
318
+	/**
319
+	 * @param string $key
320
+	 * @return string
321
+	 */
322
+
323
+	public function getParam($key)
324
+	{
325
+		return $this->params[$key];
326
+	}
327
+
328
+	/**
329
+	 * @return array
330
+	 */
331
+
332
+	public function getDefaults()
333
+	{
334
+		return $this->defaults;
335
+	}
336
+
337
+	/**
338
+	 * @param string $key
339
+	 * @return mixed
340
+	 */
341
+
342
+	public function getDefault($key)
343
+	{
344
+		return $this->defaults[$key];
345
+	}
346
+
347
+	/**
348
+	 * @return array
349
+	 */
350
+
351
+	public function getMetadataArray()
352
+	{
353
+		return $this->metadata;
354
+	}
355
+
356
+	/**
357
+	 * @param string $key
358
+	 * @return mixed
359
+	 */
360
+
361
+	public function getMetadata($key)
362
+	{
363
+		return $this->metadata[$key];
364
+	}
365
+
366
+	/**
367
+	 * @return string|null
368
+	 */
369
+
370
+	public function getStrategy()
371
+	{
372
+		if ($this->strategy instanceof StrategyInterface) {
373
+			return get_class($this->strategy);
374
+		}
375
+
376
+		return $this->strategy;
377
+	}
378
+
379
+	/**
380
+	 * @inheritdoc
381
+	 */
382
+
383
+	public function getMatcher()
384
+	{
385
+		return $this->matcher;
386
+	}
387
+
388
+	/**
389
+	 * Verify if a Route have already been blocked.
390
+	 *
391
+	 * @return boolean
392
+	 */
393
+
394
+	public function getBlock()
395
+	{
396
+		return $this->blocked;
397
+	}
398
+
399
+	/**
400
+	 * Blocking a route indicate that that route have been selected and
401
+	 * parsed, now it will be given to the matcher.
402
+	 *
403
+	 * @param bool $blocked
404
+	 * @return self
405
+	 */
406
+
407
+	public function setBlock($blocked)
408
+	{
409
+		$this->blocked = $blocked;
410
+		return $this;
411
+	}
412
+
413
+	/**
414
+	 * @param string $method
415
+	 * @return Route
416
+	 */
417
+
418
+	public function setMethod($method)
419
+	{
420
+		$this->forget();
421
+		$this->method = $method;
422
+		return $this->reset();
423
+	}
424
+
425
+	/**
426
+	 * @param string $pattern
427
+	 * @return Route
428
+	 */
429
+
430
+	public function setPattern($pattern)
431
+	{
432
+		$this->forget();
433
+		$this->pattern = $pattern;
434
+		return $this->reset();
435
+	}
436
+
437
+	/**
438
+	 * @param string $pattern
439
+	 * @return self
440
+	 */
441
+
442
+	public function setPatternWithoutReset($pattern)
443
+	{
444
+		$this->pattern = $pattern;
445
+		return $this;
446
+	}
447
+
448
+	/**
449
+	 * @param string $action
450
+	 * @return self
451
+	 */
452
+
453
+	public function setAction($action)
454
+	{
455
+		$this->action = $action;
456
+		return $this;
457
+	}
458
+
459
+	/**
460
+	 * @param string $namespace
461
+	 * @return self
462
+	 */
463
+
464
+	public function setNamespace($namespace)
465
+	{
466
+		$this->namespace = $namespace;
467
+		return $this;
468
+	}
469
+
470
+	/**
471
+	 * @param string[] $params
472
+	 * @return self
473
+	 */
474
+
475
+	public function setParams(array $params)
476
+	{
477
+		$this->params = $params;
478
+		return $this;
479
+	}
480
+
481
+	/**
482
+	 * @param string $key
483
+	 * @param string $value
484
+	 *
485
+	 * @return self
486
+	 */
487
+
488
+	public function setParam($key, $value)
489
+	{
490
+		$this->params[$key] = $value;
491
+		return $this;
492
+	}
493
+
494
+	/**
495
+	 * @param mixed[] $defaults
496
+	 * @return self
497
+	 */
498
+
499
+	public function setDefaults(array $defaults)
500
+	{
501
+		$this->defaults = $defaults;
502
+		return $this;
503
+	}
504
+
505
+	/**
506
+	 * @param string $key
507
+	 * @param mixed $value
508
+	 *
509
+	 * @return self
510
+	 */
511
+
512
+	public function setDefault($key, $value)
513
+	{
514
+		$this->defaults[$key] = $value;
515
+		return $this;
516
+	}
517
+
518
+	/**
519
+	 * @param mixed[] $metadata
520
+	 * @return self
521
+	 */
522
+
523
+	public function setMetadataArray(array $metadata)
524
+	{
525
+		$this->metadata = $metadata;
526
+		return $this;
527
+	}
528
+
529
+	/**
530
+	 * @param string $key
531
+	 * @param mixed $value
532
+	 *
533
+	 * @return $this
534
+	 */
535
+
536
+	public function setMetadata($key, $value)
537
+	{
538
+		$this->metadata[$key] = $value;
539
+		return $this;
540
+	}
541
+
542
+	/**
543
+	 * @param null|string|StrategyInterface $strategy
544
+	 * @return self
545
+	 */
546
+
547
+	public function setStrategy($strategy)
548
+	{
549
+		$this->strategy = $strategy;
550
+		return $this;
551
+	}
552
+
553
+	/**
554
+	 * @inheritdoc
555
+	 */
556
+
557
+	public function setMatcher(Matcher $matcher)
558
+	{
559
+		$this->matcher = $matcher;
560
+		return $this;
561
+	}
562
+
563
+	/**
564
+	 * Set a constraint to a token in the route pattern.
565
+	 *
566
+	 * @param string $token
567
+	 * @param string $regex
568
+	 *
569
+	 * @return self
570
+	 */
571
+
572
+	public function setConstraint($token, $regex)
573
+	{
574
+		$initPos = strpos($this->pattern, "{" . $token);
575
+
576
+		if ($initPos !== false) {
577
+			$endPos = strpos($this->pattern, "}", $initPos);
578
+			$newPattern = substr_replace($this->pattern, "{" . "$token:$regex" . "}", $initPos, $endPos - $initPos + 1);
579
+			$wildcards = $this->collector->getWildcardTokens();
580
+			$newPattern = str_replace(array_keys($wildcards), $wildcards, $newPattern);
581
+			$this->setPatternWithoutReset($newPattern);
582
+		}
583
+
584
+		return $this;
585
+	}
586
+
587
+	/**
588
+	 * Set a function to create controllers.
589
+	 *
590
+	 * @param string|array|\Closure $callable
591
+	 * @throws BadRouteException
592
+	 * @return self
593
+	 */
594
+
595
+	public function setControllerCreationFunction($callable)
596
+	{
597
+		if (!is_callable($callable)) {
598
+			throw new BadRouteException(BadRouteException::WRONG_CONTROLLER_CREATION_FUNC);
599
+		}
600
+
601
+		$this->controllerCreationFunction = $this->parseCallable($callable);
602
+		return $this;
603
+	}
604
+
605
+	/**
606
+	 * @param string $key
607
+	 * @return bool
608
+	 */
609
+
610
+	public function hasParam($key)
611
+	{
612
+		return isset($this->params[$key]);
613
+	}
614
+
615
+	/**
616
+	 * @param string $key
617
+	 * @return bool
618
+	 */
619
+
620
+	public function hasDefault($key)
621
+	{
622
+		return isset($this->defaults[$key]);
623
+	}
624
+
625
+	/**
626
+	 * @param string $key
627
+	 * @return bool
628
+	 */
629
+
630
+	public function hasMetadata($key)
631
+	{
632
+		return isset($this->metadata[$key]);
633
+	}
634 634
 
635 635
 }
Please login to merge, or discard this patch.
Spacing   +2 added lines, -2 removed lines patch added patch discarded remove patch
@@ -203,7 +203,7 @@  discard block
 block discarded – undo
203 203
 
204 204
     private function parseCallableController($controller)
205 205
     {
206
-        $controller  = rtrim($this->namespace, "\\") . "\\" . $this->parseCallablePlaceholders($controller);
206
+        $controller = rtrim($this->namespace, "\\") . "\\" . $this->parseCallablePlaceholders($controller);
207 207
 
208 208
         if ($this->controllerCreationFunction === null) {
209 209
                return new $controller;
@@ -248,7 +248,7 @@  discard block
 block discarded – undo
248 248
         }
249 249
 
250 250
         $strategy = get_class($this->strategy);
251
-        throw new BadRouteException("`$strategy` is not a valid route dispatch strategy, ".
251
+        throw new BadRouteException("`$strategy` is not a valid route dispatch strategy, " .
252 252
             "it must implement the `Codeburner\\Router\\Strategies\\StrategyInterface` interface.");
253 253
     }
254 254
 
Please login to merge, or discard this patch.
src/Exceptions/MethodNotAllowedException.php 1 patch
Indentation   +55 added lines, -55 removed lines patch added patch discarded remove patch
@@ -20,71 +20,71 @@
 block discarded – undo
20 20
 class MethodNotAllowedException extends BadRouteException
21 21
 {
22 22
 
23
-    /**
24
-     * The HTTP method from request.
25
-     *
26
-     * @var string
27
-     */
23
+	/**
24
+	 * The HTTP method from request.
25
+	 *
26
+	 * @var string
27
+	 */
28 28
 
29
-    public $requestedMethod;
29
+	public $requestedMethod;
30 30
 
31
-    /**
32
-     * The requested URi.
33
-     *
34
-     * @var string
35
-     */
31
+	/**
32
+	 * The requested URi.
33
+	 *
34
+	 * @var string
35
+	 */
36 36
 
37
-    public $requestedUri;
37
+	public $requestedUri;
38 38
 
39
-    /**
40
-     * All the allowed HTTP methods and routes for the request.
41
-     *
42
-     * @var array
43
-     */
39
+	/**
40
+	 * All the allowed HTTP methods and routes for the request.
41
+	 *
42
+	 * @var array
43
+	 */
44 44
 
45
-    public $allowedMethods;
45
+	public $allowedMethods;
46 46
 	
47
-    /**
48
-     * Exception constructor.
49
-     *
50
-     * @param string  $requestedMethod The request HTTP method.
51
-     * @param string  $requestedUri    The request URi.
52
-     * @param array   $allowedMethods  All the allowed HTTP methods and routes for the request.
53
-     * @param string  $message         The exception error message.
54
-     * @param integer $code            The exception error code.
55
-     */
47
+	/**
48
+	 * Exception constructor.
49
+	 *
50
+	 * @param string  $requestedMethod The request HTTP method.
51
+	 * @param string  $requestedUri    The request URi.
52
+	 * @param array   $allowedMethods  All the allowed HTTP methods and routes for the request.
53
+	 * @param string  $message         The exception error message.
54
+	 * @param integer $code            The exception error code.
55
+	 */
56 56
 
57
-    public function __construct($requestedMethod, $requestedUri, array $allowedMethods, $message = null, $code = 405)
58
-    {
59
-        $this->requestedMethod = $requestedMethod;
60
-        $this->requestedUri    = $requestedUri;
61
-        $this->allowedMethods  = $allowedMethods;
62
-        parent::__construct($message, $code);
63
-    }
57
+	public function __construct($requestedMethod, $requestedUri, array $allowedMethods, $message = null, $code = 405)
58
+	{
59
+		$this->requestedMethod = $requestedMethod;
60
+		$this->requestedUri    = $requestedUri;
61
+		$this->allowedMethods  = $allowedMethods;
62
+		parent::__construct($message, $code);
63
+	}
64 64
 
65
-    /**
66
-     * Verify if the given HTTP method is allowed for the request.
67
-     *
68
-     * @param string $method An HTTP method
69
-     * @return bool
70
-     */
65
+	/**
66
+	 * Verify if the given HTTP method is allowed for the request.
67
+	 *
68
+	 * @param string $method An HTTP method
69
+	 * @return bool
70
+	 */
71 71
 
72
-    public function can($method)
73
-    {
74
-        return in_array(strtolower($method), $this->allowedMethods);
75
-    }
72
+	public function can($method)
73
+	{
74
+		return in_array(strtolower($method), $this->allowedMethods);
75
+	}
76 76
 
77
-    /**
78
-     * The HTTP specification requires that a 405 Method Not Allowed response include the 
79
-     * Allow: header to detail available methods for the requested resource.
80
-     *
81
-     * @see http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html section 14.7
82
-     * @return string
83
-     */
77
+	/**
78
+	 * The HTTP specification requires that a 405 Method Not Allowed response include the 
79
+	 * Allow: header to detail available methods for the requested resource.
80
+	 *
81
+	 * @see http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html section 14.7
82
+	 * @return string
83
+	 */
84 84
     
85
-    public function allowed()
86
-    {
87
-        return implode(', ', $this->allowedMethods);
88
-    }
85
+	public function allowed()
86
+	{
87
+		return implode(', ', $this->allowedMethods);
88
+	}
89 89
 
90 90
 }
Please login to merge, or discard this patch.
src/Group.php 2 patches
Indentation   +220 added lines, -220 removed lines patch added patch discarded remove patch
@@ -19,225 +19,225 @@
 block discarded – undo
19 19
 class Group
20 20
 {
21 21
 
22
-    /**
23
-     * All grouped route objects.
24
-     *
25
-     * @var Route[]
26
-     */
27
-
28
-    protected $routes;
29
-
30
-    /**
31
-     * Set a new Route or merge an existing group of routes.
32
-     *
33
-     * @param Group|Route $route
34
-     * @return self
35
-     */
36
-
37
-    public function set($route)
38
-    {
39
-        if ($route instanceof Group) {
40
-            foreach ($route->all() as $r)
41
-                $this->routes[] = $r;
42
-        } else  $this->routes[] = $route;
43
-        return  $this;
44
-    }
45
-
46
-    /**
47
-     * A fast way to register a route into the group
48
-     *
49
-     * @param Route $route
50
-     */
51
-
52
-    public function setRoute(Route $route)
53
-    {
54
-        $this->routes[] = $route;
55
-    }
56
-
57
-    /**
58
-     * Return all grouped routes objects.
59
-     *
60
-     * @return Route[]
61
-     */
62
-
63
-    public function all()
64
-    {
65
-        return $this->routes;
66
-    }
67
-
68
-    /**
69
-     * Get a specific route of the group, routes receive a key based on
70
-     * the order they are added to the group.
71
-     *
72
-     * @param int $number
73
-     * @return Route
74
-     */
75
-
76
-    public function nth($number)
77
-    {
78
-        return $this->routes[$number];
79
-    }
80
-
81
-    /**
82
-     * Forget the registration of all grouped routes on to collector.
83
-     * After the forget the route object will still exist but will not
84
-     * count for the matcher.
85
-     *
86
-     * @return self
87
-     */
88
-
89
-    public function forget()
90
-    {
91
-        foreach ($this->routes as $route)
92
-            $route->forget();
93
-        return $this;
94
-    }
95
-
96
-    /**
97
-     * Set one HTTP method to all grouped routes.
98
-     *
99
-     * @param string $method The HTTP Method
100
-     * @return self
101
-     */
102
-
103
-    public function setMethod($method)
104
-    {
105
-        foreach ($this->routes as $route)
106
-            $route->setMethod($method);
107
-        return $this;
108
-    }
109
-
110
-    /**
111
-     * Set one action to all grouped routes.
112
-     *
113
-     * @param string $action
114
-     * @return self
115
-     */
116
-
117
-    public function setAction($action)
118
-    {
119
-        foreach ($this->routes as $route)
120
-            $route->setAction($action);
121
-        return $this;
122
-    }
123
-
124
-    /**
125
-     * Set one namespace to all grouped routes.
126
-     *
127
-     * @param string $namespace
128
-     * @return self
129
-     */
130
-
131
-    public function setNamespace($namespace)
132
-    {
133
-        foreach ($this->routes as $route)
134
-            $route->setNamespace($namespace);
135
-        return $this;
136
-    }
137
-
138
-    /**
139
-     * Add a prefix to all grouped routes pattern.
140
-     *
141
-     * @param string $prefix
142
-     * @return self
143
-     */
144
-
145
-    public function setPrefix($prefix)
146
-    {
147
-        $prefix = "/" . ltrim($prefix, "/");
148
-        foreach ($this->routes as $route)
149
-            $route->setPattern(rtrim($prefix . $route->getPattern(), "/"));
150
-        return $this;
151
-    }
152
-
153
-    /**
154
-     * Set metadata to all grouped routes.
155
-     *
156
-     * @param string $key
157
-     * @param string $value
158
-     *
159
-     * @return $this
160
-     */
161
-
162
-    public function setMetadata($key, $value)
163
-    {
164
-        foreach ($this->routes as $route)
165
-            $route->setMetadata($key, $value);
166
-        return $this;
167
-    }
168
-
169
-    /**
170
-     * Set a bunch of metadata to all grouped routes.
171
-     *
172
-     * @param mixed[] $metadata
173
-     * @return $this
174
-     */
175
-
176
-    public function setMetadataArray(array $metadata)
177
-    {
178
-        foreach ($this->routes as $route)
179
-            $route->setMetadataArray($metadata);
180
-        return $this;
181
-    }
182
-
183
-    /**
184
-     * Set default parameters to all grouped routes.
185
-     *
186
-     * @param mixed[] $defaults
187
-     * @return $this
188
-     */
189
-
190
-    public function setDefaults(array $defaults)
191
-    {
192
-        foreach ($this->routes as $route)
193
-            $route->setDefaults($defaults);
194
-        return $this;
195
-    }
196
-
197
-    /**
198
-     * Set a default parameter to all grouped routes.
199
-     *
200
-     * @param string $key
201
-     * @param mixed $value
202
-     *
203
-     * @return $this
204
-     */
205
-
206
-    public function setDefault($key, $value)
207
-    {
208
-        foreach ($this->routes as $route)
209
-            $route->setDefault($key, $value);
210
-        return $this;
211
-    }
212
-
213
-    /**
214
-     * Set one dispatch strategy to all grouped routes.
215
-     *
216
-     * @param string|Strategies\StrategyInterface $strategy
217
-     * @return self
218
-     */
219
-
220
-    public function setStrategy($strategy)
221
-    {
222
-        foreach ($this->routes as $route)
223
-            $route->setStrategy($strategy);
224
-        return $this;
225
-    }
226
-
227
-    /**
228
-     * Replace or define a constraint for all dynamic segments named by $name.
229
-     *
230
-     * @param string $name
231
-     * @param string $regex
232
-     *
233
-     * @return self
234
-     */
235
-
236
-    public function setConstraint($name, $regex)
237
-    {
238
-        foreach ($this->routes as $route)
239
-            $route->setConstraint($name, $regex);
240
-        return $this;
241
-    }
22
+	/**
23
+	 * All grouped route objects.
24
+	 *
25
+	 * @var Route[]
26
+	 */
27
+
28
+	protected $routes;
29
+
30
+	/**
31
+	 * Set a new Route or merge an existing group of routes.
32
+	 *
33
+	 * @param Group|Route $route
34
+	 * @return self
35
+	 */
36
+
37
+	public function set($route)
38
+	{
39
+		if ($route instanceof Group) {
40
+			foreach ($route->all() as $r)
41
+				$this->routes[] = $r;
42
+		} else  $this->routes[] = $route;
43
+		return  $this;
44
+	}
45
+
46
+	/**
47
+	 * A fast way to register a route into the group
48
+	 *
49
+	 * @param Route $route
50
+	 */
51
+
52
+	public function setRoute(Route $route)
53
+	{
54
+		$this->routes[] = $route;
55
+	}
56
+
57
+	/**
58
+	 * Return all grouped routes objects.
59
+	 *
60
+	 * @return Route[]
61
+	 */
62
+
63
+	public function all()
64
+	{
65
+		return $this->routes;
66
+	}
67
+
68
+	/**
69
+	 * Get a specific route of the group, routes receive a key based on
70
+	 * the order they are added to the group.
71
+	 *
72
+	 * @param int $number
73
+	 * @return Route
74
+	 */
75
+
76
+	public function nth($number)
77
+	{
78
+		return $this->routes[$number];
79
+	}
80
+
81
+	/**
82
+	 * Forget the registration of all grouped routes on to collector.
83
+	 * After the forget the route object will still exist but will not
84
+	 * count for the matcher.
85
+	 *
86
+	 * @return self
87
+	 */
88
+
89
+	public function forget()
90
+	{
91
+		foreach ($this->routes as $route)
92
+			$route->forget();
93
+		return $this;
94
+	}
95
+
96
+	/**
97
+	 * Set one HTTP method to all grouped routes.
98
+	 *
99
+	 * @param string $method The HTTP Method
100
+	 * @return self
101
+	 */
102
+
103
+	public function setMethod($method)
104
+	{
105
+		foreach ($this->routes as $route)
106
+			$route->setMethod($method);
107
+		return $this;
108
+	}
109
+
110
+	/**
111
+	 * Set one action to all grouped routes.
112
+	 *
113
+	 * @param string $action
114
+	 * @return self
115
+	 */
116
+
117
+	public function setAction($action)
118
+	{
119
+		foreach ($this->routes as $route)
120
+			$route->setAction($action);
121
+		return $this;
122
+	}
123
+
124
+	/**
125
+	 * Set one namespace to all grouped routes.
126
+	 *
127
+	 * @param string $namespace
128
+	 * @return self
129
+	 */
130
+
131
+	public function setNamespace($namespace)
132
+	{
133
+		foreach ($this->routes as $route)
134
+			$route->setNamespace($namespace);
135
+		return $this;
136
+	}
137
+
138
+	/**
139
+	 * Add a prefix to all grouped routes pattern.
140
+	 *
141
+	 * @param string $prefix
142
+	 * @return self
143
+	 */
144
+
145
+	public function setPrefix($prefix)
146
+	{
147
+		$prefix = "/" . ltrim($prefix, "/");
148
+		foreach ($this->routes as $route)
149
+			$route->setPattern(rtrim($prefix . $route->getPattern(), "/"));
150
+		return $this;
151
+	}
152
+
153
+	/**
154
+	 * Set metadata to all grouped routes.
155
+	 *
156
+	 * @param string $key
157
+	 * @param string $value
158
+	 *
159
+	 * @return $this
160
+	 */
161
+
162
+	public function setMetadata($key, $value)
163
+	{
164
+		foreach ($this->routes as $route)
165
+			$route->setMetadata($key, $value);
166
+		return $this;
167
+	}
168
+
169
+	/**
170
+	 * Set a bunch of metadata to all grouped routes.
171
+	 *
172
+	 * @param mixed[] $metadata
173
+	 * @return $this
174
+	 */
175
+
176
+	public function setMetadataArray(array $metadata)
177
+	{
178
+		foreach ($this->routes as $route)
179
+			$route->setMetadataArray($metadata);
180
+		return $this;
181
+	}
182
+
183
+	/**
184
+	 * Set default parameters to all grouped routes.
185
+	 *
186
+	 * @param mixed[] $defaults
187
+	 * @return $this
188
+	 */
189
+
190
+	public function setDefaults(array $defaults)
191
+	{
192
+		foreach ($this->routes as $route)
193
+			$route->setDefaults($defaults);
194
+		return $this;
195
+	}
196
+
197
+	/**
198
+	 * Set a default parameter to all grouped routes.
199
+	 *
200
+	 * @param string $key
201
+	 * @param mixed $value
202
+	 *
203
+	 * @return $this
204
+	 */
205
+
206
+	public function setDefault($key, $value)
207
+	{
208
+		foreach ($this->routes as $route)
209
+			$route->setDefault($key, $value);
210
+		return $this;
211
+	}
212
+
213
+	/**
214
+	 * Set one dispatch strategy to all grouped routes.
215
+	 *
216
+	 * @param string|Strategies\StrategyInterface $strategy
217
+	 * @return self
218
+	 */
219
+
220
+	public function setStrategy($strategy)
221
+	{
222
+		foreach ($this->routes as $route)
223
+			$route->setStrategy($strategy);
224
+		return $this;
225
+	}
226
+
227
+	/**
228
+	 * Replace or define a constraint for all dynamic segments named by $name.
229
+	 *
230
+	 * @param string $name
231
+	 * @param string $regex
232
+	 *
233
+	 * @return self
234
+	 */
235
+
236
+	public function setConstraint($name, $regex)
237
+	{
238
+		foreach ($this->routes as $route)
239
+			$route->setConstraint($name, $regex);
240
+		return $this;
241
+	}
242 242
 
243 243
 }
Please login to merge, or discard this patch.
Braces   +39 added lines, -25 removed lines patch added patch discarded remove patch
@@ -37,9 +37,12 @@  discard block
 block discarded – undo
37 37
     public function set($route)
38 38
     {
39 39
         if ($route instanceof Group) {
40
-            foreach ($route->all() as $r)
41
-                $this->routes[] = $r;
42
-        } else  $this->routes[] = $route;
40
+            foreach ($route->all() as $r) {
41
+                            $this->routes[] = $r;
42
+            }
43
+        } else {
44
+        	$this->routes[] = $route;
45
+        }
43 46
         return  $this;
44 47
     }
45 48
 
@@ -88,8 +91,9 @@  discard block
 block discarded – undo
88 91
 
89 92
     public function forget()
90 93
     {
91
-        foreach ($this->routes as $route)
92
-            $route->forget();
94
+        foreach ($this->routes as $route) {
95
+                    $route->forget();
96
+        }
93 97
         return $this;
94 98
     }
95 99
 
@@ -102,8 +106,9 @@  discard block
 block discarded – undo
102 106
 
103 107
     public function setMethod($method)
104 108
     {
105
-        foreach ($this->routes as $route)
106
-            $route->setMethod($method);
109
+        foreach ($this->routes as $route) {
110
+                    $route->setMethod($method);
111
+        }
107 112
         return $this;
108 113
     }
109 114
 
@@ -116,8 +121,9 @@  discard block
 block discarded – undo
116 121
 
117 122
     public function setAction($action)
118 123
     {
119
-        foreach ($this->routes as $route)
120
-            $route->setAction($action);
124
+        foreach ($this->routes as $route) {
125
+                    $route->setAction($action);
126
+        }
121 127
         return $this;
122 128
     }
123 129
 
@@ -130,8 +136,9 @@  discard block
 block discarded – undo
130 136
 
131 137
     public function setNamespace($namespace)
132 138
     {
133
-        foreach ($this->routes as $route)
134
-            $route->setNamespace($namespace);
139
+        foreach ($this->routes as $route) {
140
+                    $route->setNamespace($namespace);
141
+        }
135 142
         return $this;
136 143
     }
137 144
 
@@ -145,8 +152,9 @@  discard block
 block discarded – undo
145 152
     public function setPrefix($prefix)
146 153
     {
147 154
         $prefix = "/" . ltrim($prefix, "/");
148
-        foreach ($this->routes as $route)
149
-            $route->setPattern(rtrim($prefix . $route->getPattern(), "/"));
155
+        foreach ($this->routes as $route) {
156
+                    $route->setPattern(rtrim($prefix . $route->getPattern(), "/"));
157
+        }
150 158
         return $this;
151 159
     }
152 160
 
@@ -161,8 +169,9 @@  discard block
 block discarded – undo
161 169
 
162 170
     public function setMetadata($key, $value)
163 171
     {
164
-        foreach ($this->routes as $route)
165
-            $route->setMetadata($key, $value);
172
+        foreach ($this->routes as $route) {
173
+                    $route->setMetadata($key, $value);
174
+        }
166 175
         return $this;
167 176
     }
168 177
 
@@ -175,8 +184,9 @@  discard block
 block discarded – undo
175 184
 
176 185
     public function setMetadataArray(array $metadata)
177 186
     {
178
-        foreach ($this->routes as $route)
179
-            $route->setMetadataArray($metadata);
187
+        foreach ($this->routes as $route) {
188
+                    $route->setMetadataArray($metadata);
189
+        }
180 190
         return $this;
181 191
     }
182 192
 
@@ -189,8 +199,9 @@  discard block
 block discarded – undo
189 199
 
190 200
     public function setDefaults(array $defaults)
191 201
     {
192
-        foreach ($this->routes as $route)
193
-            $route->setDefaults($defaults);
202
+        foreach ($this->routes as $route) {
203
+                    $route->setDefaults($defaults);
204
+        }
194 205
         return $this;
195 206
     }
196 207
 
@@ -205,8 +216,9 @@  discard block
 block discarded – undo
205 216
 
206 217
     public function setDefault($key, $value)
207 218
     {
208
-        foreach ($this->routes as $route)
209
-            $route->setDefault($key, $value);
219
+        foreach ($this->routes as $route) {
220
+                    $route->setDefault($key, $value);
221
+        }
210 222
         return $this;
211 223
     }
212 224
 
@@ -219,8 +231,9 @@  discard block
 block discarded – undo
219 231
 
220 232
     public function setStrategy($strategy)
221 233
     {
222
-        foreach ($this->routes as $route)
223
-            $route->setStrategy($strategy);
234
+        foreach ($this->routes as $route) {
235
+                    $route->setStrategy($strategy);
236
+        }
224 237
         return $this;
225 238
     }
226 239
 
@@ -235,8 +248,9 @@  discard block
 block discarded – undo
235 248
 
236 249
     public function setConstraint($name, $regex)
237 250
     {
238
-        foreach ($this->routes as $route)
239
-            $route->setConstraint($name, $regex);
251
+        foreach ($this->routes as $route) {
252
+                    $route->setConstraint($name, $regex);
253
+        }
240 254
         return $this;
241 255
     }
242 256
 
Please login to merge, or discard this patch.