@@ -141,8 +141,9 @@ discard block |
||
| 141 | 141 | */ |
| 142 | 142 | public function __construct(Array $routes = []) |
| 143 | 143 | { |
| 144 | - if (count($routes)) |
|
| 145 | - $this->routes = $routes; |
|
| 144 | + if (count($routes)) { |
|
| 145 | + $this->routes = $routes; |
|
| 146 | + } |
|
| 146 | 147 | |
| 147 | 148 | $this->zendRouter = new \Zend\Router\SimpleRouteStack(); |
| 148 | 149 | } |
@@ -165,8 +166,9 @@ discard block |
||
| 165 | 166 | $module = (is_null($this->identifiers["module"]) || empty($this->identifiers["module"])) |
| 166 | 167 | ? $this->routes["defaults"]["module"] : $this->identifiers["module"]; |
| 167 | 168 | |
| 168 | - if (!array_key_exists($module, $this->routes)) |
|
| 169 | - throw new Exception\ModuleNotFoundException("The key '$module' does not exists in routes!"); |
|
| 169 | + if (!array_key_exists($module, $this->routes)) { |
|
| 170 | + throw new Exception\ModuleNotFoundException("The key '$module' does not exists in routes!"); |
|
| 171 | + } |
|
| 170 | 172 | |
| 171 | 173 | $controller = (is_null($this->identifiers["controller"]) || empty($this->identifiers["controller"])) |
| 172 | 174 | ? $this->routes[$module]["controller"] : $this->identifiers["controller"]; |
@@ -176,10 +178,11 @@ discard block |
||
| 176 | 178 | |
| 177 | 179 | $fqn_controller = '\\' . $module . "\Controller\\" . $controller; |
| 178 | 180 | |
| 179 | - if (class_exists($fqn_controller)) |
|
| 180 | - $this->controller = new $fqn_controller($module, $view, $this->basePath); |
|
| 181 | - else |
|
| 182 | - throw new Exception\ControllerNotFoundException("The control class '$fqn_controller' does not exists!"); |
|
| 181 | + if (class_exists($fqn_controller)) { |
|
| 182 | + $this->controller = new $fqn_controller($module, $view, $this->basePath); |
|
| 183 | + } else { |
|
| 184 | + throw new Exception\ControllerNotFoundException("The control class '$fqn_controller' does not exists!"); |
|
| 185 | + } |
|
| 183 | 186 | } |
| 184 | 187 | |
| 185 | 188 | /** |
@@ -196,8 +199,9 @@ discard block |
||
| 196 | 199 | $key = array_keys($route); |
| 197 | 200 | $key = array_shift($key); |
| 198 | 201 | |
| 199 | - if (array_key_exists($key, $this->routes)) |
|
| 200 | - throw new \LogicException("The key '$key' was already defined as route"); |
|
| 202 | + if (array_key_exists($key, $this->routes)) { |
|
| 203 | + throw new \LogicException("The key '$key' was already defined as route"); |
|
| 204 | + } |
|
| 201 | 205 | |
| 202 | 206 | $this->routes = array_merge($this->routes, $route); |
| 203 | 207 | } |
@@ -162,8 +162,8 @@ discard block |
||
| 162 | 162 | * This instruction includes each module declared. |
| 163 | 163 | * Each module has an autoloader to load its classes (controllers and models) |
| 164 | 164 | */ |
| 165 | - if (file_exists($this->modulePath ."/". $module."/Module.php")) |
|
| 166 | - include($this->modulePath ."/". $module."/Module.php"); |
|
| 165 | + if (file_exists($this->modulePath . "/" . $module . "/Module.php")) |
|
| 166 | + include($this->modulePath . "/" . $module . "/Module.php"); |
|
| 167 | 167 | |
| 168 | 168 | spl_autoload_register($module . "\Module::loader"); |
| 169 | 169 | } |
@@ -179,17 +179,17 @@ discard block |
||
| 179 | 179 | */ |
| 180 | 180 | public function run() |
| 181 | 181 | { |
| 182 | - $module = isset($_GET["module"]) ? $_GET["module"] : null; |
|
| 182 | + $module = isset($_GET["module"]) ? $_GET["module"] : null; |
|
| 183 | 183 | $controller = isset($_GET["controller"]) ? $_GET["controller"] : null; |
| 184 | - $view = isset($_GET["view"]) ? $_GET["view"] : null; |
|
| 184 | + $view = isset($_GET["view"]) ? $_GET["view"] : null; |
|
| 185 | 185 | |
| 186 | 186 | $request = new \Zend\Http\Request(); |
| 187 | 187 | |
| 188 | 188 | # build URI |
| 189 | 189 | $uri = ''; |
| 190 | - $uri .= !empty($module) ? '/' . $module : ""; |
|
| 190 | + $uri .= !empty($module) ? '/' . $module : ""; |
|
| 191 | 191 | $uri .= !empty($controller) ? '/' . $controller : ""; |
| 192 | - $uri .= !empty($view) ? '/' . $view : ""; |
|
| 192 | + $uri .= !empty($view) ? '/' . $view : ""; |
|
| 193 | 193 | |
| 194 | 194 | if (empty($uri)) |
| 195 | 195 | $uri = "/"; |
@@ -68,19 +68,23 @@ discard block |
||
| 68 | 68 | public function __construct(Array $init_parameters) |
| 69 | 69 | { |
| 70 | 70 | # start sessions |
| 71 | - if (!isset($_SESSION)) |
|
| 72 | - session_start(); |
|
| 71 | + if (!isset($_SESSION)) { |
|
| 72 | + session_start(); |
|
| 73 | + } |
|
| 73 | 74 | |
| 74 | - if (!array_key_exists('environment', $init_parameters)) |
|
| 75 | - throw new \InvalidArgumentException("The 'environment' key was not defined"); |
|
| 75 | + if (!array_key_exists('environment', $init_parameters)) { |
|
| 76 | + throw new \InvalidArgumentException("The 'environment' key was not defined"); |
|
| 77 | + } |
|
| 76 | 78 | |
| 77 | - if (!array_key_exists('dev_mode', $init_parameters['environment'])) |
|
| 78 | - throw new \InvalidArgumentException("The 'dev_mode' key was not defined"); |
|
| 79 | + if (!array_key_exists('dev_mode', $init_parameters['environment'])) { |
|
| 80 | + throw new \InvalidArgumentException("The 'dev_mode' key was not defined"); |
|
| 81 | + } |
|
| 79 | 82 | |
| 80 | 83 | $this->devMode = $init_parameters["environment"]["dev_mode"]; |
| 81 | 84 | |
| 82 | - if (!array_key_exists('modules', $init_parameters)) |
|
| 83 | - throw new \InvalidArgumentException("The 'modules' key was not defined"); |
|
| 85 | + if (!array_key_exists('modules', $init_parameters)) { |
|
| 86 | + throw new \InvalidArgumentException("The 'modules' key was not defined"); |
|
| 87 | + } |
|
| 84 | 88 | |
| 85 | 89 | $this->modules = $init_parameters["modules"]; |
| 86 | 90 | |
@@ -94,8 +98,7 @@ discard block |
||
| 94 | 98 | { |
| 95 | 99 | ini_set('display_errors', 1); |
| 96 | 100 | error_reporting(-1); |
| 97 | - } |
|
| 98 | - else |
|
| 101 | + } else |
|
| 99 | 102 | { |
| 100 | 103 | ini_set('display_errors', 0); |
| 101 | 104 | error_reporting(0); |
@@ -103,26 +106,30 @@ discard block |
||
| 103 | 106 | |
| 104 | 107 | $this->loadModules($this->modules); |
| 105 | 108 | |
| 106 | - if (!array_key_exists('router', $init_parameters)) |
|
| 107 | - throw new \InvalidArgumentException("The 'router' key was not defined"); |
|
| 109 | + if (!array_key_exists('router', $init_parameters)) { |
|
| 110 | + throw new \InvalidArgumentException("The 'router' key was not defined"); |
|
| 111 | + } |
|
| 108 | 112 | |
| 109 | - if (!array_key_exists('routes', $init_parameters["router"])) |
|
| 110 | - throw new \InvalidArgumentException("The 'routes' key was not defined"); |
|
| 113 | + if (!array_key_exists('routes', $init_parameters["router"])) { |
|
| 114 | + throw new \InvalidArgumentException("The 'routes' key was not defined"); |
|
| 115 | + } |
|
| 111 | 116 | |
| 112 | 117 | $this->router = new Router($init_parameters["router"]["routes"]); |
| 113 | 118 | |
| 114 | - if (!array_key_exists('base_path', $init_parameters['environment'])) |
|
| 115 | - throw new \InvalidArgumentException("The 'base_path' key was not defined"); |
|
| 119 | + if (!array_key_exists('base_path', $init_parameters['environment'])) { |
|
| 120 | + throw new \InvalidArgumentException("The 'base_path' key was not defined"); |
|
| 121 | + } |
|
| 116 | 122 | |
| 117 | 123 | $this->router->setBasePath($init_parameters["environment"]["base_path"]); |
| 118 | 124 | |
| 119 | 125 | # load routes from init_parameters |
| 120 | 126 | foreach ($init_parameters["router"]["routes"] as $name => $route) |
| 121 | 127 | { |
| 122 | - if ($route instanceof \Zend\Router\Http\RouteInterface) |
|
| 123 | - $this->getRouter()->addZendRoute($name, $route); |
|
| 124 | - else |
|
| 125 | - $this->getRouter()->addRoute($route); |
|
| 128 | + if ($route instanceof \Zend\Router\Http\RouteInterface) { |
|
| 129 | + $this->getRouter()->addZendRoute($name, $route); |
|
| 130 | + } else { |
|
| 131 | + $this->getRouter()->addRoute($route); |
|
| 132 | + } |
|
| 126 | 133 | } |
| 127 | 134 | |
| 128 | 135 | # load routes from each module |
@@ -132,11 +139,13 @@ discard block |
||
| 132 | 139 | { |
| 133 | 140 | $module_config_file = require($this->modulePath . "/$module/config/module.config.php"); |
| 134 | 141 | |
| 135 | - if (!array_key_exists('router', $module_config_file)) |
|
| 136 | - throw new \RuntimeException("The 'router' key was not defined in the config file for module '$module'"); |
|
| 142 | + if (!array_key_exists('router', $module_config_file)) { |
|
| 143 | + throw new \RuntimeException("The 'router' key was not defined in the config file for module '$module'"); |
|
| 144 | + } |
|
| 137 | 145 | |
| 138 | - if (!array_key_exists('routes', $module_config_file["router"])) |
|
| 139 | - throw new \RuntimeException("The 'routes' key was not defined in the config file for module '$module'"); |
|
| 146 | + if (!array_key_exists('routes', $module_config_file["router"])) { |
|
| 147 | + throw new \RuntimeException("The 'routes' key was not defined in the config file for module '$module'"); |
|
| 148 | + } |
|
| 140 | 149 | |
| 141 | 150 | $this->getRouter()->addRoute($module_config_file["router"]["routes"]); |
| 142 | 151 | } |
@@ -162,14 +171,15 @@ discard block |
||
| 162 | 171 | * This instruction includes each module declared. |
| 163 | 172 | * Each module has an autoloader to load its classes (controllers and models) |
| 164 | 173 | */ |
| 165 | - if (file_exists($this->modulePath ."/". $module."/Module.php")) |
|
| 166 | - include($this->modulePath ."/". $module."/Module.php"); |
|
| 174 | + if (file_exists($this->modulePath ."/". $module."/Module.php")) { |
|
| 175 | + include($this->modulePath ."/". $module."/Module.php"); |
|
| 176 | + } |
|
| 167 | 177 | |
| 168 | 178 | spl_autoload_register($module . "\Module::loader"); |
| 169 | 179 | } |
| 180 | + } else { |
|
| 181 | + throw new \RuntimeException("The application must have at least one module"); |
|
| 170 | 182 | } |
| 171 | - else |
|
| 172 | - throw new \RuntimeException("The application must have at least one module"); |
|
| 173 | 183 | } |
| 174 | 184 | |
| 175 | 185 | /** |
@@ -191,8 +201,9 @@ discard block |
||
| 191 | 201 | $uri .= !empty($controller) ? '/' . $controller : ""; |
| 192 | 202 | $uri .= !empty($view) ? '/' . $view : ""; |
| 193 | 203 | |
| 194 | - if (empty($uri)) |
|
| 195 | - $uri = "/"; |
|
| 204 | + if (empty($uri)) { |
|
| 205 | + $uri = "/"; |
|
| 206 | + } |
|
| 196 | 207 | |
| 197 | 208 | $request->setUri($uri); |
| 198 | 209 | |
@@ -208,9 +219,9 @@ discard block |
||
| 208 | 219 | $view = $params["action"]; |
| 209 | 220 | |
| 210 | 221 | $this->router->setIdentifiers($module, $controller, $view); |
| 222 | + } else { |
|
| 223 | + $this->router->setIdentifiers($module, $controller, $view); |
|
| 211 | 224 | } |
| 212 | - else |
|
| 213 | - $this->router->setIdentifiers($module, $controller, $view); |
|
| 214 | 225 | |
| 215 | 226 | $this->router->run(); |
| 216 | 227 | } |