| Total Complexity | 8 |
| Total Lines | 47 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 1 |
| 1 | <?php |
||
| 5 | trait PrioritizeTrait{ |
||
| 6 | |||
| 7 | protected array $routes = []; |
||
| 8 | |||
| 9 | protected function sortRoutes(): void |
||
| 10 | { |
||
| 11 | $staticRoutes = []; |
||
| 12 | $paramRoutes = []; |
||
| 13 | |||
| 14 | foreach($this->routes as $r => $route){ |
||
| 15 | |||
| 16 | $path = urldecode($route['uri']->getPath()); |
||
| 17 | |||
| 18 | if(strstr($path,'{')){ |
||
| 19 | $paramRoutes[$this->getKeyArray(substr_count($path,'/') + substr_count($path,'{'),$paramRoutes)] = $route; |
||
| 20 | continue; |
||
| 21 | } |
||
| 22 | |||
| 23 | $staticRoutes[$this->getKeyArray(substr_count($path,'/'),$staticRoutes)] = $route; |
||
| 24 | |||
| 25 | } |
||
| 26 | |||
| 27 | rsort($paramRoutes); |
||
| 28 | rsort($staticRoutes); |
||
| 29 | |||
| 30 | $this->orderRoutes(array_merge($staticRoutes,$paramRoutes)); |
||
| 31 | } |
||
| 32 | |||
| 33 | private function getKeyArray(int $index, array $array): int |
||
| 34 | { |
||
| 35 | while(array_key_exists($index,$array)){ |
||
| 36 | $index++; |
||
| 37 | } |
||
| 38 | return $index; |
||
| 39 | } |
||
| 40 | |||
| 41 | private function orderRoutes(array $routes):void |
||
| 52 | } |
||
| 53 | } |