| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Dazzle\Channel\Router; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Dazzle\Channel\Protocol\ProtocolInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | class Router implements RouterInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |      * @var int | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |     const MODE_DEFAULT = 1; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |      * @var int | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |     const MODE_ROUTER = 1; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |      * @var int | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     const MODE_FIREWALL = 2; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |      * @var RouterRule[] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     protected $rules; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |      * @var int | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     protected $rulesPointer; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |      * @var RouterRule[] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     protected $anchors; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |      * @var int | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     protected $anchorsPointer; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |      * @var int | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |     protected $flags; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |      * @param int $flags | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 | 29 |  |     public function __construct($flags = Router::MODE_ROUTER) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 | 29 |  |         $this->rules = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 | 29 |  |         $this->rulesPointer = 0; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 | 29 |  |         $this->anchors = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 | 29 |  |         $this->anchorsPointer = 0; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 | 29 |  |         $this->flags = $flags; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 | 29 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 | 8 |  |     public function __destruct() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 | 8 |  |         $this->erase(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 | 8 |  |         unset($this->rules); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 | 8 |  |         unset($this->rulesPointer); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 | 8 |  |         unset($this->anchors); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 | 8 |  |         unset($this->anchorsPointer); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 | 8 |  |         unset($this->flags); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 | 8 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |      * @override | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |      * @inheritDoc | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 78 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 79 | 11 |  |     public function handle($name, ProtocolInterface $protocol, $flags = 0, callable $success = null, callable $failure = null, callable $cancel = null, $timeout = 0.0) | 
            
                                                                        
                            
            
                                    
            
            
                | 80 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 81 | 11 |  |         $status = $this->flags === Router::MODE_ROUTER; | 
            
                                                                        
                            
            
                                    
            
            
                | 82 | 11 |  |         $handled = !$status; | 
            
                                                                        
                            
            
                                    
            
            
                | 83 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 84 | 11 |  |         foreach ($this->rules as $handler) | 
            
                                                                        
                            
            
                                    
            
            
                | 85 |  |  |         { | 
            
                                                                        
                            
            
                                    
            
            
                | 86 | 7 |  |             if ($handler->match($name, $protocol) === true) | 
            
                                                                        
                            
            
                                    
            
            
                | 87 |  |  |             { | 
            
                                                                        
                            
            
                                    
            
            
                | 88 | 5 |  |                 if ($handler->handle($name, $protocol, $flags, $success, $failure, $cancel, $timeout) === false) | 
            
                                                                        
                            
            
                                    
            
            
                | 89 |  |  |                 { | 
            
                                                                        
                            
            
                                    
            
            
                | 90 | 5 |  |                     return $status; | 
            
                                                                        
                            
            
                                    
            
            
                | 91 |  |  |                 } | 
            
                                                                        
                            
            
                                    
            
            
                | 92 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 93 | 4 |  |                 $handled = $status; | 
            
                                                                        
                            
            
                                    
            
            
                | 94 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 95 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 96 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 97 | 6 |  |         if ($handled === $status) | 
            
                                                                        
                            
            
                                    
            
            
                | 98 |  |  |         { | 
            
                                                                        
                            
            
                                    
            
            
                | 99 |  |  |             return $handled; | 
            
                                                                        
                            
            
                                    
            
            
                | 100 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 101 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 102 | 6 |  |         foreach ($this->anchors as $anchor) | 
            
                                                                        
                            
            
                                    
            
            
                | 103 |  |  |         { | 
            
                                                                        
                            
            
                                    
            
            
                | 104 | 4 |  |             if ($anchor->handle($name, $protocol, $flags, $success, $failure, $cancel, $timeout) === false) | 
            
                                                                        
                            
            
                                    
            
            
                | 105 |  |  |             { | 
            
                                                                        
                            
            
                                    
            
            
                | 106 | 4 |  |                 return $status; | 
            
                                                                        
                            
            
                                    
            
            
                | 107 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 108 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 109 | 1 |  |             $handled = $status; | 
            
                                                                        
                            
            
                                    
            
            
                | 110 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 111 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 112 | 2 |  |         return $handled; | 
            
                                                                        
                            
            
                                    
            
            
                | 113 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |      * @override | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |      * @inheritDoc | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 | 8 |  |     public function erase() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 | 8 |  |         foreach ($this->rules as $handler) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |         { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 | 1 |  |             $handler->cancel(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 | 8 |  |         foreach ($this->anchors as $anchor) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |         { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 | 1 |  |             $anchor->cancel(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 | 8 |  |         $this->rules = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 | 8 |  |         $this->rulesPointer = 0; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 | 8 |  |         $this->anchors = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 | 8 |  |         $this->anchorsPointer = 0; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 | 8 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |      * @override | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |      * @inheritDoc | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 | 9 |  |     public function addRule(callable $matcher, callable $handler, $propagate = false, $limit = 0) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 | 9 |  |         return $this->addRuleHandler( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 | 9 |  |             new RouterRule($this, $matcher, $handler, $propagate, $limit) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |      * @override | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |      * @inheritDoc | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 | 6 |  |     public function addDefault(callable $handler, $propagate = false, $limit = 0) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 | 6 |  |         return $this->addDefaultHandler( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |             new RouterRule($this, function() {}, $handler, $propagate, $limit) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |      * @internal | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |      * @param $stack | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |      * @param $pointer | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 | 2 |  |     public function removeHandler($stack, $pointer) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 | 2 |  |         unset($this->{$stack}[$pointer]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 | 2 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  |      * @param RouterRule $handler | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  |      * @return RouterRule | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 | 12 |  |     protected function addRuleHandler(RouterRule $handler) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 | 12 |  |         $this->rules[$this->rulesPointer] = $handler; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 | 12 |  |         $handler->setPointer('handlers', $this->rulesPointer); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 177 | 12 |  |         $this->rulesPointer++; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 178 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 179 | 12 |  |         return $handler; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 180 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 181 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 182 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 183 |  |  |      * @param RouterRule $handler | 
            
                                                                                                            
                            
            
                                    
            
            
                | 184 |  |  |      * @return RouterRule | 
            
                                                                                                            
                            
            
                                    
            
            
                | 185 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 186 | 7 |  |     protected function addDefaultHandler(RouterRule $handler) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 187 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 188 | 7 |  |         $this->anchors[$this->anchorsPointer] = $handler; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 189 | 7 |  |         $handler->setPointer('anchors', $this->anchorsPointer); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 190 | 7 |  |         $this->anchorsPointer++; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 191 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 192 | 7 |  |         return $handler; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 193 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 194 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 195 |  |  |  |