| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  * WebHemi | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * PHP version 5.6 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * @copyright 2012 - 2016 Gixx-web (http://www.gixx-web.com) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * @license   https://opensource.org/licenses/MIT The MIT License (MIT) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * @link      http://www.gixx-web.com | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | namespace WebHemi\Adapter\Router\FastRoute; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use FastRoute\Dispatcher; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use FastRoute\RouteCollector; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use Psr\Http\Message\ServerRequestInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use WebHemi\Adapter\Router\RouterAdapterInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | use WebHemi\Config\ConfigInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | use WebHemi\Routing\Result; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  * Class FastRouteAdapter. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | class FastRouteAdapter implements RouterAdapterInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     /** @var Result */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     private $result; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     /** @var ConfigInterface */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     private $config; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     /** @var Dispatcher */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     private $adapter; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     /** @var string */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     private $applicationPath; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |      * FastRouteAdapter constructor. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |      * @param Result          $routeResult | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |      * @param ConfigInterface $routeConfig | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |      * @param string          $applicationPath | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 41 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |     public function __construct(Result $routeResult, ConfigInterface $routeConfig, $applicationPath = '/') | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |         $this->result = $routeResult; | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |         $this->config = $routeConfig; | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |         $this->applicationPath = $applicationPath; | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |         $routes = $this->config->toArray(); | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  |         $this->adapter = \FastRoute\simpleDispatcher( | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |             function (RouteCollector $routeCollector) use ($routes) { | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |                 foreach ($routes as $route) { | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |                     $method   = $route['allowed_methods']; | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |                     $uri      = $route['path']; | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |                     $callback = $route['middleware']; | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |                     $routeCollector->addRoute($method, $uri, $callback); | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |                 } | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |         ); | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |      * According to the application path, determines the route uri | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |      * @param ServerRequestInterface $request | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |      * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |     private function getApplicationRouteUri(ServerRequestInterface $request) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |         $uri = $request->getUri()->getPath(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |         if (strpos($uri, $this->applicationPath) === 0) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |             $uri = ltrim($uri, $this->applicationPath); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |         if (empty($uri)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |             $uri = '/'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |         return $uri; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |      * Processes the Request and give a Result. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |      * @param ServerRequestInterface $request | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |      * @return Result | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |     public function match(ServerRequestInterface $request) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |         $httpMethod = $request->getMethod(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |         $uri        = $this->getApplicationRouteUri($request); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |         $routeInfo  = $this->adapter->dispatch($httpMethod, $uri); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |         switch ($routeInfo[0]) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |             case Dispatcher::FOUND: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |                 $this->result->setStatus(Result::CODE_FOUND); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |                 $this->result->setMatchedMiddleware($routeInfo[1]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |                 break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |             case Dispatcher::METHOD_NOT_ALLOWED: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |                 $this->result->setStatus(Result::CODE_BAD_METHOD); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |                 break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |             case Dispatcher::NOT_FOUND: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |             default: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |                 $this->result->setStatus(Result::CODE_NOT_FOUND); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |                 break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |         return $this->result; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 113 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 114 |  |  |  |