| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace App\Middlewares; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use App\Http\Request; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use App\Http\Interfaces\Middleware\ILayer; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use App\Container; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * App\Middleware\Restful | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * Patch kernel action from request method | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | class Restful implements ILayer | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |     use \App\Middlewares\Reuse\TInit; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     const _SIGN = 'X-Middleware-Restful'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     const METHOD_ACTIONS = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |         Request::METHOD_GET => 'index', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |         Request::METHOD_POST => 'store', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |         Request::METHOD_PUT => 'update', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |         Request::METHOD_PATCH => 'update', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |         Request::METHOD_OPTIONS => 'preflight', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |      * peel | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |      * @param Container $container | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |      * @param \Closure $next | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |      * @return \Closure | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 | 7 |  |     public function peel(Container $container, \Closure $next) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 | 7 |  |         $this->init($container); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 | 7 |  |         $this->process(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 | 7 |  |         return $next($container); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |      * process | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |      * | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 45 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 46 | 1 |  |     private function process() | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 48 | 1 |  |         if ($this->enabled) { | 
            
                                                                        
                            
            
                                    
            
            
                | 49 | 1 |  |             $this->response->getHeaderManager()->add( | 
            
                                                                        
                            
            
                                    
            
            
                | 50 | 1 |  |                 self::_SIGN, | 
            
                                                                        
                            
            
                                    
            
            
                | 51 | 1 |  |                 microtime(true) | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |             ); | 
            
                                                                        
                            
            
                                    
            
            
                | 53 | 1 |  |             if ($this->required()) { | 
            
                                                                        
                            
            
                                    
            
            
                | 54 | 1 |  |                 $pureCa = preg_replace('/\?.*/', '', $this->caUri()) . '/'; | 
            
                                                                        
                            
            
                                    
            
            
                | 55 | 1 |  |                 $caFrags = explode('/', $pureCa); | 
            
                                                                        
                            
            
                                    
            
            
                | 56 | 1 |  |                 $controller = $caFrags[0]; | 
            
                                                                        
                            
            
                                    
            
            
                | 57 | 1 |  |                 $met = $this->request->getMethod(); | 
            
                                                                        
                            
            
                                    
            
            
                | 58 | 1 |  |                 $action = self::METHOD_ACTIONS[$met]; | 
            
                                                                        
                            
            
                                    
            
            
                | 59 | 1 |  |                 $this->kernel->setAction([$controller, $action]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |      * required | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |      * @return boolean | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 | 1 |  |     protected function required(): bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 | 1 |  |         return (!$this->isExclude() && $this->requestUriPrefix() === $this->prefix); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |      * check exclusion uri fragment from exclude regexps. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |      * note that negate with ! means allow. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |      * @return Boolean | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 | 1 |  |     protected function isExclude(): bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 | 1 |  |         $reqFrag = $this->caUri(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 | 1 |  |         $countExc = count($this->exclude); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 | 1 |  |         for ($i = 0; $i < $countExc; $i++) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 | 1 |  |             $matches = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 | 1 |  |             $match = preg_match($this->exclude[$i], $reqFrag, $matches); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 | 1 |  |             if ($match) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 | 1 |  |                 return true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 | 1 |  |         return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |      * return controller action from uri | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |      * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 | 1 |  |     protected function caUri(): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 | 1 |  |         return str_replace($this->prefix, '', $this->request->getUri()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |      * uriPrefix | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |      * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 | 1 |  |     protected function requestUriPrefix(): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 | 1 |  |         return substr($this->request->getUri(), 0, strlen($this->prefix)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 113 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 114 |  |  |  |