Skip to content

Commit 671f0bf

Browse files
committed
Updated Router and fixed bug.
1 parent 01f21e1 commit 671f0bf

File tree

2 files changed

+32
-9
lines changed

2 files changed

+32
-9
lines changed

src/Router.php

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@ function __construct($params = [])
5757
if(is_null($params) || empty($params) || !is_array($params))
5858
return;
5959

60-
RouterException::$debug = (isset($params['debug']) && $params['debug'] === true ? true : false);
60+
if(isset($params['debug']) && is_bool($params['debug']))
61+
RouterException::$debug = $params['debug'];
6162

6263
if(isset($params['paths']) && $paths = $params['paths'])
6364
{
@@ -160,19 +161,25 @@ public function pattern($pattern, $attr = null)
160161
if(!in_array('{' . $key . '}', array_keys($this->patterns)))
161162
$this->patterns['{' . $key . '}'] = '(' . $value . ')';
162163
else
163-
return new RouterException($key . ' pattern cannot be changed.');
164+
return $this->exception($key . ' pattern cannot be changed.');
164165
}
165166
else
166167
{
167168
if(!in_array('{' . $pattern . '}', array_keys($this->patterns)))
168169
$this->patterns['{' . $pattern . '}'] = '(' . $attr . ')';
169170
else
170-
return new RouterException($pattern . ' pattern cannot be changed.');
171+
return $this->exception($pattern . ' pattern cannot be changed.');
171172
}
172173

173174
return;
174175
}
175176

177+
178+
/**
179+
* Add new middleware
180+
*
181+
* @return
182+
*/
176183
public function middleware($name, $command)
177184
{
178185
$this->middlewares[$name] = $command;
@@ -273,7 +280,7 @@ public function run()
273280
$this->errorCallback = function()
274281
{
275282
header($_SERVER['SERVER_PROTOCOL']." 404 Not Found");
276-
return new RouterException('Bad Request :( Looks like something went wrong. Please try again.');
283+
return $this->exception('Route not found. Looks like something went wrong. Please try again.');
277284
};
278285
}
279286

@@ -342,7 +349,7 @@ public function controller($route, $controller)
342349
$req = require($controllerFile);
343350
}
344351
else
345-
return new RouterException($controller . " controller file is not found! Please, check file.");
352+
return $this->exception($controller . " controller file is not found! Please, check file.");
346353

347354
$classMethods = get_class_methods($this->namespaces['controllers'] . $controller);
348355

@@ -510,4 +517,14 @@ public function getRoutes()
510517
{
511518
return $this->routes;
512519
}
520+
521+
/**
522+
* Throw new Exception for Router Error
523+
*
524+
* @return RouterException
525+
*/
526+
public function exception($message = '')
527+
{
528+
return new RouterException($message);
529+
}
513530
}

src/Router/RouterCommand.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@
1515

1616
class RouterCommand
1717
{
18+
19+
public static function exception($message = '')
20+
{
21+
return new RouterException($message);
22+
}
23+
1824
/**
1925
* Run Route Middlewares
2026
*
@@ -42,15 +48,15 @@ public static function beforeAfter($command, $middleware, $path = '', $namespace
4248
$middlewareFile = realpath($path . $parts[0] . '/' . (str_replace($namespace, '', $segments[0])) .'.php');
4349

4450
if(!file_exists($middlewareFile))
45-
return new RouterException($segments[0] . ' middleware file is not found. Please, check file.');
51+
return self::exception($segments[0] . ' middleware file is not found. Please, check file.');
4652

4753
require_once($middlewareFile);
4854
$controller = new $segments[0]();
4955

5056
if(in_array($segments[1], get_class_methods($controller)))
5157
return call_user_func([$controller, $segments[1]]);
5258
else
53-
return new RouterException($segments[1] . ' method is not found in <b>'.$segments[0].'</b> middleware. Please, check file.');
59+
return self::exception($segments[1] . ' method is not found in <b>'.$segments[0].'</b> middleware. Please, check file.');
5460
}
5561
else
5662
{
@@ -82,7 +88,7 @@ public static function runRoute($command, $params = null, $path = '', $namespace
8288
$controllerFile = realpath($path . $parts[0] . '/' . ($segments[0]).'.php');
8389

8490
if(!file_exists($controllerFile))
85-
return new RouterException($segments[0] . ' Controller File is not found. Please, check file.');
91+
return self::exception($segments[0] . ' Controller File is not found. Please, check file.');
8692

8793
require_once($controllerFile);
8894
$controller = new $segments[0]();
@@ -92,7 +98,7 @@ public static function runRoute($command, $params = null, $path = '', $namespace
9298
elseif(is_null($params) && in_array($segments[1], get_class_methods($controller)))
9399
echo call_user_func([$controller, $segments[1]]);
94100
else
95-
return new RouterException($segments[1] . ' method is not found in '.$segments[0].' controller. Please, check file.');
101+
return self::exception($segments[1] . ' method is not found in '.$segments[0].' controller. Please, check file.');
96102
}
97103
else
98104
if(!is_null($params))

0 commit comments

Comments
 (0)