| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | /* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * This file is part of Flight Routing. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * PHP version 7.4 and above required | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * @author    Divine Niiquaye Ibok <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * @copyright 2019 Biurad Group (https://biurad.com/) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * @license   https://opensource.org/licenses/BSD-3-Clause License | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  * For the full copyright and license information, please view the LICENSE | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  * file that was distributed with this source code. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | namespace Flight\Routing\Handlers; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | use Flight\Routing\Exceptions\{InvalidControllerException, RouteNotFoundException}; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | use Flight\Routing\Router; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | use Psr\Http\Message\{ResponseFactoryInterface, ResponseInterface, ServerRequestInterface}; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | use Psr\Http\Server\RequestHandlerInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  * Default routing request handler. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  * if route is found in request attribute, dispatch the route handler's | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  * response to the browser and provides ability to detect right response content-type. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  * @author Divine Niiquaye Ibok <[email protected]> | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 32 |  |  |  */ | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  | class RouteHandler implements RequestHandlerInterface | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |     /** This allows a response to be served when no route is found. */ | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |     public const OVERRIDE_NULL_ROUTE = 'OVERRIDE_NULL_ROUTE'; | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |     /** @var callable */ | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |     protected $handlerResolver; | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |     public function __construct(protected ResponseFactoryInterface $responseFactory, callable $handlerResolver = null) | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |         $this->handlerResolver = $handlerResolver ?? new RouteInvoker(); | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |      * {@inheritdoc} | 
            
                                                                        
                            
            
                                    
            
            
                | 48 | 90 |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |      * @throws InvalidControllerException|RouteNotFoundException | 
            
                                                                        
                            
            
                                    
            
            
                | 50 | 90 |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 51 | 90 |  |     public function handle(ServerRequestInterface $request): ResponseInterface | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |         if (null === $route = $request->getAttribute(Router::class)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |             if (true === $res = $request->getAttribute(static::OVERRIDE_NULL_ROUTE)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |                 return $this->responseFactory->createResponse(); | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |             return $res instanceof ResponseInterface ? $res : throw new RouteNotFoundException($request->getUri()); | 
            
                                                                        
                            
            
                                    
            
            
                | 59 | 85 |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 61 | 85 |  |         if (empty($handler = $route['handler'] ?? null)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 62 | 25 |  |             return $this->responseFactory->createResponse(204)->withHeader('Content-Type', 'text/plain; charset=utf-8'); | 
            
                                                                        
                            
            
                                    
            
            
                | 63 | 10 |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 64 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 65 |  |  |         $arguments = fn (ServerRequestInterface $request): array => $this->resolveArguments($request, $route['arguments'] ?? []); | 
            
                                                                        
                            
            
                                    
            
            
                | 66 | 16 |  |         $response = RouteInvoker::resolveRoute($request, $this->handlerResolver, $handler, $arguments); | 
            
                                                                        
                            
            
                                    
            
            
                | 67 | 15 |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 68 | 15 |  |         if ($response instanceof FileHandler) { | 
            
                                                                        
                            
            
                                    
            
            
                | 69 |  |  |             return $response($this->responseFactory); | 
            
                                                                        
                            
            
                                    
            
            
                | 70 | 15 |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 71 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 72 |  |  |         if (!$response instanceof ResponseInterface) { | 
            
                                                                        
                            
            
                                    
            
            
                | 73 |  |  |             if (empty($contents = $response)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 74 |  |  |                 throw new InvalidControllerException('The route handler\'s content is not a valid PSR7 response body stream.'); | 
            
                                                                        
                            
            
                                    
            
            
                | 75 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 76 | 1 |  |             ($response = $this->responseFactory->createResponse())->getBody()->write($contents); | 
            
                                                                        
                            
            
                                    
            
            
                | 77 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 78 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 79 |  |  |         return $response->hasHeader('Content-Type') ? $response : $this->negotiateContentType($response); | 
            
                                                                        
                            
            
                                    
            
            
                | 80 | 60 |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 81 | 1 |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 82 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 83 |  |  |      * A HTTP response Content-Type header negotiator for html, json, svg, xml, and plain-text. | 
            
                                                                        
                            
            
                                    
            
            
                | 84 | 56 |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 85 | 10 |  |     protected function negotiateContentType(ResponseInterface $response): ResponseInterface | 
            
                                                                        
                            
            
                                    
            
            
                | 86 | 10 |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 87 |  |  |         if (empty($contents = (string) $response->getBody())) { | 
            
                                                                        
                            
            
                                    
            
            
                | 88 |  |  |             $mime = 'text/plain; charset=utf-8'; | 
            
                                                                        
                            
            
                                    
            
            
                | 89 | 56 |  |             $response = $response->withStatus(204); | 
            
                                                                        
                            
            
                                    
            
            
                | 90 |  |  |         } elseif (false === $mime = (new \finfo(\FILEINFO_MIME_TYPE))->buffer($contents)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 91 |  |  |             $mime = 'text/html; charset=utf-8'; // @codeCoverageIgnore | 
            
                                                                        
                            
            
                                    
            
            
                | 92 |  |  |         } elseif ('text/xml' === $mime) { | 
            
                                                                        
                            
            
                                    
            
            
                | 93 |  |  |             \preg_match('/<(?:\s+)?\/?(?:\s+)?(\w+)(?:\s+)?>$/', $contents, $xml, \PREG_UNMATCHED_AS_NULL); | 
            
                                                                        
                            
            
                                    
            
            
                | 94 |  |  |             $mime = 'svg' === $xml[1] ? 'image/svg+xml' : \sprintf('%s; charset=utf-8', 'rss' === $xml[1] ? 'application/rss+xml' : 'text/xml'); | 
            
                                                                        
                            
            
                                    
            
            
                | 95 | 60 |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 96 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 97 | 60 |  |         return $response->withHeader('Content-Type', $mime); | 
            
                                                                        
                            
            
                                    
            
            
                | 98 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 99 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 100 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 101 | 60 |  |      * @param array<string,mixed> $parameters | 
            
                                                                        
                            
            
                                    
            
            
                | 102 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 103 | 60 |  |      * @return array<int|string,mixed> | 
            
                                                                        
                            
            
                                    
            
            
                | 104 | 3 |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |     protected function resolveArguments(ServerRequestInterface $request, array $parameters): array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 | 60 |  |         foreach ([$request, $this->responseFactory] as $psr7) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |             $parameters[$psr7::class] = $psr7; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 | 57 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 | 8 |  |             foreach ((@\class_implements($psr7) ?: []) as $psr7Interface) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |                 $parameters[$psr7Interface] = $psr7; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 | 49 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 | 42 |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 115 |  |  |         return $parameters; | 
            
                                                                        
                                                                
            
                                    
            
            
                | 116 |  |  |     } | 
            
                                                                        
                                                                
            
                                    
            
            
                | 117 |  |  | } | 
            
                                                                        
                                                                
            
                                    
            
            
                | 118 |  |  |  |