utilities-php /
router
| 1 | <?php |
||
| 2 | declare(strict_types=1); |
||
| 3 | |||
| 4 | namespace Utilities\Router\Traits; |
||
| 5 | |||
| 6 | use Utilities\Router\Controller; |
||
| 7 | use Utilities\Router\Router; |
||
| 8 | |||
| 9 | /** |
||
| 10 | * RouterTrait class |
||
| 11 | * |
||
| 12 | * @link https://github.com/utilities-php/router |
||
| 13 | * @author Shahrad Elahi (https://github.com/shahradelahi) |
||
| 14 | * @license https://github.com/utilities-php/router/blob/master/LICENSE (MIT License) |
||
| 15 | */ |
||
| 16 | trait RouterTrait |
||
| 17 | { |
||
| 18 | |||
| 19 | /** |
||
| 20 | * @var array |
||
| 21 | */ |
||
| 22 | private static array $routes = []; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * @var array|Controller[] |
||
| 26 | */ |
||
| 27 | private static array $controllers = []; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * The rate limited routes. |
||
| 31 | * |
||
| 32 | * @var array |
||
| 33 | */ |
||
| 34 | private static array $rateLimitedRoutes = []; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @var array|string[] |
||
| 38 | */ |
||
| 39 | private static array $defaultMethods = [ |
||
| 40 | 'ANY', |
||
| 41 | 'GET', |
||
| 42 | 'POST', |
||
| 43 | 'PUT', |
||
| 44 | 'PATCH', |
||
| 45 | 'DELETE', |
||
| 46 | 'OPTIONS', |
||
| 47 | ]; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Find dynamic route with the given uri |
||
| 51 | * |
||
| 52 | * @param string $uri |
||
| 53 | * @return array|false [method, route, params] |
||
| 54 | */ |
||
| 55 | private static function find(string $uri): array|false |
||
| 56 | { |
||
| 57 | foreach (static::$routes as $method => $routes) { |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 58 | foreach ($routes as $route => $callback) { |
||
| 59 | if (preg_match_all(self::convertUri2RegExp($route), $uri, $matches)) { |
||
| 60 | $params = []; |
||
| 61 | foreach ($matches as $key => $value) { |
||
| 62 | if (is_numeric($key)) { |
||
| 63 | continue; |
||
| 64 | } |
||
| 65 | |||
| 66 | $params[$key] = $value[0]; |
||
| 67 | } |
||
| 68 | |||
| 69 | return [ |
||
| 70 | 'method' => $method, |
||
| 71 | 'route' => $route, |
||
| 72 | 'params' => $params, |
||
| 73 | ]; |
||
| 74 | } |
||
| 75 | } |
||
| 76 | } |
||
| 77 | |||
| 78 | return false; |
||
| 79 | } |
||
| 80 | |||
| 81 | /** |
||
| 82 | * @param string $uri |
||
| 83 | * @return string |
||
| 84 | */ |
||
| 85 | private static function convertUri2RegExp(string $uri): string |
||
| 86 | { |
||
| 87 | if (str_contains($uri, '{')) { |
||
| 88 | $uri = preg_replace('/\{([^}]+)}/', '(?<$1>[^/]+)', $uri); |
||
| 89 | } |
||
| 90 | |||
| 91 | return '#^' . $uri . '$#'; |
||
| 92 | } |
||
| 93 | |||
| 94 | /** |
||
| 95 | * Find the route and pass the data to the callback |
||
| 96 | * |
||
| 97 | * @param string $uri |
||
| 98 | * @return void |
||
| 99 | */ |
||
| 100 | private static function findAndPassData(string $uri): void |
||
| 101 | { |
||
| 102 | if (($find = Router::find($uri)) !== false) { |
||
| 103 | if (is_callable(($callback = static::$routes[$find['method']][$find['route']]))) { |
||
|
0 ignored issues
–
show
|
|||
| 104 | call_user_func_array($callback, [...$find['params']]); |
||
| 105 | } |
||
| 106 | } |
||
| 107 | } |
||
| 108 | |||
| 109 | } |