| 1 |  |  | <?php declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Limoncello\Core\Routing; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * Copyright 2015-2020 [email protected] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * Licensed under the Apache License, Version 2.0 (the "License"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * you may not use this file except in compliance with the License. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * You may obtain a copy of the License at | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * http://www.apache.org/licenses/LICENSE-2.0 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  * Unless required by applicable law or agreed to in writing, software | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  * distributed under the License is distributed on an "AS IS" BASIS, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  * See the License for the specific language governing permissions and | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  * limitations under the License. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | use Limoncello\Common\Reflection\CheckCallableTrait; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | use Limoncello\Contracts\Routing\GroupInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | use Limoncello\Contracts\Routing\RouteInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | use Limoncello\Core\Routing\Traits\CallableTrait; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | use Limoncello\Core\Routing\Traits\HasConfiguratorsTrait; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  | use Limoncello\Core\Routing\Traits\HasMiddlewareTrait; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  | use Limoncello\Core\Routing\Traits\HasRequestFactoryTrait; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  | use Limoncello\Core\Routing\Traits\UriTrait; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  | use LogicException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  | use Psr\Container\ContainerInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  | use Psr\Http\Message\ResponseInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  | use Psr\Http\Message\ServerRequestInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  | use ReflectionException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  | use function array_merge; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |  * @package Limoncello\Core | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |  * @SuppressWarnings(PHPMD.LongVariable) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  | class Route implements RouteInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     use CallableTrait, UriTrait, HasConfiguratorsTrait, HasMiddlewareTrait, HasRequestFactoryTrait, CheckCallableTrait { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         CheckCallableTrait::checkPublicStaticCallable insteadof HasMiddlewareTrait; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         CheckCallableTrait::checkPublicStaticCallable insteadof HasConfiguratorsTrait; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |         CheckCallableTrait::checkPublicStaticCallable insteadof HasRequestFactoryTrait; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |      * @var GroupInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |     private $group; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |      * @var string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |     private $method; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |      * @var string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |     private $uriPath; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |      * @var callable | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |     private $handler; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |      * @var bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |     private $isGroupRequestFactory = true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |      * @var string|null | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |     private $name; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |      * @param GroupInterface $group | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |      * @param string         $method | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |      * @param string         $uriPath | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |      * @param callable       $handler | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 | 22 |  |      * @throws ReflectionException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 | 22 |  |     public function __construct(GroupInterface $group, string $method, string $uriPath, callable $handler) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 | 22 |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 | 22 |  |         $this->group   = $group; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |         $this->method  = $method; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 | 22 |  |         $this->uriPath = $uriPath; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |         $this->setHandler($handler); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |      * @param string|null $name | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 | 18 |  |      * @return self | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 | 18 |  |     public function setName(string $name = null): self | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 | 18 |  |         $this->name = $name; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |         return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 | 18 |  |      * @inheritdoc | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 | 18 |  |     public function getGroup(): GroupInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |         return $this->group; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 | 17 |  |      * @inheritdoc | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 118 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 119 | 17 |  |     public function getMethod(): string | 
            
                                                                        
                            
            
                                    
            
            
                | 120 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 121 |  |  |         return $this->method; | 
            
                                                                        
                            
            
                                    
            
            
                | 122 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 | 18 |  |      * @inheritdoc | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 | 18 |  |     public function getUriPath(): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 | 18 |  |         $uriPath = $this->concatUri($this->getGroup()->getUriPrefix(), $this->uriPath); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |         return $this->normalizeUri($uriPath, $this->getGroup()->hasTrailSlash()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 | 17 |  |      * @inheritdoc | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 | 17 |  |     public function getMiddleware(): array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |         return array_merge($this->getGroup()->getMiddleware(), $this->middleware); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 | 17 |  |      * @inheritdoc | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 | 17 |  |     public function getHandler(): callable | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |         return $this->handler; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 | 17 |  |      * @inheritdoc | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 | 17 |  |     public function getContainerConfigurators(): array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |         return array_merge($this->getGroup()->getContainerConfigurators(), $this->configurators); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 | 17 |  |      * @inheritdoc | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 | 17 |  |     public function getRequestFactory(): ?callable | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 | 17 |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |         if ($this->isUseGroupRequestFactory() === true) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |             return $this->getGroup()->getRequestFactory(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 | 14 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  |         return $this->isRequestFactorySet() === true ? $this->requestFactory : $this->getDefaultRequestFactory(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 | 17 |  |      * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 | 17 |  |     public function isUseGroupRequestFactory(): bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 |  |  |         return $this->isGroupRequestFactory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 177 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 178 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 179 |  |  |      * @param bool $isGroupFactory | 
            
                                                                                                            
                            
            
                                    
            
            
                | 180 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 181 | 21 |  |      * @return self | 
            
                                                                                                            
                            
            
                                    
            
            
                | 182 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 183 | 21 |  |     public function setUseGroupRequestFactory(bool $isGroupFactory): self | 
            
                                                                                                            
                            
            
                                    
            
            
                | 184 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 185 | 21 |  |         $this->isGroupRequestFactory = $isGroupFactory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 186 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 187 |  |  |         return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 188 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 189 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 190 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 191 | 16 |  |      * @inheritdoc | 
            
                                                                                                            
                            
            
                                    
            
            
                | 192 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 193 | 16 |  |     public function getName(): ?string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 194 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 195 | 16 |  |         $result = $this->name !== null ? (string)$this->getGroup()->getName() . $this->name : null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 196 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 197 |  |  |         return $result; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 198 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 199 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 200 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 201 |  |  |      * @param callable $handler | 
            
                                                                                                            
                            
            
                                    
            
            
                | 202 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 203 |  |  |      * @return self | 
            
                                                                                                            
                            
            
                                    
            
            
                | 204 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 205 | 22 |  |      * @throws ReflectionException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 206 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 207 | 22 |  |     protected function setHandler(callable $handler): self | 
            
                                                                                                            
                            
            
                                    
            
            
                | 208 | 22 |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 209 |  |  |         $isValidHandler = $this->checkPublicStaticCallable($handler, [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 210 |  |  |             'array', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 211 | 22 |  |             ContainerInterface::class, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 212 | 22 |  |             ServerRequestInterface::class, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 213 |  |  |         ], ResponseInterface::class); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 214 |  |  |         if ($isValidHandler === false) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 215 | 1 |  |             // Handler method should have signature | 
            
                                                                                                            
                            
            
                                    
            
            
                | 216 |  |  |             // `public static methodName(array, ContainerInterface, ServerRequestInterface): ResponseInterface`' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 217 |  |  |             throw new LogicException($this->getCallableToCacheMessage()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 218 | 21 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 219 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 220 | 21 |  |         $this->handler = $handler; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 221 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 222 |  |  |         return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 223 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 224 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 225 |  |  |  |