|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\Routing; |
| 4 | + |
| 5 | +use App\Http\Request; |
| 6 | +use App\Http\Response; |
| 7 | + |
| 8 | +class Route |
| 9 | +{ |
| 10 | + private static $routes = []; |
| 11 | + private $matches_route = null; |
| 12 | + private $wildcard = '/\{(.*?)\}/'; |
| 13 | + private $parameters = []; |
| 14 | + private $query = []; |
| 15 | + |
| 16 | + public function __construct($method, $path) |
| 17 | + { |
| 18 | + $this->method = $method; |
| 19 | + $this->path = $path; |
| 20 | + } |
| 21 | + |
| 22 | + public static function get($uri, $fn) |
| 23 | + { |
| 24 | + self::make("GET", $uri, $fn); |
| 25 | + } |
| 26 | + |
| 27 | + public static function post($uri, $fn) |
| 28 | + { |
| 29 | + self::make("POST", $uri, $fn); |
| 30 | + } |
| 31 | + |
| 32 | + public static function put($uri, $fn) |
| 33 | + { |
| 34 | + self::make("PUT", $uri, $fn); |
| 35 | + } |
| 36 | + |
| 37 | + public static function delete($uri, $fn) |
| 38 | + { |
| 39 | + self::make("DELETE", $uri, $fn); |
| 40 | + } |
| 41 | + |
| 42 | + public function call() |
| 43 | + { |
| 44 | + $routes = $this->filter_routes_by_method(self::$routes, $this->method) ?: Response::_404(); |
| 45 | + |
| 46 | + foreach ($routes as $value) { |
| 47 | + if ($this->matche($value["pattern"], $this->path) === true) { |
| 48 | + $this->matches_route = (object) $value; |
| 49 | + break 1; |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + $this->matches_route ?: Response::_404(); |
| 54 | + |
| 55 | + $callback = $this->matches_route->callback; |
| 56 | + $arg = (object)["params" => (object) $this->parameters, "query" => (object) $this->query]; |
| 57 | + return call_user_func($callback, (new Request($arg))); |
| 58 | + } |
| 59 | + |
| 60 | + public static function make($method, $pattern, $callback) |
| 61 | + { |
| 62 | + $action = ["method" => $method, "pattern" => $pattern, "callback" => $callback]; |
| 63 | + array_push(self::$routes, $action); |
| 64 | + } |
| 65 | + |
| 66 | + protected function filter_routes_by_method(array $routes, string $method) |
| 67 | + { |
| 68 | + $_ = []; |
| 69 | + foreach ($routes as $value) { |
| 70 | + if ($value["method"] === $method) { |
| 71 | + array_push($_, $value); |
| 72 | + } |
| 73 | + } |
| 74 | + return $_; |
| 75 | + } |
| 76 | + |
| 77 | + public function matche($pattern, $path) |
| 78 | + { |
| 79 | + $url = parse_url($path); |
| 80 | + $path = $url["path"]; |
| 81 | + parse_str($url["query"] ?? '', $this->query); |
| 82 | + |
| 83 | + $parameters = preg_replace_callback($this->wildcard, function ($m) { |
| 84 | + return '(?<' . preg_replace($this->wildcard, '$1', $m[0]) . '>[a-zA-Z0-9_\-\@\.]+)'; |
| 85 | + }, $pattern); |
| 86 | + |
| 87 | + $parameters = ('@^' . $parameters . '$@'); |
| 88 | + |
| 89 | + if (preg_match($parameters, $path, $params)) { |
| 90 | + foreach ($params as $key => $value) { |
| 91 | + if (is_string($key)) { |
| 92 | + $this->parameters[$key] = $value; |
| 93 | + } |
| 94 | + } |
| 95 | + return true; |
| 96 | + } |
| 97 | + return false; |
| 98 | + } |
| 99 | +} |
0 commit comments