@@ -7,85 +7,85 @@ |
||
| 7 | 7 | |
| 8 | 8 | abstract class AbstractRouter { |
| 9 | 9 | |
| 10 | - /** |
|
| 11 | - * Application instance |
|
| 12 | - * |
|
| 13 | - * @var Application |
|
| 14 | - */ |
|
| 15 | - private $application; |
|
| 10 | + /** |
|
| 11 | + * Application instance |
|
| 12 | + * |
|
| 13 | + * @var Application |
|
| 14 | + */ |
|
| 15 | + private $application; |
|
| 16 | 16 | |
| 17 | - public function setApplication(Application $application) |
|
| 18 | - { |
|
| 19 | - $this->application = $application; |
|
| 20 | - foreach($this->routes as $routeName => $controller) { |
|
| 21 | - $this->application->registerController($controller, $routeName); |
|
| 22 | - } |
|
| 23 | - } |
|
| 17 | + public function setApplication(Application $application) |
|
| 18 | + { |
|
| 19 | + $this->application = $application; |
|
| 20 | + foreach($this->routes as $routeName => $controller) { |
|
| 21 | + $this->application->registerController($controller, $routeName); |
|
| 22 | + } |
|
| 23 | + } |
|
| 24 | 24 | |
| 25 | - /** |
|
| 26 | - * @var array |
|
| 27 | - */ |
|
| 28 | - private $routes; |
|
| 25 | + /** |
|
| 26 | + * @var array |
|
| 27 | + */ |
|
| 28 | + private $routes; |
|
| 29 | 29 | |
| 30 | - public function addRoute(string $name, AbstractController $controller) |
|
| 31 | - { |
|
| 32 | - $this->routes[$name] = $controller; |
|
| 33 | - } |
|
| 30 | + public function addRoute(string $name, AbstractController $controller) |
|
| 31 | + { |
|
| 32 | + $this->routes[$name] = $controller; |
|
| 33 | + } |
|
| 34 | 34 | |
| 35 | - /** |
|
| 36 | - * Checks wether a route exists |
|
| 37 | - * |
|
| 38 | - * @param string $name |
|
| 39 | - * @return bool |
|
| 40 | - */ |
|
| 41 | - public function hasRoute(string $name) |
|
| 42 | - { |
|
| 43 | - return key_exists($name, $this->routes); |
|
| 44 | - } |
|
| 35 | + /** |
|
| 36 | + * Checks wether a route exists |
|
| 37 | + * |
|
| 38 | + * @param string $name |
|
| 39 | + * @return bool |
|
| 40 | + */ |
|
| 41 | + public function hasRoute(string $name) |
|
| 42 | + { |
|
| 43 | + return key_exists($name, $this->routes); |
|
| 44 | + } |
|
| 45 | 45 | |
| 46 | - /** |
|
| 47 | - * |
|
| 48 | - * @param string $name |
|
| 49 | - * @throws RouterException |
|
| 50 | - * @return AbstractController |
|
| 51 | - */ |
|
| 52 | - private function getRoute(string $name) |
|
| 53 | - { |
|
| 54 | - if(!$this->hasRoute($name)) { |
|
| 55 | - throw new RouterException("Router {$router} is not registered"); |
|
| 56 | - } |
|
| 46 | + /** |
|
| 47 | + * |
|
| 48 | + * @param string $name |
|
| 49 | + * @throws RouterException |
|
| 50 | + * @return AbstractController |
|
| 51 | + */ |
|
| 52 | + private function getRoute(string $name) |
|
| 53 | + { |
|
| 54 | + if(!$this->hasRoute($name)) { |
|
| 55 | + throw new RouterException("Router {$router} is not registered"); |
|
| 56 | + } |
|
| 57 | 57 | |
| 58 | - return $this->routes[$name]; |
|
| 59 | - } |
|
| 58 | + return $this->routes[$name]; |
|
| 59 | + } |
|
| 60 | 60 | |
| 61 | - /** |
|
| 62 | - * Route the existing request into a new controller |
|
| 63 | - * |
|
| 64 | - * @param string $name The name of route |
|
| 65 | - * @param Request $request The existing request instance |
|
| 66 | - * @return \Nkey\Caribu\Mvc\Controller\AbstractController |
|
| 67 | - */ |
|
| 68 | - public function route(string $name, Request $request) |
|
| 69 | - { |
|
| 70 | - $parts = \explode('/', $request->getOrigin()); |
|
| 71 | - $found = false; |
|
| 72 | - for($i = 0; $i < count($parts); $i++) { |
|
| 73 | - if($parts[$i] === $name && isset($parts[$i+1])) { |
|
| 74 | - $action = $parts[$i+1]; |
|
| 75 | - if(strpos($action, "?")) { |
|
| 76 | - $action = strstr($action, "?"); |
|
| 77 | - } |
|
| 61 | + /** |
|
| 62 | + * Route the existing request into a new controller |
|
| 63 | + * |
|
| 64 | + * @param string $name The name of route |
|
| 65 | + * @param Request $request The existing request instance |
|
| 66 | + * @return \Nkey\Caribu\Mvc\Controller\AbstractController |
|
| 67 | + */ |
|
| 68 | + public function route(string $name, Request $request) |
|
| 69 | + { |
|
| 70 | + $parts = \explode('/', $request->getOrigin()); |
|
| 71 | + $found = false; |
|
| 72 | + for($i = 0; $i < count($parts); $i++) { |
|
| 73 | + if($parts[$i] === $name && isset($parts[$i+1])) { |
|
| 74 | + $action = $parts[$i+1]; |
|
| 75 | + if(strpos($action, "?")) { |
|
| 76 | + $action = strstr($action, "?"); |
|
| 77 | + } |
|
| 78 | 78 | |
| 79 | - $request->setAction($action); |
|
| 80 | - $found = true; |
|
| 81 | - } |
|
| 82 | - } |
|
| 83 | - if(!$found) { |
|
| 84 | - $request->setAction("index"); |
|
| 85 | - } |
|
| 86 | - $controller = $this->getRoute($name); |
|
| 87 | - $request->setController($controller->getControllerSimpleName()); |
|
| 79 | + $request->setAction($action); |
|
| 80 | + $found = true; |
|
| 81 | + } |
|
| 82 | + } |
|
| 83 | + if(!$found) { |
|
| 84 | + $request->setAction("index"); |
|
| 85 | + } |
|
| 86 | + $controller = $this->getRoute($name); |
|
| 87 | + $request->setController($controller->getControllerSimpleName()); |
|
| 88 | 88 | |
| 89 | - return $controller; |
|
| 90 | - } |
|
| 89 | + return $controller; |
|
| 90 | + } |
|
| 91 | 91 | } |
| 92 | 92 | \ No newline at end of file |
@@ -17,7 +17,7 @@ discard block |
||
| 17 | 17 | public function setApplication(Application $application) |
| 18 | 18 | { |
| 19 | 19 | $this->application = $application; |
| 20 | - foreach($this->routes as $routeName => $controller) { |
|
| 20 | + foreach ($this->routes as $routeName => $controller) { |
|
| 21 | 21 | $this->application->registerController($controller, $routeName); |
| 22 | 22 | } |
| 23 | 23 | } |
@@ -51,7 +51,7 @@ discard block |
||
| 51 | 51 | */ |
| 52 | 52 | private function getRoute(string $name) |
| 53 | 53 | { |
| 54 | - if(!$this->hasRoute($name)) { |
|
| 54 | + if (!$this->hasRoute($name)) { |
|
| 55 | 55 | throw new RouterException("Router {$router} is not registered"); |
| 56 | 56 | } |
| 57 | 57 | |
@@ -69,10 +69,10 @@ discard block |
||
| 69 | 69 | { |
| 70 | 70 | $parts = \explode('/', $request->getOrigin()); |
| 71 | 71 | $found = false; |
| 72 | - for($i = 0; $i < count($parts); $i++) { |
|
| 73 | - if($parts[$i] === $name && isset($parts[$i+1])) { |
|
| 74 | - $action = $parts[$i+1]; |
|
| 75 | - if(strpos($action, "?")) { |
|
| 72 | + for ($i = 0; $i < count($parts); $i++) { |
|
| 73 | + if ($parts[$i] === $name && isset($parts[$i + 1])) { |
|
| 74 | + $action = $parts[$i + 1]; |
|
| 75 | + if (strpos($action, "?")) { |
|
| 76 | 76 | $action = strstr($action, "?"); |
| 77 | 77 | } |
| 78 | 78 | |
@@ -80,7 +80,7 @@ discard block |
||
| 80 | 80 | $found = true; |
| 81 | 81 | } |
| 82 | 82 | } |
| 83 | - if(!$found) { |
|
| 83 | + if (!$found) { |
|
| 84 | 84 | $request->setAction("index"); |
| 85 | 85 | } |
| 86 | 86 | $controller = $this->getRoute($name); |