@@ -55,8 +55,7 @@ discard block |
||
| 55 | 55 | * @class RouteHelper |
| 56 | 56 | * @package Platine\Framework\Http |
| 57 | 57 | */ |
| 58 | -class RouteHelper |
|
| 59 | -{
|
|
| 58 | +class RouteHelper { |
|
| 60 | 59 | /** |
| 61 | 60 | * The router instance |
| 62 | 61 | * @var Router |
@@ -67,8 +66,7 @@ discard block |
||
| 67 | 66 | * Create new instance |
| 68 | 67 | * @param Router $router |
| 69 | 68 | */ |
| 70 | - public function __construct(Router $router) |
|
| 71 | - {
|
|
| 69 | + public function __construct(Router $router) { |
|
| 72 | 70 | $this->router = $router; |
| 73 | 71 | } |
| 74 | 72 | |
@@ -59,8 +59,7 @@ discard block |
||
| 59 | 59 | * @class RouteDispatcherMiddleware |
| 60 | 60 | * @package Platine\Framework\Http\Middleware |
| 61 | 61 | */ |
| 62 | -class RouteDispatcherMiddleware implements MiddlewareInterface |
|
| 63 | -{
|
|
| 62 | +class RouteDispatcherMiddleware implements MiddlewareInterface { |
|
| 64 | 63 | /** |
| 65 | 64 | * The Middleware resolver instance |
| 66 | 65 | * @var MiddlewareResolverInterface |
@@ -71,8 +70,7 @@ discard block |
||
| 71 | 70 | * Create new instance |
| 72 | 71 | * @param MiddlewareResolverInterface $resolver |
| 73 | 72 | */ |
| 74 | - public function __construct(MiddlewareResolverInterface $resolver) |
|
| 75 | - {
|
|
| 73 | + public function __construct(MiddlewareResolverInterface $resolver) { |
|
| 76 | 74 | $this->resolver = $resolver; |
| 77 | 75 | } |
| 78 | 76 | |
@@ -83,11 +81,11 @@ discard block |
||
| 83 | 81 | ServerRequestInterface $request, |
| 84 | 82 | RequestHandlerInterface $handler |
| 85 | 83 | ): ResponseInterface {
|
| 86 | - if (!$result = $request->getAttribute(Route::class)) {
|
|
| 84 | + if (!$result = $request->getAttribute(Route::class)) { |
|
| 87 | 85 | return $handler->handle($request); |
| 88 | 86 | } |
| 89 | 87 | |
| 90 | - if ($result instanceof Route) {
|
|
| 88 | + if ($result instanceof Route) { |
|
| 91 | 89 | $result = $result->getHandler(); |
| 92 | 90 | } |
| 93 | 91 | |
@@ -174,14 +174,14 @@ |
||
| 174 | 174 | {
|
| 175 | 175 | if ($this->isPreflight()) {
|
| 176 | 176 | $this->setOrigin() |
| 177 | - ->setMaxAge() |
|
| 178 | - ->setAllowCredentials() |
|
| 179 | - ->setAllowMethods() |
|
| 180 | - ->setAllowHeaders(); |
|
| 177 | + ->setMaxAge() |
|
| 178 | + ->setAllowCredentials() |
|
| 179 | + ->setAllowMethods() |
|
| 180 | + ->setAllowHeaders(); |
|
| 181 | 181 | } else {
|
| 182 | 182 | $this->setOrigin() |
| 183 | - ->setExposedHeaders() |
|
| 184 | - ->setAllowCredentials(); |
|
| 183 | + ->setExposedHeaders() |
|
| 184 | + ->setAllowCredentials(); |
|
| 185 | 185 | } |
| 186 | 186 | } |
| 187 | 187 | |
@@ -62,8 +62,7 @@ discard block |
||
| 62 | 62 | * @package Platine\Framework\Http\Middleware |
| 63 | 63 | * @template T |
| 64 | 64 | */ |
| 65 | -class CorsMiddleware implements MiddlewareInterface |
|
| 66 | -{
|
|
| 65 | +class CorsMiddleware implements MiddlewareInterface { |
|
| 67 | 66 | /** |
| 68 | 67 | * The configuration instance |
| 69 | 68 | * @var Config<T> |
@@ -96,7 +95,7 @@ discard block |
||
| 96 | 95 | public function __construct( |
| 97 | 96 | LoggerInterface $logger, |
| 98 | 97 | Config $config |
| 99 | - ) {
|
|
| 98 | + ) { |
|
| 100 | 99 | $this->config = $config; |
| 101 | 100 | $this->logger = $logger; |
| 102 | 101 | } |
@@ -108,13 +107,13 @@ discard block |
||
| 108 | 107 | ServerRequestInterface $request, |
| 109 | 108 | RequestHandlerInterface $handler |
| 110 | 109 | ): ResponseInterface {
|
| 111 | - if (!$this->shouldBeProcessed($request)) {
|
|
| 110 | + if (!$this->shouldBeProcessed($request)) { |
|
| 112 | 111 | return $handler->handle($request); |
| 113 | 112 | } |
| 114 | 113 | |
| 115 | 114 | $this->request = $request; |
| 116 | 115 | |
| 117 | - if ($this->isPreflight()) {
|
|
| 116 | + if ($this->isPreflight()) { |
|
| 118 | 117 | $response = new Response(204); |
| 119 | 118 | |
| 120 | 119 | $this->logger->info( |
@@ -124,7 +123,7 @@ discard block |
||
| 124 | 123 | 'url' => (string) $request->getUri(), |
| 125 | 124 | ] |
| 126 | 125 | ); |
| 127 | - } else {
|
|
| 126 | + } else { |
|
| 128 | 127 | $response = $handler->handle($request); |
| 129 | 128 | } |
| 130 | 129 | $this->response = $response; |
@@ -144,13 +143,13 @@ discard block |
||
| 144 | 143 | //If no route has been match no need check for CORS |
| 145 | 144 | /** @var ?Route $route */ |
| 146 | 145 | $route = $request->getAttribute(Route::class); |
| 147 | - if (!$route) {
|
|
| 146 | + if (!$route) { |
|
| 148 | 147 | return false; |
| 149 | 148 | } |
| 150 | 149 | |
| 151 | 150 | //Check if the path match |
| 152 | 151 | $path = $this->config->get('security.cors.path', '/');
|
| 153 | - if (!preg_match('~^' . $path . '~', $route->getPattern())) {
|
|
| 152 | + if (!preg_match('~^' . $path . '~', $route->getPattern())) { |
|
| 154 | 153 | return false; |
| 155 | 154 | } |
| 156 | 155 | |
@@ -172,13 +171,13 @@ discard block |
||
| 172 | 171 | */ |
| 173 | 172 | protected function setCorsHeaders(): void |
| 174 | 173 | {
|
| 175 | - if ($this->isPreflight()) {
|
|
| 174 | + if ($this->isPreflight()) { |
|
| 176 | 175 | $this->setOrigin() |
| 177 | 176 | ->setMaxAge() |
| 178 | 177 | ->setAllowCredentials() |
| 179 | 178 | ->setAllowMethods() |
| 180 | 179 | ->setAllowHeaders(); |
| 181 | - } else {
|
|
| 180 | + } else { |
|
| 182 | 181 | $this->setOrigin() |
| 183 | 182 | ->setExposedHeaders() |
| 184 | 183 | ->setAllowCredentials(); |
@@ -195,8 +194,8 @@ discard block |
||
| 195 | 194 | // default to the first allowed origin |
| 196 | 195 | $origin = reset($origins); |
| 197 | 196 | |
| 198 | - foreach ($origins as $ori) {
|
|
| 199 | - if ($ori === $this->request->getHeaderLine('Origin')) {
|
|
| 197 | + foreach ($origins as $ori) { |
|
| 198 | + if ($ori === $this->request->getHeaderLine('Origin')) { |
|
| 200 | 199 | $origin = $ori; |
| 201 | 200 | break; |
| 202 | 201 | } |
@@ -219,7 +218,7 @@ discard block |
||
| 219 | 218 | {
|
| 220 | 219 | |
| 221 | 220 | $headers = $this->config->get('security.cors.expose_headers', []);
|
| 222 | - if (!empty($headers)) {
|
|
| 221 | + if (!empty($headers)) { |
|
| 223 | 222 | $this->response = $this->response |
| 224 | 223 | ->withHeader( |
| 225 | 224 | 'Access-Control-Expose-Headers', |
@@ -255,7 +254,7 @@ discard block |
||
| 255 | 254 | {
|
| 256 | 255 | $allowCredentials = $this->config->get('security.cors.allow_credentials', false);
|
| 257 | 256 | |
| 258 | - if ($allowCredentials) {
|
|
| 257 | + if ($allowCredentials) { |
|
| 259 | 258 | $this->response = $this->response |
| 260 | 259 | ->withHeader( |
| 261 | 260 | 'Access-Control-Allow-Credential', |
@@ -274,7 +273,7 @@ discard block |
||
| 274 | 273 | {
|
| 275 | 274 | $methods = $this->config->get('security.cors.allow_methods', []);
|
| 276 | 275 | |
| 277 | - if (!empty($methods)) {
|
|
| 276 | + if (!empty($methods)) { |
|
| 278 | 277 | $this->response = $this->response |
| 279 | 278 | ->withHeader( |
| 280 | 279 | 'Access-Control-Allow-Methods', |
@@ -294,17 +293,17 @@ discard block |
||
| 294 | 293 | |
| 295 | 294 | $headers = $this->config->get('security.cors.allow_headers', []);
|
| 296 | 295 | |
| 297 | - if (empty($headers)) {
|
|
| 296 | + if (empty($headers)) { |
|
| 298 | 297 | //use request headers |
| 299 | 298 | $requestHeaders = $this->request |
| 300 | 299 | ->getHeaderLine('Access-Control-Request-Headers');
|
| 301 | 300 | |
| 302 | - if (!empty($requestHeaders)) {
|
|
| 301 | + if (!empty($requestHeaders)) { |
|
| 303 | 302 | $headers = $requestHeaders; |
| 304 | 303 | } |
| 305 | 304 | } |
| 306 | - if (!empty($headers)) {
|
|
| 307 | - if (is_array($headers)) {
|
|
| 305 | + if (!empty($headers)) { |
|
| 306 | + if (is_array($headers)) { |
|
| 308 | 307 | $headers = implode(', ', $headers);
|
| 309 | 308 | } |
| 310 | 309 | |
@@ -138,8 +138,8 @@ |
||
| 138 | 138 | ); |
| 139 | 139 | |
| 140 | 140 | $httpException->setTitle('Service Unavailable')
|
| 141 | - ->setDescription($message) |
|
| 142 | - ->setHeaders($this->getHeaders($data)); |
|
| 141 | + ->setDescription($message) |
|
| 142 | + ->setHeaders($this->getHeaders($data)); |
|
| 143 | 143 | |
| 144 | 144 | // TODO Add headers |
| 145 | 145 | throw $httpException; |
@@ -71,8 +71,7 @@ discard block |
||
| 71 | 71 | * @package Platine\Framework\Http\Middleware |
| 72 | 72 | * @template T |
| 73 | 73 | */ |
| 74 | -class MaintenanceMiddleware implements MiddlewareInterface |
|
| 75 | -{
|
|
| 74 | +class MaintenanceMiddleware implements MiddlewareInterface { |
|
| 76 | 75 | /** |
| 77 | 76 | * The configuration instance |
| 78 | 77 | * @var Config<T> |
@@ -93,7 +92,7 @@ discard block |
||
| 93 | 92 | public function __construct( |
| 94 | 93 | Config $config, |
| 95 | 94 | Application $app |
| 96 | - ) {
|
|
| 95 | + ) { |
|
| 97 | 96 | $this->config = $config; |
| 98 | 97 | $this->app = $app; |
| 99 | 98 | } |
@@ -106,26 +105,26 @@ discard block |
||
| 106 | 105 | RequestHandlerInterface $handler |
| 107 | 106 | ): ResponseInterface {
|
| 108 | 107 | |
| 109 | - if ($this->isExceptUrl($request)) {
|
|
| 108 | + if ($this->isExceptUrl($request)) { |
|
| 110 | 109 | return $handler->handle($request); |
| 111 | 110 | } |
| 112 | 111 | |
| 113 | - if ($this->app->isInMaintenance()) {
|
|
| 114 | - try {
|
|
| 112 | + if ($this->app->isInMaintenance()) { |
|
| 113 | + try { |
|
| 115 | 114 | $data = $this->app->maintenance()->data(); |
| 116 | - } catch (Throwable $ex) {
|
|
| 115 | + } catch (Throwable $ex) { |
|
| 117 | 116 | throw $ex; |
| 118 | 117 | } |
| 119 | 118 | |
| 120 | - if (isset($data['secret']) && trim($request->getUri()->getPath(), '/') === $data['secret']) {
|
|
| 119 | + if (isset($data['secret']) && trim($request->getUri()->getPath(), '/') === $data['secret']) { |
|
| 121 | 120 | return $this->bypassResponse($data['secret']); |
| 122 | 121 | } |
| 123 | 122 | |
| 124 | - if ($this->hasValidBypassCookie($request, $data)) {
|
|
| 123 | + if ($this->hasValidBypassCookie($request, $data)) { |
|
| 125 | 124 | return $handler->handle($request); |
| 126 | 125 | } |
| 127 | 126 | |
| 128 | - if (isset($data['template'])) {
|
|
| 127 | + if (isset($data['template'])) { |
|
| 129 | 128 | return $this->templateResponse($data['template'], $data); |
| 130 | 129 | } |
| 131 | 130 | |
@@ -179,7 +178,7 @@ discard block |
||
| 179 | 178 | $data['status'] ?? 503 |
| 180 | 179 | ); |
| 181 | 180 | |
| 182 | - foreach ($this->getHeaders($data) as $name => $value) {
|
|
| 181 | + foreach ($this->getHeaders($data) as $name => $value) { |
|
| 183 | 182 | $response = $response->withAddedHeader($name, $value); |
| 184 | 183 | } |
| 185 | 184 | |
@@ -194,8 +193,8 @@ discard block |
||
| 194 | 193 | protected function isExceptUrl(ServerRequestInterface $request): bool |
| 195 | 194 | {
|
| 196 | 195 | $path = $request->getUri()->getPath(); |
| 197 | - foreach ($this->getExceptUrls() as $url) {
|
|
| 198 | - if (Str::is($url, $path)) {
|
|
| 196 | + foreach ($this->getExceptUrls() as $url) { |
|
| 197 | + if (Str::is($url, $path)) { |
|
| 199 | 198 | return true; |
| 200 | 199 | } |
| 201 | 200 | } |
@@ -220,7 +219,7 @@ discard block |
||
| 220 | 219 | */ |
| 221 | 220 | protected function hasValidBypassCookie(ServerRequestInterface $request, array $data): bool |
| 222 | 221 | {
|
| 223 | - if (!isset($data['secret'])) {
|
|
| 222 | + if (!isset($data['secret'])) { |
|
| 224 | 223 | return false; |
| 225 | 224 | } |
| 226 | 225 | |
@@ -228,7 +227,7 @@ discard block |
||
| 228 | 227 | $secret = $data['secret']; |
| 229 | 228 | $name = $this->getCookieName(); |
| 230 | 229 | $cookieValue = (new RequestData($request))->cookie($name); |
| 231 | - if (empty($cookieValue)) {
|
|
| 230 | + if (empty($cookieValue)) { |
|
| 232 | 231 | return false; |
| 233 | 232 | } |
| 234 | 233 | |
@@ -278,7 +277,7 @@ discard block |
||
| 278 | 277 | protected function getHeaders(array $data): array |
| 279 | 278 | {
|
| 280 | 279 | $headers = isset($data['retry']) ? ['Retry-After' => $data['retry']] : []; |
| 281 | - if (isset($data['refresh'])) {
|
|
| 280 | + if (isset($data['refresh'])) { |
|
| 282 | 281 | $headers['Refresh'] = $data['refresh']; |
| 283 | 282 | } |
| 284 | 283 | |
@@ -116,7 +116,7 @@ |
||
| 116 | 116 | */ |
| 117 | 117 | protected function shouldBeProcessed(ServerRequestInterface $request): bool |
| 118 | 118 | {
|
| 119 | - //If no route has been match no need check for CSRF |
|
| 119 | + //If no route has been match no need check for CSRF |
|
| 120 | 120 | /** @var ?Route $route */ |
| 121 | 121 | $route = $request->getAttribute(Route::class); |
| 122 | 122 | if (!$route) {
|
@@ -59,8 +59,7 @@ discard block |
||
| 59 | 59 | * @package Platine\Framework\Http\Middleware |
| 60 | 60 | * @template T |
| 61 | 61 | */ |
| 62 | -class SecurityPolicyMiddleware implements MiddlewareInterface |
|
| 63 | -{
|
|
| 62 | +class SecurityPolicyMiddleware implements MiddlewareInterface { |
|
| 64 | 63 | /** |
| 65 | 64 | * The SecurityPolicy instance |
| 66 | 65 | * @var SecurityPolicy<T> |
@@ -71,8 +70,7 @@ discard block |
||
| 71 | 70 | * Create new instance |
| 72 | 71 | * @param SecurityPolicy<T> $securityPolicy |
| 73 | 72 | */ |
| 74 | - public function __construct(SecurityPolicy $securityPolicy) |
|
| 75 | - {
|
|
| 73 | + public function __construct(SecurityPolicy $securityPolicy) { |
|
| 76 | 74 | $this->securityPolicy = $securityPolicy; |
| 77 | 75 | } |
| 78 | 76 | |
@@ -84,7 +82,7 @@ discard block |
||
| 84 | 82 | RequestHandlerInterface $handler |
| 85 | 83 | ): ResponseInterface {
|
| 86 | 84 | |
| 87 | - if (!$this->shouldBeProcessed($request)) {
|
|
| 85 | + if (!$this->shouldBeProcessed($request)) { |
|
| 88 | 86 | return $handler->handle($request); |
| 89 | 87 | } |
| 90 | 88 | |
@@ -102,7 +100,7 @@ discard block |
||
| 102 | 100 | $response = $handler->handle($request); |
| 103 | 101 | |
| 104 | 102 | $headers = $this->securityPolicy->headers(); |
| 105 | - foreach ($headers as $name => $value) {
|
|
| 103 | + foreach ($headers as $name => $value) { |
|
| 106 | 104 | $response = $response->withAddedHeader($name, $value); |
| 107 | 105 | } |
| 108 | 106 | |
@@ -119,7 +117,7 @@ discard block |
||
| 119 | 117 | //If no route has been match no need check for CSRF |
| 120 | 118 | /** @var ?Route $route */ |
| 121 | 119 | $route = $request->getAttribute(Route::class); |
| 122 | - if (!$route) {
|
|
| 120 | + if (!$route) { |
|
| 123 | 121 | return false; |
| 124 | 122 | } |
| 125 | 123 | |
@@ -147,7 +147,7 @@ |
||
| 147 | 147 | ServerRequestInterface $request, |
| 148 | 148 | RequestHandlerInterface $handler |
| 149 | 149 | ): ResponseInterface {
|
| 150 | - set_error_handler(static function ( |
|
| 150 | + set_error_handler(static function( |
|
| 151 | 151 | int $severity, |
| 152 | 152 | string $message, |
| 153 | 153 | string $file, |
@@ -63,8 +63,7 @@ discard block |
||
| 63 | 63 | * @class ErrorHandlerMiddleware |
| 64 | 64 | * @package Platine\Framework\Http\Middleware |
| 65 | 65 | */ |
| 66 | -class ErrorHandlerMiddleware implements MiddlewareInterface |
|
| 67 | -{
|
|
| 66 | +class ErrorHandlerMiddleware implements MiddlewareInterface { |
|
| 68 | 67 | /** |
| 69 | 68 | * The logger instance to use to save error |
| 70 | 69 | * @var LoggerInterface |
@@ -100,8 +99,7 @@ discard block |
||
| 100 | 99 | * @param bool $detail |
| 101 | 100 | * @param LoggerInterface $logger |
| 102 | 101 | */ |
| 103 | - public function __construct(bool $detail, LoggerInterface $logger) |
|
| 104 | - {
|
|
| 102 | + public function __construct(bool $detail, LoggerInterface $logger) { |
|
| 105 | 103 | $this->detail = $detail; |
| 106 | 104 | $this->logger = $logger; |
| 107 | 105 | $this->errorHandler = $this->getDefaultErrorHandler(); |
@@ -131,9 +129,9 @@ discard block |
||
| 131 | 129 | ErrorHandlerInterface $handler, |
| 132 | 130 | bool $useSubClasses = false |
| 133 | 131 | ): self {
|
| 134 | - if ($useSubClasses) {
|
|
| 132 | + if ($useSubClasses) { |
|
| 135 | 133 | $this->subClassHandlers[$type] = $handler; |
| 136 | - } else {
|
|
| 134 | + } else { |
|
| 137 | 135 | $this->handlers[$type] = $handler; |
| 138 | 136 | } |
| 139 | 137 | |
@@ -155,7 +153,7 @@ discard block |
||
| 155 | 153 | ): bool {
|
| 156 | 154 | // https://www.php.net/manual/en/function.error-reporting.php#8866 |
| 157 | 155 | // Usages the defined levels of `error_reporting()`. |
| 158 | - if (!(error_reporting() & $severity)) {
|
|
| 156 | + if (!(error_reporting() & $severity)) { |
|
| 159 | 157 | // This error code is not included in `error_reporting()`. |
| 160 | 158 | return true; |
| 161 | 159 | } |
@@ -163,9 +161,9 @@ discard block |
||
| 163 | 161 | throw new ErrorException($message, 0, $severity, $file, $line); |
| 164 | 162 | }); |
| 165 | 163 | |
| 166 | - try {
|
|
| 164 | + try { |
|
| 167 | 165 | $response = $handler->handle($request); |
| 168 | - } catch (Throwable $exception) {
|
|
| 166 | + } catch (Throwable $exception) { |
|
| 169 | 167 | $response = $this->handleException($request, $exception); |
| 170 | 168 | } |
| 171 | 169 | |
@@ -183,7 +181,7 @@ discard block |
||
| 183 | 181 | ServerRequestInterface $request, |
| 184 | 182 | Throwable $exception |
| 185 | 183 | ): ResponseInterface {
|
| 186 | - if ($exception instanceof HttpException) {
|
|
| 184 | + if ($exception instanceof HttpException) { |
|
| 187 | 185 | $request = $exception->getRequest(); |
| 188 | 186 | } |
| 189 | 187 | |
@@ -201,16 +199,16 @@ discard block |
||
| 201 | 199 | */ |
| 202 | 200 | protected function getExceptionErrorHandler(string $type): ErrorHandlerInterface |
| 203 | 201 | {
|
| 204 | - if (isset($this->handlers[$type])) {
|
|
| 202 | + if (isset($this->handlers[$type])) { |
|
| 205 | 203 | return $this->handlers[$type]; |
| 206 | 204 | } |
| 207 | 205 | |
| 208 | - if (isset($this->subClassHandlers[$type])) {
|
|
| 206 | + if (isset($this->subClassHandlers[$type])) { |
|
| 209 | 207 | return $this->subClassHandlers[$type]; |
| 210 | 208 | } |
| 211 | 209 | |
| 212 | - foreach ($this->subClassHandlers as $class => $handler) {
|
|
| 213 | - if (is_subclass_of($type, $class)) {
|
|
| 210 | + foreach ($this->subClassHandlers as $class => $handler) { |
|
| 211 | + if (is_subclass_of($type, $class)) { |
|
| 214 | 212 | return $handler; |
| 215 | 213 | } |
| 216 | 214 | } |
@@ -60,8 +60,7 @@ discard block |
||
| 60 | 60 | * @class RouteMatchMiddleware |
| 61 | 61 | * @package Platine\Framework\Http\Middleware |
| 62 | 62 | */ |
| 63 | -class RouteMatchMiddleware implements MiddlewareInterface |
|
| 64 | -{
|
|
| 63 | +class RouteMatchMiddleware implements MiddlewareInterface { |
|
| 65 | 64 | /** |
| 66 | 65 | * The Router instance |
| 67 | 66 | * @var Router |
@@ -79,12 +78,11 @@ discard block |
||
| 79 | 78 | * @param Router $router |
| 80 | 79 | * @param array<string> $allowedMethods the default allowed methods |
| 81 | 80 | */ |
| 82 | - public function __construct(Router $router, array $allowedMethods = ['HEAD']) |
|
| 83 | - {
|
|
| 81 | + public function __construct(Router $router, array $allowedMethods = ['HEAD']) { |
|
| 84 | 82 | $this->router = $router; |
| 85 | 83 | |
| 86 | - foreach ($allowedMethods as $method) {
|
|
| 87 | - if (is_string($method)) {
|
|
| 84 | + foreach ($allowedMethods as $method) { |
|
| 85 | + if (is_string($method)) { |
|
| 88 | 86 | $this->allowedMethods[] = strtoupper($method); |
| 89 | 87 | } |
| 90 | 88 | } |
@@ -97,7 +95,7 @@ discard block |
||
| 97 | 95 | ServerRequestInterface $request, |
| 98 | 96 | RequestHandlerInterface $handler |
| 99 | 97 | ): ResponseInterface {
|
| 100 | - if (!$route = $this->router->match($request, false)) {
|
|
| 98 | + if (!$route = $this->router->match($request, false)) { |
|
| 101 | 99 | return $handler->handle($request); |
| 102 | 100 | } |
| 103 | 101 | |
@@ -105,14 +103,14 @@ discard block |
||
| 105 | 103 | !$this->isAllowedMethod($request->getMethod()) |
| 106 | 104 | && !$route->isAllowedMethod($request->getMethod()) |
| 107 | 105 | && $request->getMethod() !== 'OPTIONS' |
| 108 | - ) {
|
|
| 106 | + ) { |
|
| 109 | 107 | $exception = new HttpMethodNotAllowedException($request); |
| 110 | 108 | $exception->setAllowedMethods($route->getMethods()); |
| 111 | 109 | |
| 112 | 110 | throw $exception; |
| 113 | 111 | } |
| 114 | 112 | |
| 115 | - foreach ($route->getParameters()->all() as $parameter) {
|
|
| 113 | + foreach ($route->getParameters()->all() as $parameter) { |
|
| 116 | 114 | $request = $request->withAttribute( |
| 117 | 115 | $parameter->getName(), |
| 118 | 116 | $parameter->getValue() |
@@ -163,7 +163,7 @@ |
||
| 163 | 163 | } |
| 164 | 164 | |
| 165 | 165 | if (!$this->hasParser($contentType)) {
|
| 166 | - // If not, look for a media type with a structured syntax suffix (RFC 6839) |
|
| 166 | + // If not, look for a media type with a structured syntax suffix (RFC 6839) |
|
| 167 | 167 | $parts = explode('+', $contentType);
|
| 168 | 168 | |
| 169 | 169 | if (count($parts) >= 2) {
|
@@ -138,11 +138,11 @@ |
||
| 138 | 138 | */ |
| 139 | 139 | protected function registerDefaultParsers(): void |
| 140 | 140 | {
|
| 141 | - $this->registerParser('application/json', static function ($body) {
|
|
| 141 | + $this->registerParser('application/json', static function($body) {
|
|
| 142 | 142 | return Json::decode($body, true); |
| 143 | 143 | }); |
| 144 | 144 | |
| 145 | - $this->registerParser('application/x-www-form-urlencoded', static function ($body) {
|
|
| 145 | + $this->registerParser('application/x-www-form-urlencoded', static function($body) {
|
|
| 146 | 146 | $data = []; |
| 147 | 147 | parse_str($body, $data); |
| 148 | 148 | |
@@ -58,8 +58,7 @@ discard block |
||
| 58 | 58 | * @class BodyParserMiddleware |
| 59 | 59 | * @package Platine\Framework\Http\Middleware |
| 60 | 60 | */ |
| 61 | -class BodyParserMiddleware implements MiddlewareInterface |
|
| 62 | -{
|
|
| 61 | +class BodyParserMiddleware implements MiddlewareInterface { |
|
| 63 | 62 | /** |
| 64 | 63 | * List of body parser to use |
| 65 | 64 | * @var array<string, callable> |
@@ -69,8 +68,7 @@ discard block |
||
| 69 | 68 | /** |
| 70 | 69 | * Create new instance |
| 71 | 70 | */ |
| 72 | - public function __construct() |
|
| 73 | - {
|
|
| 71 | + public function __construct() { |
|
| 74 | 72 | $this->registerDefaultParsers(); |
| 75 | 73 | } |
| 76 | 74 | |
@@ -82,7 +80,7 @@ discard block |
||
| 82 | 80 | RequestHandlerInterface $handler |
| 83 | 81 | ): ResponseInterface {
|
| 84 | 82 | $parsedBody = $request->getParsedBody(); |
| 85 | - if ($parsedBody === null || empty($parsedBody)) {
|
|
| 83 | + if ($parsedBody === null || empty($parsedBody)) { |
|
| 86 | 84 | $parsedBody = $this->parseBody($request); |
| 87 | 85 | |
| 88 | 86 | $request = $request->withParsedBody($parsedBody); |
@@ -122,7 +120,7 @@ discard block |
||
| 122 | 120 | */ |
| 123 | 121 | public function getParser(string $contentType): callable |
| 124 | 122 | {
|
| 125 | - if (!isset($this->parsers[$contentType])) {
|
|
| 123 | + if (!isset($this->parsers[$contentType])) { |
|
| 126 | 124 | throw new RuntimeException(sprintf( |
| 127 | 125 | 'Can not find the parser for the given content type [%s]', |
| 128 | 126 | $contentType |
@@ -138,11 +136,11 @@ discard block |
||
| 138 | 136 | */ |
| 139 | 137 | protected function registerDefaultParsers(): void |
| 140 | 138 | {
|
| 141 | - $this->registerParser('application/json', static function ($body) {
|
|
| 139 | + $this->registerParser('application/json', static function ($body) { |
|
| 142 | 140 | return Json::decode($body, true); |
| 143 | 141 | }); |
| 144 | 142 | |
| 145 | - $this->registerParser('application/x-www-form-urlencoded', static function ($body) {
|
|
| 143 | + $this->registerParser('application/x-www-form-urlencoded', static function ($body) { |
|
| 146 | 144 | $data = []; |
| 147 | 145 | parse_str($body, $data); |
| 148 | 146 | |
@@ -155,28 +153,27 @@ discard block |
||
| 155 | 153 | * @param ServerRequestInterface $request |
| 156 | 154 | * @return null|array<mixed>|object |
| 157 | 155 | */ |
| 158 | - protected function parseBody(ServerRequestInterface $request) |
|
| 159 | - {
|
|
| 156 | + protected function parseBody(ServerRequestInterface $request) { |
|
| 160 | 157 | $contentType = $this->getRequestContentType($request); |
| 161 | - if ($contentType === null) {
|
|
| 158 | + if ($contentType === null) { |
|
| 162 | 159 | return null; |
| 163 | 160 | } |
| 164 | 161 | |
| 165 | - if (!$this->hasParser($contentType)) {
|
|
| 162 | + if (!$this->hasParser($contentType)) { |
|
| 166 | 163 | // If not, look for a media type with a structured syntax suffix (RFC 6839) |
| 167 | 164 | $parts = explode('+', $contentType);
|
| 168 | 165 | |
| 169 | - if (count($parts) >= 2) {
|
|
| 166 | + if (count($parts) >= 2) { |
|
| 170 | 167 | $contentType = 'application/' . $parts[count($parts) - 1]; |
| 171 | 168 | } |
| 172 | 169 | } |
| 173 | 170 | |
| 174 | - if ($this->hasParser($contentType)) {
|
|
| 171 | + if ($this->hasParser($contentType)) { |
|
| 175 | 172 | $body = (string) $request->getBody(); |
| 176 | 173 | $parser = $this->getParser($contentType); |
| 177 | 174 | |
| 178 | 175 | $parsed = $parser($body); |
| 179 | - if (!is_null($parsed) && !is_array($parsed) && !is_object($parsed)) {
|
|
| 176 | + if (!is_null($parsed) && !is_array($parsed) && !is_object($parsed)) { |
|
| 180 | 177 | throw new RuntimeException(sprintf( |
| 181 | 178 | 'The return value of body parser must be an array, an ' |
| 182 | 179 | . 'object, or null, got [%s]', |
@@ -200,7 +197,7 @@ discard block |
||
| 200 | 197 | $contentTypes = $request->getHeader('Content-Type');
|
| 201 | 198 | $contentType = $contentTypes[0] ?? null; |
| 202 | 199 | |
| 203 | - if (is_string($contentType) && trim($contentType) !== '') {
|
|
| 200 | + if (is_string($contentType) && trim($contentType) !== '') { |
|
| 204 | 201 | $parts = explode(';', $contentType);
|
| 205 | 202 | |
| 206 | 203 | return strtolower(trim($parts[0])); |
@@ -64,8 +64,7 @@ discard block |
||
| 64 | 64 | * @package Platine\Framework\Http\Middleware |
| 65 | 65 | * @template T |
| 66 | 66 | */ |
| 67 | -class CsrfMiddleware implements MiddlewareInterface |
|
| 68 | -{
|
|
| 67 | +class CsrfMiddleware implements MiddlewareInterface { |
|
| 69 | 68 | /** |
| 70 | 69 | * The configuration instance |
| 71 | 70 | * @var Config<T> |
@@ -108,7 +107,7 @@ discard block |
||
| 108 | 107 | Lang $lang, |
| 109 | 108 | Config $config, |
| 110 | 109 | CsrfManager $csrfManager |
| 111 | - ) {
|
|
| 110 | + ) { |
|
| 112 | 111 | $this->config = $config; |
| 113 | 112 | $this->csrfManager = $csrfManager; |
| 114 | 113 | $this->lang = $lang; |
@@ -123,13 +122,13 @@ discard block |
||
| 123 | 122 | RequestHandlerInterface $handler |
| 124 | 123 | ): ResponseInterface {
|
| 125 | 124 | |
| 126 | - if (!$this->shouldBeProcessed($request)) {
|
|
| 125 | + if (!$this->shouldBeProcessed($request)) { |
|
| 127 | 126 | return $handler->handle($request); |
| 128 | 127 | } |
| 129 | 128 | |
| 130 | 129 | $this->request = $request; |
| 131 | 130 | |
| 132 | - if ($this->isValid() === false) {
|
|
| 131 | + if ($this->isValid() === false) { |
|
| 133 | 132 | return $this->unauthorizedResponse(); |
| 134 | 133 | } |
| 135 | 134 | |
@@ -155,7 +154,7 @@ discard block |
||
| 155 | 154 | //If no route has been match no need check for CSRF |
| 156 | 155 | /** @var ?Route $route */ |
| 157 | 156 | $route = $request->getAttribute(Route::class); |
| 158 | - if (!$route) {
|
|
| 157 | + if (!$route) { |
|
| 159 | 158 | return false; |
| 160 | 159 | } |
| 161 | 160 | |
@@ -164,13 +163,13 @@ discard block |
||
| 164 | 163 | |
| 165 | 164 | $methods = $this->config->get('security.csrf.http_methods', []);
|
| 166 | 165 | |
| 167 | - if (!in_array($request->getMethod(), $methods) && $csrf === false) {
|
|
| 166 | + if (!in_array($request->getMethod(), $methods) && $csrf === false) { |
|
| 168 | 167 | return false; |
| 169 | 168 | } |
| 170 | 169 | |
| 171 | 170 | //check if is url whitelist |
| 172 | 171 | $urls = $this->config->get('security.csrf.url_whitelist', []);
|
| 173 | - if (in_array($route->getName(), $urls)) {
|
|
| 172 | + if (in_array($route->getName(), $urls)) { |
|
| 174 | 173 | return false; |
| 175 | 174 | } |
| 176 | 175 | |
@@ -116,7 +116,7 @@ |
||
| 116 | 116 | */ |
| 117 | 117 | protected function shouldBeProcessed(ServerRequestInterface $request): bool |
| 118 | 118 | {
|
| 119 | - //If no route has been match no need check for CSRF |
|
| 119 | + //If no route has been match no need check for CSRF |
|
| 120 | 120 | /** @var ?Route $route */ |
| 121 | 121 | $route = $request->getAttribute(Route::class); |
| 122 | 122 | if (!$route) {
|