Skip to content

Commit 8088fd1

Browse files
committed
Update RouterCommand class.
1 parent 74fb53c commit 8088fd1

File tree

2 files changed

+37
-15
lines changed

2 files changed

+37
-15
lines changed

src/Router.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -445,7 +445,7 @@ private function addRoute($uri, $method, $callback, $settings)
445445
*/
446446
private function beforeAfterCommand($command)
447447
{
448-
return RouterCommand::beforeAfter(
448+
return RouterCommand::getInstance()->beforeAfter(
449449
$command, $this->middlewares[$command], $this->paths['middlewares'], $this->namespaces['middlewares']
450450
);
451451
}
@@ -457,7 +457,7 @@ private function beforeAfterCommand($command)
457457
*/
458458
private function runRouteCommand($command, $params = null)
459459
{
460-
RouterCommand::runRoute(
460+
RouterCommand::getInstance()->runRoute(
461461
$command, $params, $this->paths['controllers'], $this->namespaces['controllers']
462462
);
463463
}
@@ -472,16 +472,16 @@ public function runRouteMiddleware($middleware, $type)
472472
if($type == 'before')
473473
{
474474
if(!is_null($middleware['group']))
475-
RouterCommand::beforeAfter($middleware['group'][$type], $this->middlewares, $this->paths['middlewares'], $this->namespaces['middlewares']);
475+
RouterCommand::getInstance()->beforeAfter($middleware['group'][$type], $this->middlewares, $this->paths['middlewares'], $this->namespaces['middlewares']);
476476

477-
RouterCommand::beforeAfter($middleware[$type], $this->middlewares, $this->paths['middlewares'], $this->namespaces['middlewares']);
477+
RouterCommand::getInstance()->beforeAfter($middleware[$type], $this->middlewares, $this->paths['middlewares'], $this->namespaces['middlewares']);
478478
}
479479
else
480480
{
481-
RouterCommand::beforeAfter($middleware[$type], $this->middlewares, $this->paths['middlewares'], $this->namespaces['middlewares']);
481+
RouterCommand::getInstance()->beforeAfter($middleware[$type], $this->middlewares, $this->paths['middlewares'], $this->namespaces['middlewares']);
482482

483483
if(!is_null($middleware['group']))
484-
RouterCommand::beforeAfter($middleware['group'][$type], $this->middlewares, $this->paths['middlewares'], $this->namespaces['middlewares']);
484+
RouterCommand::getInstance()->beforeAfter($middleware['group'][$type], $this->middlewares, $this->paths['middlewares'], $this->namespaces['middlewares']);
485485
}
486486
}
487487

src/Router/RouterCommand.php

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,30 @@
1515

1616
class RouterCommand
1717
{
18+
/**
19+
* Class instance variable
20+
*/
21+
protected static $instance = null;
1822

19-
public static function exception($message = '')
23+
/**
24+
* Get class instance
25+
*
26+
* @return PdoxObject
27+
*/
28+
public static function getInstance()
29+
{
30+
if (null === self::$instance)
31+
self::$instance = new static();
32+
33+
return self::$instance;
34+
}
35+
36+
/**
37+
* Throw new Exception for Router Error
38+
*
39+
* @return RouterException
40+
*/
41+
public function exception($message = '')
2042
{
2143
return new RouterException($message);
2244
}
@@ -26,13 +48,13 @@ public static function exception($message = '')
2648
*
2749
* @return true | false
2850
*/
29-
public static function beforeAfter($command, $middleware, $path = '', $namespace = '')
51+
public function beforeAfter($command, $middleware, $path = '', $namespace = '')
3052
{
3153
if(!is_null($command))
3254
{
3355
if(is_array($command))
3456
foreach ($command as $key => $value)
35-
self::beforeAfter($value, $middleware);
57+
$this->beforeAfter($value, $middleware);
3658

3759
elseif(is_object($command))
3860
return call_user_func($command);
@@ -48,20 +70,20 @@ public static function beforeAfter($command, $middleware, $path = '', $namespace
4870
$middlewareFile = realpath($path . $parts[0] . '/' . (str_replace($namespace, '', $segments[0])) .'.php');
4971

5072
if(!file_exists($middlewareFile))
51-
return self::exception($segments[0] . ' middleware file is not found. Please, check file.');
73+
return $this->exception($segments[0] . ' middleware file is not found. Please, check file.');
5274

5375
require_once($middlewareFile);
5476
$controller = new $segments[0]();
5577

5678
if(in_array($segments[1], get_class_methods($controller)))
5779
return call_user_func([$controller, $segments[1]]);
5880
else
59-
return self::exception($segments[1] . ' method is not found in <b>'.$segments[0].'</b> middleware. Please, check file.');
81+
return $this->exception($segments[1] . ' method is not found in <b>'.$segments[0].'</b> middleware. Please, check file.');
6082
}
6183
else
6284
{
6385
if(!is_null($middleware[$command]) && isset($middleware[$command]))
64-
self::beforeAfter($middleware[$command], $middleware);
86+
$this->beforeAfter($middleware[$command], $middleware);
6587
else
6688
return false;
6789
}
@@ -75,7 +97,7 @@ public static function beforeAfter($command, $middleware, $path = '', $namespace
7597
*
7698
* @return null
7799
*/
78-
public static function runRoute($command, $params = null, $path = '', $namespace = '')
100+
public function runRoute($command, $params = null, $path = '', $namespace = '')
79101
{
80102
if(!is_object($command))
81103
{
@@ -88,7 +110,7 @@ public static function runRoute($command, $params = null, $path = '', $namespace
88110
$controllerFile = realpath($path . $parts[0] . '/' . ($segments[0]).'.php');
89111

90112
if(!file_exists($controllerFile))
91-
return self::exception($segments[0] . ' Controller File is not found. Please, check file.');
113+
return $this->exception($segments[0] . ' Controller File is not found. Please, check file.');
92114

93115
require_once($controllerFile);
94116
$controller = new $segments[0]();
@@ -98,7 +120,7 @@ public static function runRoute($command, $params = null, $path = '', $namespace
98120
elseif(is_null($params) && in_array($segments[1], get_class_methods($controller)))
99121
echo call_user_func([$controller, $segments[1]]);
100122
else
101-
return self::exception($segments[1] . ' method is not found in '.$segments[0].' controller. Please, check file.');
123+
return $this->exception($segments[1] . ' method is not found in '.$segments[0].' controller. Please, check file.');
102124
}
103125
else
104126
if(!is_null($params))

0 commit comments

Comments
 (0)