@@ -167,6 +167,9 @@ |
||
| 167 | 167 | |
| 168 | 168 | } |
| 169 | 169 | |
| 170 | + /** |
|
| 171 | + * @param string $name |
|
| 172 | + */ |
|
| 170 | 173 | private function emitServiceSpecializedEvents($name) { |
| 171 | 174 | |
| 172 | 175 | return new ServiceEvent( |
@@ -75,7 +75,7 @@ discard block |
||
| 75 | 75 | |
| 76 | 76 | $this->extra = new Extra($this->logger); |
| 77 | 77 | |
| 78 | - $this->configuration = new Configuration( DefaultConfiguration::get() ); |
|
| 78 | + $this->configuration = new Configuration(DefaultConfiguration::get()); |
|
| 79 | 79 | |
| 80 | 80 | $this->configuration->merge($configuration); |
| 81 | 81 | |
@@ -149,9 +149,9 @@ discard block |
||
| 149 | 149 | |
| 150 | 150 | $this->logger->debug("Emitting global dispatcher event."); |
| 151 | 151 | |
| 152 | - $this->events->emit( new DispatcherEvent($this) ); |
|
| 152 | + $this->events->emit(new DispatcherEvent($this)); |
|
| 153 | 153 | |
| 154 | - if ( $this->configuration()->get('enabled') === false ) { |
|
| 154 | + if ($this->configuration()->get('enabled') === false) { |
|
| 155 | 155 | |
| 156 | 156 | $this->logger->debug("Dispatcher disabled, shutting down gracefully."); |
| 157 | 157 | |
@@ -167,11 +167,11 @@ discard block |
||
| 167 | 167 | |
| 168 | 168 | } |
| 169 | 169 | |
| 170 | - $this->events->emit( $this->emitServiceSpecializedEvents('dispatcher.request') ); |
|
| 170 | + $this->events->emit($this->emitServiceSpecializedEvents('dispatcher.request')); |
|
| 171 | 171 | |
| 172 | - $this->events->emit( $this->emitServiceSpecializedEvents('dispatcher.request.'.$this->request->method()->get()) ); |
|
| 172 | + $this->events->emit($this->emitServiceSpecializedEvents('dispatcher.request.'.$this->request->method()->get())); |
|
| 173 | 173 | |
| 174 | - $this->events->emit( $this->emitServiceSpecializedEvents('dispatcher.request.#') ); |
|
| 174 | + $this->events->emit($this->emitServiceSpecializedEvents('dispatcher.request.#')); |
|
| 175 | 175 | |
| 176 | 176 | $this->logger->debug("Starting router."); |
| 177 | 177 | |
@@ -195,13 +195,13 @@ discard block |
||
| 195 | 195 | |
| 196 | 196 | $this->logger->debug("Route acquired, type $route_type directed to $route_service."); |
| 197 | 197 | |
| 198 | - $this->events->emit( $this->emitServiceSpecializedEvents('dispatcher.route') ); |
|
| 198 | + $this->events->emit($this->emitServiceSpecializedEvents('dispatcher.route')); |
|
| 199 | 199 | |
| 200 | - $this->events->emit( $this->emitServiceSpecializedEvents('dispatcher.route.'.$route_type) ); |
|
| 200 | + $this->events->emit($this->emitServiceSpecializedEvents('dispatcher.route.'.$route_type)); |
|
| 201 | 201 | |
| 202 | - $this->events->emit( $this->emitServiceSpecializedEvents('dispatcher.route.'.$route_service) ); |
|
| 202 | + $this->events->emit($this->emitServiceSpecializedEvents('dispatcher.route.'.$route_service)); |
|
| 203 | 203 | |
| 204 | - $this->events->emit( $this->emitServiceSpecializedEvents('dispatcher.route.#') ); |
|
| 204 | + $this->events->emit($this->emitServiceSpecializedEvents('dispatcher.route.#')); |
|
| 205 | 205 | |
| 206 | 206 | // translate route to service |
| 207 | 207 | |
@@ -256,11 +256,11 @@ discard block |
||
| 256 | 256 | |
| 257 | 257 | private function shutdown() { |
| 258 | 258 | |
| 259 | - $this->events->emit( $this->emitServiceSpecializedEvents('dispatcher.response') ); |
|
| 259 | + $this->events->emit($this->emitServiceSpecializedEvents('dispatcher.response')); |
|
| 260 | 260 | |
| 261 | - $this->events->emit( $this->emitServiceSpecializedEvents('dispatcher.response.'.$this->response->status()->get()) ); |
|
| 261 | + $this->events->emit($this->emitServiceSpecializedEvents('dispatcher.response.'.$this->response->status()->get())); |
|
| 262 | 262 | |
| 263 | - $this->events->emit( $this->emitServiceSpecializedEvents('dispatcher.response.#') ); |
|
| 263 | + $this->events->emit($this->emitServiceSpecializedEvents('dispatcher.response.#')); |
|
| 264 | 264 | |
| 265 | 265 | $this->logger->debug("Composing return value."); |
| 266 | 266 | |
@@ -209,6 +209,9 @@ |
||
| 209 | 209 | |
| 210 | 210 | } |
| 211 | 211 | |
| 212 | + /** |
|
| 213 | + * @param string[] $bits |
|
| 214 | + */ |
|
| 212 | 215 | private function evalUri($parameters, $bits) { |
| 213 | 216 | |
| 214 | 217 | $count = 0; |
@@ -160,10 +160,10 @@ discard block |
||
| 160 | 160 | |
| 161 | 161 | $methods = $this->configuration->get('allowed-http-methods'); |
| 162 | 162 | |
| 163 | - if ( ( $methods != null || !empty($methods) ) && in_array($method, $methods) === false ) { |
|
| 163 | + if (($methods != null || !empty($methods)) && in_array($method, $methods) === false) { |
|
| 164 | 164 | |
| 165 | 165 | throw new DispatcherException("Method not allowed", 0, null, 405, array( |
| 166 | - "Allow" => implode(",",$methods) |
|
| 166 | + "Allow" => implode(",", $methods) |
|
| 167 | 167 | )); |
| 168 | 168 | |
| 169 | 169 | } |
@@ -192,7 +192,7 @@ discard block |
||
| 192 | 192 | |
| 193 | 193 | $methods = $service->getImplementedMethods(); |
| 194 | 194 | |
| 195 | - if ( in_array($method, $metods) ) { |
|
| 195 | + if (in_array($method, $metods)) { |
|
| 196 | 196 | |
| 197 | 197 | $callable = $service->getMethod($method); |
| 198 | 198 | |
@@ -232,9 +232,9 @@ discard block |
||
| 232 | 232 | |
| 233 | 233 | $routes = Yaml::parse($yaml); |
| 234 | 234 | |
| 235 | - if ( !empty($routes) ) { |
|
| 235 | + if (!empty($routes)) { |
|
| 236 | 236 | |
| 237 | - foreach( $routes as $name => $route ) { |
|
| 237 | + foreach ($routes as $name => $route) { |
|
| 238 | 238 | |
| 239 | 239 | $this->add($route['route'], $route['type'], $route['class'], $route['parameters']); |
| 240 | 240 | |
@@ -252,7 +252,7 @@ discard block |
||
| 252 | 252 | |
| 253 | 253 | foreach ($this->table->routes() as $regex => $value) { |
| 254 | 254 | |
| 255 | - if (preg_match("/" . $regex . "/", $path, $matches)) { |
|
| 255 | + if (preg_match("/".$regex."/", $path, $matches)) { |
|
| 256 | 256 | |
| 257 | 257 | $this->evalUri($value['query'], $matches); |
| 258 | 258 | |
@@ -265,7 +265,7 @@ discard block |
||
| 265 | 265 | $this->classname = $value['class']; |
| 266 | 266 | $this->type = $value['type']; |
| 267 | 267 | $this->service = implode('.', $value['service']); |
| 268 | - $this->service = empty($this->service)?"default":$this->service; |
|
| 268 | + $this->service = empty($this->service) ? "default" : $this->service; |
|
| 269 | 269 | |
| 270 | 270 | return true; |
| 271 | 271 | |
@@ -279,13 +279,13 @@ discard block |
||
| 279 | 279 | |
| 280 | 280 | private function evalUri($parameters, $bits) { |
| 281 | 281 | |
| 282 | - $count = 0; |
|
| 282 | + $count = 0; |
|
| 283 | 283 | |
| 284 | 284 | foreach ($parameters as $key => $value) { |
| 285 | 285 | |
| 286 | 286 | if (isset($bits[$key])) { |
| 287 | 287 | |
| 288 | - if (preg_match('/^' . $value['regex'] . '$/', $bits[$key], $matches)) { |
|
| 288 | + if (preg_match('/^'.$value['regex'].'$/', $bits[$key], $matches)) { |
|
| 289 | 289 | |
| 290 | 290 | if (count($matches) == 1) $matches = $matches[0]; |
| 291 | 291 | |
@@ -113,8 +113,9 @@ discard block |
||
| 113 | 113 | $this->extra |
| 114 | 114 | ); |
| 115 | 115 | |
| 116 | + } else { |
|
| 117 | + return null; |
|
| 116 | 118 | } |
| 117 | - else return null; |
|
| 118 | 119 | |
| 119 | 120 | } |
| 120 | 121 | |
@@ -294,7 +295,10 @@ discard block |
||
| 294 | 295 | */ |
| 295 | 296 | if (preg_match('/^' . $value['regex'] . '$/', $bits[$key], $matches)) { |
| 296 | 297 | |
| 297 | - if (count($matches) == 1) $matches = $matches[0]; // This is the case where no backreferences are present or available. |
|
| 298 | + if (count($matches) == 1) { |
|
| 299 | + $matches = $matches[0]; |
|
| 300 | + } |
|
| 301 | + // This is the case where no backreferences are present or available. |
|
| 298 | 302 | |
| 299 | 303 | // The extracted value (with any backreference available) is added to the query parameters. |
| 300 | 304 | $this->request->query()->set($key, $matches); |
@@ -26,11 +26,11 @@ discard block |
||
| 26 | 26 | |
| 27 | 27 | protected $parameters = array(); |
| 28 | 28 | |
| 29 | - final public function get($parameter=null) { |
|
| 29 | + final public function get($parameter = null) { |
|
| 30 | 30 | |
| 31 | - if ( is_null($parameter) ) return $this->parameters; |
|
| 31 | + if (is_null($parameter)) return $this->parameters; |
|
| 32 | 32 | |
| 33 | - else if ( array_key_exists($parameter, $this->parameters) ) { |
|
| 33 | + else if (array_key_exists($parameter, $this->parameters)) { |
|
| 34 | 34 | |
| 35 | 35 | return $this->parameters[$parameter]; |
| 36 | 36 | |
@@ -50,13 +50,13 @@ discard block |
||
| 50 | 50 | |
| 51 | 51 | final public function unset($parameter = null) { |
| 52 | 52 | |
| 53 | - if ( is_null($parameter) ) { |
|
| 53 | + if (is_null($parameter)) { |
|
| 54 | 54 | |
| 55 | 55 | $this->parameters = array(); |
| 56 | 56 | |
| 57 | 57 | return true; |
| 58 | 58 | |
| 59 | - } else if ( array_key_exists($parameter, $this->parameters) ) { |
|
| 59 | + } else if (array_key_exists($parameter, $this->parameters)) { |
|
| 60 | 60 | |
| 61 | 61 | unset($this->parameters[$parameter]); |
| 62 | 62 | |
@@ -28,16 +28,16 @@ |
||
| 28 | 28 | |
| 29 | 29 | final public function get($parameter=null) { |
| 30 | 30 | |
| 31 | - if ( is_null($parameter) ) return $this->parameters; |
|
| 32 | - |
|
| 33 | - else if ( array_key_exists($parameter, $this->parameters) ) { |
|
| 31 | + if ( is_null($parameter) ) { |
|
| 32 | + return $this->parameters; |
|
| 33 | + } else if ( array_key_exists($parameter, $this->parameters) ) { |
|
| 34 | 34 | |
| 35 | 35 | return $this->parameters[$parameter]; |
| 36 | 36 | |
| 37 | + } else { |
|
| 38 | + return null; |
|
| 37 | 39 | } |
| 38 | 40 | |
| 39 | - else return null; |
|
| 40 | - |
|
| 41 | 41 | } |
| 42 | 42 | |
| 43 | 43 | final public function set($parameter, $value) { |
@@ -41,13 +41,13 @@ |
||
| 41 | 41 | */ |
| 42 | 42 | private static function getHeaders() { |
| 43 | 43 | |
| 44 | - if ( function_exists('getallheaders') ) return getallheaders(); |
|
| 44 | + if (function_exists('getallheaders')) return getallheaders(); |
|
| 45 | 45 | |
| 46 | 46 | $headers = array(); |
| 47 | 47 | |
| 48 | - foreach ( $_SERVER as $name => $value ) { |
|
| 48 | + foreach ($_SERVER as $name => $value) { |
|
| 49 | 49 | |
| 50 | - if ( substr($name, 0, 5) == 'HTTP_' ) { |
|
| 50 | + if (substr($name, 0, 5) == 'HTTP_') { |
|
| 51 | 51 | |
| 52 | 52 | $headers[str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($name, 5)))))] = $value; |
| 53 | 53 | |
@@ -41,7 +41,9 @@ |
||
| 41 | 41 | */ |
| 42 | 42 | private static function getHeaders() { |
| 43 | 43 | |
| 44 | - if ( function_exists('getallheaders') ) return getallheaders(); |
|
| 44 | + if ( function_exists('getallheaders') ) { |
|
| 45 | + return getallheaders(); |
|
| 46 | + } |
|
| 45 | 47 | |
| 46 | 48 | $headers = array(); |
| 47 | 49 | |
@@ -46,7 +46,7 @@ |
||
| 46 | 46 | |
| 47 | 47 | private static function getParameters() { |
| 48 | 48 | |
| 49 | - switch( $_SERVER['REQUEST_METHOD'] ) { |
|
| 49 | + switch ($_SERVER['REQUEST_METHOD']) { |
|
| 50 | 50 | |
| 51 | 51 | case 'POST': |
| 52 | 52 | |
@@ -46,7 +46,7 @@ |
||
| 46 | 46 | |
| 47 | 47 | private static function getParameters() { |
| 48 | 48 | |
| 49 | - switch( $_SERVER['REQUEST_METHOD'] ) { |
|
| 49 | + switch ($_SERVER['REQUEST_METHOD']) { |
|
| 50 | 50 | |
| 51 | 51 | case 'POST': |
| 52 | 52 | |
@@ -40,7 +40,7 @@ discard block |
||
| 40 | 40 | |
| 41 | 41 | public function set($content) { |
| 42 | 42 | |
| 43 | - if ( !is_scalar($content) ) { |
|
| 43 | + if (!is_scalar($content)) { |
|
| 44 | 44 | |
| 45 | 45 | throw new Exception("Invalid HTTP content"); |
| 46 | 46 | |
@@ -54,7 +54,7 @@ discard block |
||
| 54 | 54 | |
| 55 | 55 | public function type($type = null) { |
| 56 | 56 | |
| 57 | - if ( is_null($type) ) { |
|
| 57 | + if (is_null($type)) { |
|
| 58 | 58 | |
| 59 | 59 | return $this->type; |
| 60 | 60 | |
@@ -68,7 +68,7 @@ discard block |
||
| 68 | 68 | |
| 69 | 69 | public function charset($charset = null) { |
| 70 | 70 | |
| 71 | - if ( is_null($charset) ) { |
|
| 71 | + if (is_null($charset)) { |
|
| 72 | 72 | |
| 73 | 73 | return $this->charset; |
| 74 | 74 | |
@@ -85,7 +85,7 @@ discard block |
||
| 85 | 85 | |
| 86 | 86 | public function set($code) { |
| 87 | 87 | |
| 88 | - if ( !array_key_exists($code, $this->http_status_codes) ) { |
|
| 88 | + if (!array_key_exists($code, $this->http_status_codes)) { |
|
| 89 | 89 | |
| 90 | 90 | throw new Exception("Invalid HTTP Status Code"); |
| 91 | 91 | |
@@ -97,9 +97,9 @@ discard block |
||
| 97 | 97 | |
| 98 | 98 | } |
| 99 | 99 | |
| 100 | - public function description($code=null) { |
|
| 100 | + public function description($code = null) { |
|
| 101 | 101 | |
| 102 | - if ( is_null($code) || !array_key_exists($code, $this->http_status_codes) ) { |
|
| 102 | + if (is_null($code) || !array_key_exists($code, $this->http_status_codes)) { |
|
| 103 | 103 | |
| 104 | 104 | $message = $this->http_status_codes[$this->status_code]; |
| 105 | 105 | |
@@ -73,10 +73,11 @@ discard block |
||
| 73 | 73 | |
| 74 | 74 | $regex = $this->readpath($folders); |
| 75 | 75 | |
| 76 | - if (isset($this->routes[$regex])) |
|
| 77 | - return $this->routes[$regex]; |
|
| 78 | - else |
|
| 79 | - return null; |
|
| 76 | + if (isset($this->routes[$regex])) { |
|
| 77 | + return $this->routes[$regex]; |
|
| 78 | + } else { |
|
| 79 | + return null; |
|
| 80 | + } |
|
| 80 | 81 | |
| 81 | 82 | } |
| 82 | 83 | |
@@ -86,7 +87,9 @@ discard block |
||
| 86 | 87 | |
| 87 | 88 | $regex = $this->readpath($folders); |
| 88 | 89 | |
| 89 | - if (isset($this->routes[$regex])) unset($this->routes[$regex]); |
|
| 90 | + if (isset($this->routes[$regex])) { |
|
| 91 | + unset($this->routes[$regex]); |
|
| 92 | + } |
|
| 90 | 93 | |
| 91 | 94 | } |
| 92 | 95 | |
@@ -142,9 +142,9 @@ discard block |
||
| 142 | 142 | */ |
| 143 | 143 | foreach ($decoded as $key => $string) { |
| 144 | 144 | |
| 145 | - $this->logger->debug("PARAMETER KEY: " . $key); |
|
| 145 | + $this->logger->debug("PARAMETER KEY: ".$key); |
|
| 146 | 146 | |
| 147 | - $this->logger->debug("PARAMETER STRING: " . $string); |
|
| 147 | + $this->logger->debug("PARAMETER STRING: ".$string); |
|
| 148 | 148 | |
| 149 | 149 | /* The key and the regex of every paramater is passed to the 'readparam' |
| 150 | 150 | * method which will build an appropriate regular expression and will understand |
@@ -152,14 +152,14 @@ discard block |
||
| 152 | 152 | */ |
| 153 | 153 | $param_regex .= $this->readparam($key, $string, $param_required, $value); |
| 154 | 154 | |
| 155 | - $this->logger->debug("PARAMETER REGEX: " . $param_regex); |
|
| 155 | + $this->logger->debug("PARAMETER REGEX: ".$param_regex); |
|
| 156 | 156 | |
| 157 | 157 | } |
| 158 | 158 | // Once the parameter is analyzed, the result is passed to the next iteration |
| 159 | 159 | $this->readpath( |
| 160 | 160 | $folders, |
| 161 | 161 | $value, |
| 162 | - $regex.'(?:\/'.$param_regex.')'. (($param_required)?'{1}':'?') |
|
| 162 | + $regex.'(?:\/'.$param_regex.')'.(($param_required) ? '{1}' : '?') |
|
| 163 | 163 | ); |
| 164 | 164 | |
| 165 | 165 | } else { |
@@ -223,7 +223,7 @@ discard block |
||
| 223 | 223 | * If the field is required, the regular expression is completed with a '{1}' (which make it compulsory), |
| 224 | 224 | * otherwise a '?' is added. |
| 225 | 225 | */ |
| 226 | - return '(?P<' . $key . '>' . $string . ')' . (($field_required)?'{1}':'?'); |
|
| 226 | + return '(?P<'.$key.'>'.$string.')'.(($field_required) ? '{1}' : '?'); |
|
| 227 | 227 | |
| 228 | 228 | } |
| 229 | 229 | |
@@ -231,24 +231,24 @@ discard block |
||
| 231 | 231 | private function add($folders, $type, $class, $parameters) { |
| 232 | 232 | |
| 233 | 233 | // The values associated with a route are as follows: |
| 234 | - $value = array( |
|
| 235 | - "type" => $type, // Type of route |
|
| 236 | - "class" => $class, // Class to be invoked |
|
| 237 | - "service" => array(), // Service name (it can be a list of namespaces plus a final service name) |
|
| 234 | + $value = array( |
|
| 235 | + "type" => $type, // Type of route |
|
| 236 | + "class" => $class, // Class to be invoked |
|
| 237 | + "service" => array(), // Service name (it can be a list of namespaces plus a final service name) |
|
| 238 | 238 | "parameters" => $parameters, // Parameters passed via the composer.json configuration (cache, ttl, etc...) |
| 239 | 239 | "query" => array() // List of parameters with their regular expression that must be added among the query parameters |
| 240 | 240 | ); |
| 241 | 241 | |
| 242 | - $this->logger->debug("ROUTE: " . $route); |
|
| 242 | + $this->logger->debug("ROUTE: ".$route); |
|
| 243 | 243 | |
| 244 | - $this->logger->debug("PARAMETERS: " . var_export($value, true)); |
|
| 244 | + $this->logger->debug("PARAMETERS: ".var_export($value, true)); |
|
| 245 | 245 | |
| 246 | 246 | // This method generate a global regular expression which will be able to match all the URI supported by the route |
| 247 | 247 | $regex = $this->readpath($folders, $value); |
| 248 | 248 | |
| 249 | - $this->logger->debug("ROUTE: " . $regex); |
|
| 249 | + $this->logger->debug("ROUTE: ".$regex); |
|
| 250 | 250 | |
| 251 | - $this->logger->debug("PARAMETERS: " . var_export($value, true)); |
|
| 251 | + $this->logger->debug("PARAMETERS: ".var_export($value, true)); |
|
| 252 | 252 | |
| 253 | 253 | $this->routes[$regex] = $value; |
| 254 | 254 | |