@@ -120,8 +120,8 @@ discard block |
||
| 120 | 120 | final public function matchController($controller) |
| 121 | 121 | { |
| 122 | 122 | return (in_array($controller, $this->controllers) || |
| 123 | - in_array('any', $this->controllers) || |
|
| 124 | - count($this->controllers) == 0); |
|
| 123 | + in_array('any', $this->controllers) || |
|
| 124 | + count($this->controllers) == 0); |
|
| 125 | 125 | } |
| 126 | 126 | |
| 127 | 127 | /** |
@@ -132,8 +132,8 @@ discard block |
||
| 132 | 132 | final public function matchAction($action) |
| 133 | 133 | { |
| 134 | 134 | return (in_array($action, $this->actions) || |
| 135 | - in_array('any', $this->actions) || |
|
| 136 | - count($this->actions) == 0); |
|
| 135 | + in_array('any', $this->actions) || |
|
| 136 | + count($this->actions) == 0); |
|
| 137 | 137 | } |
| 138 | 138 | |
| 139 | 139 | /** |
@@ -35,7 +35,7 @@ discard block |
||
| 35 | 35 | $class = isset($field['class']) ? $field['class'] : $field['name']; |
| 36 | 36 | |
| 37 | 37 | $rendered .= sprintf('<input type="%s" id="%s" class="%s" name="%s"/>', |
| 38 | - $fieldType, $id, $class, $field['name']); |
|
| 38 | + $fieldType, $id, $class, $field['name']); |
|
| 39 | 39 | } |
| 40 | 40 | |
| 41 | 41 | return $rendered; |
@@ -64,7 +64,7 @@ discard block |
||
| 64 | 64 | $label = isset($button['label']) ? $button['label'] : $button['name']; |
| 65 | 65 | |
| 66 | 66 | $rendered .= sprintf('<button type="%s" id="%s" class="%s" name="%s">%s</button>', |
| 67 | - $buttonType, $id, $class, $button['name'], $label); |
|
| 67 | + $buttonType, $id, $class, $button['name'], $label); |
|
| 68 | 68 | } |
| 69 | 69 | |
| 70 | 70 | return $rendered; |
@@ -83,16 +83,16 @@ discard block |
||
| 83 | 83 | } |
| 84 | 84 | |
| 85 | 85 | $formAction = sprintf("%s%s/%s", $contentPrefix, isset($parameters['controller']) ? |
| 86 | - $parameters['controller'] : lcfirst($request->getController()), isset($parameters['action']) ? |
|
| 87 | - $parameters['action'] : lcfirst($request->getAction())); |
|
| 86 | + $parameters['controller'] : lcfirst($request->getController()), isset($parameters['action']) ? |
|
| 87 | + $parameters['action'] : lcfirst($request->getAction())); |
|
| 88 | 88 | |
| 89 | 89 | if (isset($parameters['formAction'])) { |
| 90 | 90 | $formAction = $parameters['formAction']; |
| 91 | 91 | } |
| 92 | 92 | |
| 93 | 93 | $rendered = sprintf('<form action="%s" method="%s"%s>', $formAction, isset($parameters['formMethod']) ? |
| 94 | - $parameters['formMethod'] : "POST", isset($parameters['formClass']) ? |
|
| 95 | - (sprintf(' class="%s"', $parameters['formClass'])) : ""); |
|
| 94 | + $parameters['formMethod'] : "POST", isset($parameters['formClass']) ? |
|
| 95 | + (sprintf(' class="%s"', $parameters['formClass'])) : ""); |
|
| 96 | 96 | |
| 97 | 97 | $rendered = $this->renderFields($rendered, $parameters['fields']); |
| 98 | 98 | |
@@ -215,7 +215,7 @@ |
||
| 215 | 215 | $currentBody = $response->getBody(); |
| 216 | 216 | |
| 217 | 217 | if (! isset($this->viewParams[$controlIdentifier][$controlName]) |
| 218 | - || ! $view->hasControl($controlIdentifier)) { |
|
| 218 | + || ! $view->hasControl($controlIdentifier)) { |
|
| 219 | 219 | $response->setBody(str_replace($matches[0], '', $currentBody)); |
| 220 | 220 | continue; |
| 221 | 221 | } |
@@ -318,21 +318,21 @@ discard block |
||
| 318 | 318 | */ |
| 319 | 319 | public function registerController($controller, $applicationName = 'default') |
| 320 | 320 | { |
| 321 | - if ( !$controller instanceof \Nkey\Caribu\Mvc\Controller\AbstractController ) { |
|
| 322 | - if (! class_exists($controller)) { |
|
| 323 | - throw new ControllerException("No such controller class {controller} found", array( |
|
| 324 | - 'controller' => $controller |
|
| 325 | - )); |
|
| 326 | - } |
|
| 327 | - $c = new $controller(); |
|
| 328 | - if (! ($c instanceof AbstractController)) { |
|
| 329 | - throw new ControllerException("Controller {controller} is not in application scope", array( |
|
| 330 | - 'controller' => $controller |
|
| 331 | - )); |
|
| 332 | - } |
|
| 333 | - } |
|
| 321 | + if ( !$controller instanceof \Nkey\Caribu\Mvc\Controller\AbstractController ) { |
|
| 322 | + if (! class_exists($controller)) { |
|
| 323 | + throw new ControllerException("No such controller class {controller} found", array( |
|
| 324 | + 'controller' => $controller |
|
| 325 | + )); |
|
| 326 | + } |
|
| 327 | + $c = new $controller(); |
|
| 328 | + if (! ($c instanceof AbstractController)) { |
|
| 329 | + throw new ControllerException("Controller {controller} is not in application scope", array( |
|
| 330 | + 'controller' => $controller |
|
| 331 | + )); |
|
| 332 | + } |
|
| 333 | + } |
|
| 334 | 334 | else { |
| 335 | - $c = $controller; |
|
| 335 | + $c = $controller; |
|
| 336 | 336 | } |
| 337 | 337 | $settings = $c->getControllerSettings(); |
| 338 | 338 | $this->controllers[$applicationName][$settings->getControllerSimpleName()] = $settings; |
@@ -375,35 +375,35 @@ discard block |
||
| 375 | 375 | )); |
| 376 | 376 | |
| 377 | 377 | if ( null != $this->router && $this->router->hasRoute($action) ) { |
| 378 | - $controllerInstance = $this->router->route($action, $request); |
|
| 379 | - $action = $request->getAction(); |
|
| 378 | + $controllerInstance = $this->router->route($action, $request); |
|
| 379 | + $action = $request->getAction(); |
|
| 380 | 380 | } |
| 381 | 381 | else { |
| 382 | - if (! isset($this->controllers[$applicationName][$controller])) { |
|
| 383 | - $this->getLogger()->error("[{remote}] No such controller {controller}", array( |
|
| 384 | - 'remote' => $request->getRemoteHost(), |
|
| 385 | - 'controller' => $controller |
|
| 386 | - )); |
|
| 387 | - $controller = 'Error'; |
|
| 388 | - $action = 'error'; |
|
| 389 | - } |
|
| 382 | + if (! isset($this->controllers[$applicationName][$controller])) { |
|
| 383 | + $this->getLogger()->error("[{remote}] No such controller {controller}", array( |
|
| 384 | + 'remote' => $request->getRemoteHost(), |
|
| 385 | + 'controller' => $controller |
|
| 386 | + )); |
|
| 387 | + $controller = 'Error'; |
|
| 388 | + $action = 'error'; |
|
| 389 | + } |
|
| 390 | 390 | |
| 391 | - $controllerInstance = $this->controllers[$applicationName][$controller]; |
|
| 392 | - assert($controllerInstance instanceof AbstractController); |
|
| 393 | - if (! $controllerInstance->hasAction($action)) { |
|
| 394 | - $this->getLogger()->error("[{remote}] No such action {action}", array( |
|
| 395 | - 'remote' => $request->getRemoteHost(), |
|
| 396 | - 'action' => $action |
|
| 397 | - )); |
|
| 398 | - $controllerInstance = $this->controllers[$applicationName]['Error']; |
|
| 399 | - $action = 'error'; |
|
| 400 | - } |
|
| 391 | + $controllerInstance = $this->controllers[$applicationName][$controller]; |
|
| 392 | + assert($controllerInstance instanceof AbstractController); |
|
| 393 | + if (! $controllerInstance->hasAction($action)) { |
|
| 394 | + $this->getLogger()->error("[{remote}] No such action {action}", array( |
|
| 395 | + 'remote' => $request->getRemoteHost(), |
|
| 396 | + 'action' => $action |
|
| 397 | + )); |
|
| 398 | + $controllerInstance = $this->controllers[$applicationName]['Error']; |
|
| 399 | + $action = 'error'; |
|
| 400 | + } |
|
| 401 | 401 | |
| 402 | - $this->getLogger()->debug("[{remote}] Routing request to {controller}:{action}", array( |
|
| 403 | - 'remote' => $request->getRemoteHost(), |
|
| 404 | - 'controller' => $controller, |
|
| 405 | - 'action' => $action |
|
| 406 | - )); |
|
| 402 | + $this->getLogger()->debug("[{remote}] Routing request to {controller}:{action}", array( |
|
| 403 | + 'remote' => $request->getRemoteHost(), |
|
| 404 | + 'controller' => $controller, |
|
| 405 | + 'action' => $action |
|
| 406 | + )); |
|
| 407 | 407 | } |
| 408 | 408 | |
| 409 | 409 | $view = $this->getViewBestMatch($request, $applicationName); |
@@ -467,9 +467,9 @@ discard block |
||
| 467 | 467 | */ |
| 468 | 468 | public function registerRouter(AbstractRouter $router) |
| 469 | 469 | { |
| 470 | - $this->router = $router; |
|
| 471 | - $this->router->setApplication($this); |
|
| 472 | - return $this; |
|
| 470 | + $this->router = $router; |
|
| 471 | + $this->router->setApplication($this); |
|
| 472 | + return $this; |
|
| 473 | 473 | } |
| 474 | 474 | |
| 475 | 475 | /** |
@@ -51,12 +51,12 @@ discard block |
||
| 51 | 51 | * @return array Parsed parts for later usage |
| 52 | 52 | */ |
| 53 | 53 | private static function parseUri(Request &$request, |
| 54 | - $uri, $defaultController, $defaultAction) |
|
| 54 | + $uri, $defaultController, $defaultAction) |
|
| 55 | 55 | { |
| 56 | 56 | // All beyond the context prefix is our application request uri |
| 57 | 57 | $contextUri = $uri; |
| 58 | 58 | if (null != $request->getContextPrefix() && '/' != $request->getContextPrefix()) { |
| 59 | - $contextUri = str_replace($request->getContextPrefix(), '', $uri); |
|
| 59 | + $contextUri = str_replace($request->getContextPrefix(), '', $uri); |
|
| 60 | 60 | } |
| 61 | 61 | |
| 62 | 62 | // Split parts |
@@ -102,10 +102,10 @@ discard block |
||
| 102 | 102 | * The destination parameter name |
| 103 | 103 | */ |
| 104 | 104 | private static function parseElement(Request &$req, |
| 105 | - $serverVars, $elementName, $paramName) |
|
| 105 | + $serverVars, $elementName, $paramName) |
|
| 106 | 106 | { |
| 107 | 107 | if (isset($serverVars[$elementName])) { |
| 108 | - $req->setParam( $paramName, $serverVars[$elementName] ); |
|
| 108 | + $req->setParam( $paramName, $serverVars[$elementName] ); |
|
| 109 | 109 | } |
| 110 | 110 | } |
| 111 | 111 | |
@@ -181,7 +181,7 @@ discard block |
||
| 181 | 181 | */ |
| 182 | 182 | public function setController($controller) |
| 183 | 183 | { |
| 184 | - $this->controller = strval($controller); |
|
| 184 | + $this->controller = strval($controller); |
|
| 185 | 185 | } |
| 186 | 186 | |
| 187 | 187 | /** |
@@ -201,7 +201,7 @@ discard block |
||
| 201 | 201 | */ |
| 202 | 202 | public function setAction($action) |
| 203 | 203 | { |
| 204 | - $this->action = $action; |
|
| 204 | + $this->action = $action; |
|
| 205 | 205 | } |
| 206 | 206 | |
| 207 | 207 | /** |
@@ -231,7 +231,7 @@ discard block |
||
| 231 | 231 | */ |
| 232 | 232 | public function setContextPrefix($prefix) |
| 233 | 233 | { |
| 234 | - $this->contextPrefix = strval($prefix); |
|
| 234 | + $this->contextPrefix = strval($prefix); |
|
| 235 | 235 | } |
| 236 | 236 | |
| 237 | 237 | /** |
@@ -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, "?", true); |
|
| 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, "?", true); |
|
| 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 |