@@ -45,149 +45,149 @@ |
||
| 45 | 45 | */ |
| 46 | 46 | class Dispatcher { |
| 47 | 47 | |
| 48 | - /** @var MiddlewareDispatcher */ |
|
| 49 | - private $middlewareDispatcher; |
|
| 50 | - |
|
| 51 | - /** @var Http */ |
|
| 52 | - private $protocol; |
|
| 53 | - |
|
| 54 | - /** @var ControllerMethodReflector */ |
|
| 55 | - private $reflector; |
|
| 56 | - |
|
| 57 | - /** @var IRequest */ |
|
| 58 | - private $request; |
|
| 59 | - |
|
| 60 | - /** |
|
| 61 | - * @param Http $protocol the http protocol with contains all status headers |
|
| 62 | - * @param MiddlewareDispatcher $middlewareDispatcher the dispatcher which |
|
| 63 | - * runs the middleware |
|
| 64 | - * @param ControllerMethodReflector $reflector the reflector that is used to inject |
|
| 65 | - * the arguments for the controller |
|
| 66 | - * @param IRequest $request the incoming request |
|
| 67 | - */ |
|
| 68 | - public function __construct(Http $protocol, |
|
| 69 | - MiddlewareDispatcher $middlewareDispatcher, |
|
| 70 | - ControllerMethodReflector $reflector, |
|
| 71 | - IRequest $request) { |
|
| 72 | - $this->protocol = $protocol; |
|
| 73 | - $this->middlewareDispatcher = $middlewareDispatcher; |
|
| 74 | - $this->reflector = $reflector; |
|
| 75 | - $this->request = $request; |
|
| 76 | - } |
|
| 77 | - |
|
| 78 | - |
|
| 79 | - /** |
|
| 80 | - * Handles a request and calls the dispatcher on the controller |
|
| 81 | - * @param Controller $controller the controller which will be called |
|
| 82 | - * @param string $methodName the method name which will be called on |
|
| 83 | - * the controller |
|
| 84 | - * @return array $array[0] contains a string with the http main header, |
|
| 85 | - * $array[1] contains headers in the form: $key => value, $array[2] contains |
|
| 86 | - * the response output |
|
| 87 | - * @throws \Exception |
|
| 88 | - */ |
|
| 89 | - public function dispatch(Controller $controller, string $methodName): array { |
|
| 90 | - $out = [null, [], null]; |
|
| 91 | - |
|
| 92 | - try { |
|
| 93 | - // prefill reflector with everything thats needed for the |
|
| 94 | - // middlewares |
|
| 95 | - $this->reflector->reflect($controller, $methodName); |
|
| 96 | - |
|
| 97 | - $this->middlewareDispatcher->beforeController($controller, |
|
| 98 | - $methodName); |
|
| 99 | - $response = $this->executeController($controller, $methodName); |
|
| 100 | - |
|
| 101 | - // if an exception appears, the middleware checks if it can handle the |
|
| 102 | - // exception and creates a response. If no response is created, it is |
|
| 103 | - // assumed that theres no middleware who can handle it and the error is |
|
| 104 | - // thrown again |
|
| 105 | - } catch(\Exception $exception){ |
|
| 106 | - $response = $this->middlewareDispatcher->afterException( |
|
| 107 | - $controller, $methodName, $exception); |
|
| 108 | - if ($response === null) { |
|
| 109 | - throw $exception; |
|
| 110 | - } |
|
| 111 | - } |
|
| 112 | - |
|
| 113 | - $response = $this->middlewareDispatcher->afterController( |
|
| 114 | - $controller, $methodName, $response); |
|
| 115 | - |
|
| 116 | - // depending on the cache object the headers need to be changed |
|
| 117 | - $out[0] = $this->protocol->getStatusHeader($response->getStatus(), |
|
| 118 | - $response->getLastModified(), $response->getETag()); |
|
| 119 | - $out[1] = array_merge($response->getHeaders()); |
|
| 120 | - $out[2] = $response->getCookies(); |
|
| 121 | - $out[3] = $this->middlewareDispatcher->beforeOutput( |
|
| 122 | - $controller, $methodName, $response->render() |
|
| 123 | - ); |
|
| 124 | - $out[4] = $response; |
|
| 125 | - |
|
| 126 | - return $out; |
|
| 127 | - } |
|
| 128 | - |
|
| 129 | - |
|
| 130 | - /** |
|
| 131 | - * Uses the reflected parameters, types and request parameters to execute |
|
| 132 | - * the controller |
|
| 133 | - * @param Controller $controller the controller to be executed |
|
| 134 | - * @param string $methodName the method on the controller that should be executed |
|
| 135 | - * @return Response |
|
| 136 | - */ |
|
| 137 | - private function executeController(Controller $controller, string $methodName): Response { |
|
| 138 | - $arguments = []; |
|
| 139 | - |
|
| 140 | - // valid types that will be casted |
|
| 141 | - $types = ['int', 'integer', 'bool', 'boolean', 'float']; |
|
| 142 | - |
|
| 143 | - foreach($this->reflector->getParameters() as $param => $default) { |
|
| 144 | - |
|
| 145 | - // try to get the parameter from the request object and cast |
|
| 146 | - // it to the type annotated in the @param annotation |
|
| 147 | - $value = $this->request->getParam($param, $default); |
|
| 148 | - $type = $this->reflector->getType($param); |
|
| 149 | - |
|
| 150 | - // if this is submitted using GET or a POST form, 'false' should be |
|
| 151 | - // converted to false |
|
| 152 | - if(($type === 'bool' || $type === 'boolean') && |
|
| 153 | - $value === 'false' && |
|
| 154 | - ( |
|
| 155 | - $this->request->method === 'GET' || |
|
| 156 | - strpos($this->request->getHeader('Content-Type'), |
|
| 157 | - 'application/x-www-form-urlencoded') !== false |
|
| 158 | - ) |
|
| 159 | - ) { |
|
| 160 | - $value = false; |
|
| 161 | - |
|
| 162 | - } elseif($value !== null && \in_array($type, $types, true)) { |
|
| 163 | - settype($value, $type); |
|
| 164 | - } |
|
| 165 | - |
|
| 166 | - $arguments[] = $value; |
|
| 167 | - } |
|
| 168 | - |
|
| 169 | - $response = \call_user_func_array([$controller, $methodName], $arguments); |
|
| 170 | - |
|
| 171 | - // format response |
|
| 172 | - if($response instanceof DataResponse || !($response instanceof Response)) { |
|
| 173 | - |
|
| 174 | - // get format from the url format or request format parameter |
|
| 175 | - $format = $this->request->getParam('format'); |
|
| 176 | - |
|
| 177 | - // if none is given try the first Accept header |
|
| 178 | - if($format === null) { |
|
| 179 | - $headers = $this->request->getHeader('Accept'); |
|
| 180 | - $format = $controller->getResponderByHTTPHeader($headers, null); |
|
| 181 | - } |
|
| 182 | - |
|
| 183 | - if ($format !== null) { |
|
| 184 | - $response = $controller->buildResponse($response, $format); |
|
| 185 | - } else { |
|
| 186 | - $response = $controller->buildResponse($response); |
|
| 187 | - } |
|
| 188 | - } |
|
| 189 | - |
|
| 190 | - return $response; |
|
| 191 | - } |
|
| 48 | + /** @var MiddlewareDispatcher */ |
|
| 49 | + private $middlewareDispatcher; |
|
| 50 | + |
|
| 51 | + /** @var Http */ |
|
| 52 | + private $protocol; |
|
| 53 | + |
|
| 54 | + /** @var ControllerMethodReflector */ |
|
| 55 | + private $reflector; |
|
| 56 | + |
|
| 57 | + /** @var IRequest */ |
|
| 58 | + private $request; |
|
| 59 | + |
|
| 60 | + /** |
|
| 61 | + * @param Http $protocol the http protocol with contains all status headers |
|
| 62 | + * @param MiddlewareDispatcher $middlewareDispatcher the dispatcher which |
|
| 63 | + * runs the middleware |
|
| 64 | + * @param ControllerMethodReflector $reflector the reflector that is used to inject |
|
| 65 | + * the arguments for the controller |
|
| 66 | + * @param IRequest $request the incoming request |
|
| 67 | + */ |
|
| 68 | + public function __construct(Http $protocol, |
|
| 69 | + MiddlewareDispatcher $middlewareDispatcher, |
|
| 70 | + ControllerMethodReflector $reflector, |
|
| 71 | + IRequest $request) { |
|
| 72 | + $this->protocol = $protocol; |
|
| 73 | + $this->middlewareDispatcher = $middlewareDispatcher; |
|
| 74 | + $this->reflector = $reflector; |
|
| 75 | + $this->request = $request; |
|
| 76 | + } |
|
| 77 | + |
|
| 78 | + |
|
| 79 | + /** |
|
| 80 | + * Handles a request and calls the dispatcher on the controller |
|
| 81 | + * @param Controller $controller the controller which will be called |
|
| 82 | + * @param string $methodName the method name which will be called on |
|
| 83 | + * the controller |
|
| 84 | + * @return array $array[0] contains a string with the http main header, |
|
| 85 | + * $array[1] contains headers in the form: $key => value, $array[2] contains |
|
| 86 | + * the response output |
|
| 87 | + * @throws \Exception |
|
| 88 | + */ |
|
| 89 | + public function dispatch(Controller $controller, string $methodName): array { |
|
| 90 | + $out = [null, [], null]; |
|
| 91 | + |
|
| 92 | + try { |
|
| 93 | + // prefill reflector with everything thats needed for the |
|
| 94 | + // middlewares |
|
| 95 | + $this->reflector->reflect($controller, $methodName); |
|
| 96 | + |
|
| 97 | + $this->middlewareDispatcher->beforeController($controller, |
|
| 98 | + $methodName); |
|
| 99 | + $response = $this->executeController($controller, $methodName); |
|
| 100 | + |
|
| 101 | + // if an exception appears, the middleware checks if it can handle the |
|
| 102 | + // exception and creates a response. If no response is created, it is |
|
| 103 | + // assumed that theres no middleware who can handle it and the error is |
|
| 104 | + // thrown again |
|
| 105 | + } catch(\Exception $exception){ |
|
| 106 | + $response = $this->middlewareDispatcher->afterException( |
|
| 107 | + $controller, $methodName, $exception); |
|
| 108 | + if ($response === null) { |
|
| 109 | + throw $exception; |
|
| 110 | + } |
|
| 111 | + } |
|
| 112 | + |
|
| 113 | + $response = $this->middlewareDispatcher->afterController( |
|
| 114 | + $controller, $methodName, $response); |
|
| 115 | + |
|
| 116 | + // depending on the cache object the headers need to be changed |
|
| 117 | + $out[0] = $this->protocol->getStatusHeader($response->getStatus(), |
|
| 118 | + $response->getLastModified(), $response->getETag()); |
|
| 119 | + $out[1] = array_merge($response->getHeaders()); |
|
| 120 | + $out[2] = $response->getCookies(); |
|
| 121 | + $out[3] = $this->middlewareDispatcher->beforeOutput( |
|
| 122 | + $controller, $methodName, $response->render() |
|
| 123 | + ); |
|
| 124 | + $out[4] = $response; |
|
| 125 | + |
|
| 126 | + return $out; |
|
| 127 | + } |
|
| 128 | + |
|
| 129 | + |
|
| 130 | + /** |
|
| 131 | + * Uses the reflected parameters, types and request parameters to execute |
|
| 132 | + * the controller |
|
| 133 | + * @param Controller $controller the controller to be executed |
|
| 134 | + * @param string $methodName the method on the controller that should be executed |
|
| 135 | + * @return Response |
|
| 136 | + */ |
|
| 137 | + private function executeController(Controller $controller, string $methodName): Response { |
|
| 138 | + $arguments = []; |
|
| 139 | + |
|
| 140 | + // valid types that will be casted |
|
| 141 | + $types = ['int', 'integer', 'bool', 'boolean', 'float']; |
|
| 142 | + |
|
| 143 | + foreach($this->reflector->getParameters() as $param => $default) { |
|
| 144 | + |
|
| 145 | + // try to get the parameter from the request object and cast |
|
| 146 | + // it to the type annotated in the @param annotation |
|
| 147 | + $value = $this->request->getParam($param, $default); |
|
| 148 | + $type = $this->reflector->getType($param); |
|
| 149 | + |
|
| 150 | + // if this is submitted using GET or a POST form, 'false' should be |
|
| 151 | + // converted to false |
|
| 152 | + if(($type === 'bool' || $type === 'boolean') && |
|
| 153 | + $value === 'false' && |
|
| 154 | + ( |
|
| 155 | + $this->request->method === 'GET' || |
|
| 156 | + strpos($this->request->getHeader('Content-Type'), |
|
| 157 | + 'application/x-www-form-urlencoded') !== false |
|
| 158 | + ) |
|
| 159 | + ) { |
|
| 160 | + $value = false; |
|
| 161 | + |
|
| 162 | + } elseif($value !== null && \in_array($type, $types, true)) { |
|
| 163 | + settype($value, $type); |
|
| 164 | + } |
|
| 165 | + |
|
| 166 | + $arguments[] = $value; |
|
| 167 | + } |
|
| 168 | + |
|
| 169 | + $response = \call_user_func_array([$controller, $methodName], $arguments); |
|
| 170 | + |
|
| 171 | + // format response |
|
| 172 | + if($response instanceof DataResponse || !($response instanceof Response)) { |
|
| 173 | + |
|
| 174 | + // get format from the url format or request format parameter |
|
| 175 | + $format = $this->request->getParam('format'); |
|
| 176 | + |
|
| 177 | + // if none is given try the first Accept header |
|
| 178 | + if($format === null) { |
|
| 179 | + $headers = $this->request->getHeader('Accept'); |
|
| 180 | + $format = $controller->getResponderByHTTPHeader($headers, null); |
|
| 181 | + } |
|
| 182 | + |
|
| 183 | + if ($format !== null) { |
|
| 184 | + $response = $controller->buildResponse($response, $format); |
|
| 185 | + } else { |
|
| 186 | + $response = $controller->buildResponse($response); |
|
| 187 | + } |
|
| 188 | + } |
|
| 189 | + |
|
| 190 | + return $response; |
|
| 191 | + } |
|
| 192 | 192 | |
| 193 | 193 | } |
@@ -1,5 +1,5 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | -declare(strict_types=1); |
|
| 2 | +declare(strict_types = 1); |
|
| 3 | 3 | /** |
| 4 | 4 | * @copyright Copyright (c) 2016, ownCloud, Inc. |
| 5 | 5 | * |
@@ -102,7 +102,7 @@ discard block |
||
| 102 | 102 | // exception and creates a response. If no response is created, it is |
| 103 | 103 | // assumed that theres no middleware who can handle it and the error is |
| 104 | 104 | // thrown again |
| 105 | - } catch(\Exception $exception){ |
|
| 105 | + } catch (\Exception $exception) { |
|
| 106 | 106 | $response = $this->middlewareDispatcher->afterException( |
| 107 | 107 | $controller, $methodName, $exception); |
| 108 | 108 | if ($response === null) { |
@@ -140,7 +140,7 @@ discard block |
||
| 140 | 140 | // valid types that will be casted |
| 141 | 141 | $types = ['int', 'integer', 'bool', 'boolean', 'float']; |
| 142 | 142 | |
| 143 | - foreach($this->reflector->getParameters() as $param => $default) { |
|
| 143 | + foreach ($this->reflector->getParameters() as $param => $default) { |
|
| 144 | 144 | |
| 145 | 145 | // try to get the parameter from the request object and cast |
| 146 | 146 | // it to the type annotated in the @param annotation |
@@ -149,7 +149,7 @@ discard block |
||
| 149 | 149 | |
| 150 | 150 | // if this is submitted using GET or a POST form, 'false' should be |
| 151 | 151 | // converted to false |
| 152 | - if(($type === 'bool' || $type === 'boolean') && |
|
| 152 | + if (($type === 'bool' || $type === 'boolean') && |
|
| 153 | 153 | $value === 'false' && |
| 154 | 154 | ( |
| 155 | 155 | $this->request->method === 'GET' || |
@@ -159,7 +159,7 @@ discard block |
||
| 159 | 159 | ) { |
| 160 | 160 | $value = false; |
| 161 | 161 | |
| 162 | - } elseif($value !== null && \in_array($type, $types, true)) { |
|
| 162 | + } elseif ($value !== null && \in_array($type, $types, true)) { |
|
| 163 | 163 | settype($value, $type); |
| 164 | 164 | } |
| 165 | 165 | |
@@ -169,13 +169,13 @@ discard block |
||
| 169 | 169 | $response = \call_user_func_array([$controller, $methodName], $arguments); |
| 170 | 170 | |
| 171 | 171 | // format response |
| 172 | - if($response instanceof DataResponse || !($response instanceof Response)) { |
|
| 172 | + if ($response instanceof DataResponse || !($response instanceof Response)) { |
|
| 173 | 173 | |
| 174 | 174 | // get format from the url format or request format parameter |
| 175 | 175 | $format = $this->request->getParam('format'); |
| 176 | 176 | |
| 177 | 177 | // if none is given try the first Accept header |
| 178 | - if($format === null) { |
|
| 178 | + if ($format === null) { |
|
| 179 | 179 | $headers = $this->request->getHeader('Accept'); |
| 180 | 180 | $format = $controller->getResponderByHTTPHeader($headers, null); |
| 181 | 181 | } |