Skip to content

Commit 8ce25bc

Browse files
committed
Updated Router class. Some bugs fixed.
1 parent 4b375cc commit 8ce25bc

File tree

4 files changed

+45
-42
lines changed

4 files changed

+45
-42
lines changed

src/Router.php

Lines changed: 32 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@ class Router
6767
*/
6868
protected $mainMethod = 'main';
6969

70+
/**
71+
* @var $errorCallback Route error callback function
72+
*/
7073
protected $errorCallback;
7174

7275
/**
@@ -121,6 +124,14 @@ protected function setPaths($params)
121124
? trim($namespaces['middlewares'], '\\') . '\\'
122125
: '';
123126
}
127+
128+
if (isset($params['base_folder'])) {
129+
$this->baseFolder = rtrim($params['base_folder'], '/');
130+
}
131+
132+
if (isset($params['main_method'])) {
133+
$this->mainMethod = $params['main_method'];
134+
}
124135
}
125136

126137
/**
@@ -131,11 +142,11 @@ protected function setPaths($params)
131142
*/
132143
public function __call($method, $params)
133144
{
134-
if(is_null($params)) {
145+
if (is_null($params)) {
135146
return;
136147
}
137148

138-
if( !in_array(strtoupper($method), explode('|', RouterRequest::$validMethods)) ) {
149+
if (! in_array(strtoupper($method), explode('|', RouterRequest::$validMethods)) ) {
139150
return $this->exception($method . ' is not valid.');
140151
}
141152

@@ -248,7 +259,7 @@ public function run()
248259
$base = str_replace('\\', '/', str_replace($documentRoot, '', $getCwd) . '/');
249260
$uri = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
250261

251-
if (($base != $uri) && (substr($uri, -1) == '/')) {
262+
if (($base !== $uri) && (substr($uri, -1) === '/')) {
252263
$uri = substr($uri, 0, (strlen($uri)-1));
253264
}
254265

@@ -269,7 +280,7 @@ public function run()
269280
// check if route is defined without regex
270281
if (in_array($uri, array_values($routes))) {
271282
foreach ($this->routes as $data) {
272-
if (RouterRequest::validMethod($data['method'], $method) && ($data['route'] == $uri)) {
283+
if (RouterRequest::validMethod($data['method'], $method) && ($data['route'] === $uri)) {
273284
$foundRoute = true;
274285
$this->runRouteMiddleware($data, 'before');
275286
$this->runRouteCommand($data['callback']);
@@ -317,7 +328,7 @@ public function run()
317328
ob_end_clean();
318329
}
319330

320-
if ($foundRoute == false) {
331+
if ($foundRoute === false) {
321332
if (! $this->errorCallback) {
322333
$this->errorCallback = function() {
323334
header($_SERVER['SERVER_PROTOCOL']." 404 Not Found");
@@ -348,7 +359,7 @@ public function group($name, $settings = null, $callback = null)
348359
$callback = $settings;
349360
} else {
350361
$group['before'][] = (!isset($settings['before']) ? null : $settings['before']);
351-
$group['after'][] = (!isset($settings['after']) ? null : $settings['after']);
362+
$group['after'][] = (!isset($settings['after']) ? null : $settings['after']);
352363
}
353364

354365
$groupCount = count($this->groups);
@@ -422,27 +433,25 @@ public function controller($route, $settings, $controller = null)
422433
$classMethods = get_class_methods($this->namespaces['controllers'] . $controller);
423434
if ($classMethods) {
424435
foreach ($classMethods as $methodName) {
425-
if (! strstr($methodName, '__')) {
426-
$method = 'any';
427-
foreach (explode('|', RouterRequest::$validMethods) as $m) {
428-
if (stripos($methodName, strtolower($m), 0) === 0) {
436+
if(!strstr($methodName, '__')) {
437+
$method = "any";
438+
foreach(explode('|', RouterRequest::$validMethods) as $m) {
439+
if(stripos($methodName, strtolower($m), 0) === 0) {
429440
$method = strtolower($m);
430441
break;
431442
}
432443
}
433444

434445
$methodVar = lcfirst(str_replace($method, '', $methodName));
435446
$r = new ReflectionMethod($this->namespaces['controllers'] . $controller, $methodName);
436-
$requiredParam = $r->getNumberOfRequiredParameters();
447+
$reqiredParam = $r->getNumberOfRequiredParameters();
437448
$totalParam = $r->getNumberOfParameters();
438449

439-
$value = ($methodVar === 'main' ? $route : $route . '/' . $methodVar);
440-
441-
$this->addRoute(
442-
($value.str_repeat('/{a}', $requiredParam).str_repeat('/{a?}', $totalParam-$requiredParam)),
443-
$method,
444-
($controller.'@'.$methodName),
445-
$settings
450+
$value = ($methodVar === $this->mainMethod ? $route : $route.'/'.$methodVar);
451+
$this->{$method}(
452+
($value.str_repeat('/{a}', $reqiredParam).str_repeat('/{a?}', $totalParam-$reqiredParam)),
453+
$settings,
454+
($controller . '@' . $methodName)
446455
);
447456
}
448457
}
@@ -483,39 +492,32 @@ private function addRoute($uri, $method, $callback, $settings)
483492
}
484493

485494
$page = dirname($_SERVER['PHP_SELF']);
486-
$page = $page == '/' ? '' : $page;
495+
$page = $page === '/' ? '' : $page;
487496
if (strstr($page, 'index.php')) {
488497
$data = implode('/', explode('/', $page));
489498
$page = str_replace($data, '', $page);
490499
}
491500

492501
$route = $page . $group . '/' . trim($uri, '/');
493502
$route = rtrim($route, '/');
494-
if ($route == $page) {
503+
if ($route === $page) {
495504
$route .= '/';
496505
}
497506

498507
$data = [
499508
'route' => str_replace('//', '/', $route),
500509
'method' => strtoupper($method),
501-
'callback' => (is_object($callback)
502-
? $callback
503-
: $this->namespaces['controllers'] . $callback
504-
),
510+
'callback' => $callback,
505511
'name' => (isset($settings['name'])
506512
? $settings['name']
507513
: null
508514
),
509515
'before' => (isset($settings['before'])
510-
? (is_string($settings['before'])
511-
? $this->namespaces['middlewares'] . $settings['before']
512-
: $settings['before'])
516+
? $settings['before']
513517
: null
514518
),
515519
'after' => (isset($settings['after'])
516-
? (is_string($settings['after'])
517-
? $this->namespaces['middlewares'] . $settings['after']
518-
: $settings['after'])
520+
? $settings['after']
519521
: null
520522
),
521523
'group' => ($groupItem === -1) ? null : $this->groups[$groupItem]

src/Router/RouterCommand.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -90,11 +90,11 @@ public function runRoute($command, $params = null, $path = '', $namespace = '')
9090
$controllerMethod = $segments[1];
9191

9292
$controller = $this->resolveClass($controllerClass, $path, $namespace);
93-
if (! is_null($params) && method_exists($controller, $controllerMethod)) {
94-
echo call_user_func_array([$controller, $controllerMethod], $params);
95-
return;
96-
} elseif (is_null($params) && method_exists($controller, $controllerMethod)) {
97-
echo call_user_func([$controller, $controllerMethod]);
93+
if (method_exists($controller, $controllerMethod)) {
94+
echo call_user_func_array(
95+
[$controller, $controllerMethod],
96+
(!is_null($params) ? $params : [])
97+
);
9898
return;
9999
}
100100

src/Router/RouterException.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ class RouterException
2222
/**
2323
* Create Exception Class.
2424
*
25-
* @return string|Exception
25+
* @return string
26+
* @throw Exception
2627
*/
2728
public function __construct($message)
2829
{

src/Router/RouterRequest.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,11 @@ public static function validMethod($data, $method)
4747
protected static function checkMethods($value, $method)
4848
{
4949
$valid = false;
50-
if ($value == 'AJAX' && self::isAjax && $value == $method) {
50+
if ($value === 'AJAX' && self::isAjax && $value === $method) {
5151
$valid = true;
52-
} elseif ($value == 'AJAXP' && self::isAjax && $method == 'POST') {
52+
} elseif ($value === 'AJAXP' && self::isAjax && $method === 'POST') {
5353
$valid = true;
54-
} elseif (in_array($value, explode('|', self::$validMethods)) && ($value == $method || $value == 'ANY')) {
54+
} elseif (in_array($value, explode('|', self::$validMethods)) && ($value === $method || $value === 'ANY')) {
5555
$valid = true;
5656
}
5757

@@ -61,11 +61,11 @@ protected static function checkMethods($value, $method)
6161
/**
6262
* Check ajax request
6363
*
64-
* @return array
64+
* @return bool
6565
*/
6666
protected static function isAjax()
6767
{
68-
return (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest');
68+
return (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH'] === 'XMLHttpRequest');
6969
}
7070

7171
/**
@@ -83,7 +83,7 @@ protected static function getRequestHeaders()
8383
// Method getallheaders() not available: manually extract 'm
8484
$headers = [];
8585
foreach ($_SERVER as $name => $value) {
86-
if ((substr($name, 0, 5) == 'HTTP_') || ($name == 'CONTENT_TYPE') || ($name == 'CONTENT_LENGTH')) {
86+
if ((substr($name, 0, 5) == 'HTTP_') || ($name === 'CONTENT_TYPE') || ($name === 'CONTENT_LENGTH')) {
8787
$headers[str_replace([' ', 'Http'], ['-', 'HTTP'], ucwords(strtolower(str_replace('_', ' ', substr($name, 5)))))] = $value;
8888
}
8989
}

0 commit comments

Comments
 (0)