|
1 | 1 | <?php |
2 | 2 | namespace GuahanWeb\Http; |
3 | 3 |
|
| 4 | +class RouterException extends \Exception {} |
| 5 | + |
4 | 6 | class Router { |
5 | 7 | protected $routes; |
| 8 | + protected $supported_methods; |
6 | 9 |
|
7 | 10 | protected function __construct() { |
8 | | - $this->routes = array( |
9 | | - 'GET' => array(), |
10 | | - 'POST' => array(), |
11 | | - 'PUT' => array(), |
12 | | - 'DELETE' => array() |
13 | | - ); |
| 11 | + $this->supported_methods = array('GET', 'POST', 'PUT', 'DELETE'); |
| 12 | + $this->routes = array(); |
| 13 | + foreach ($this->supported_methods as $method) { |
| 14 | + $this->routes[$method] = array(); |
| 15 | + } |
14 | 16 | } |
15 | 17 |
|
16 | 18 | static public function instance() { |
@@ -58,10 +60,80 @@ protected function match($method, $uri, &$params = null) { |
58 | 60 | return false; |
59 | 61 | } |
60 | 62 |
|
| 63 | + /** |
| 64 | + * Registers a new route |
| 65 | + * |
| 66 | + * @param {string|array} $method The HTTP method(s) this route supports |
| 67 | + * @param {string} $route The route pattern to register |
| 68 | + * @param {function} $handler The handler to execute when route and method matches |
| 69 | + * @return void |
| 70 | + */ |
| 71 | + public function route($method, $route, $handler) { |
| 72 | + if (is_array($method)) { |
| 73 | + foreach ($method as $m) { |
| 74 | + $this->route($m, $route, $handler); |
| 75 | + } |
| 76 | + } elseif ($method == '*') { |
| 77 | + foreach ($this->supported_methods as $m) { |
| 78 | + $this->route($m, $route, $handler); |
| 79 | + } |
| 80 | + } elseif (in_array($method, strtoupper($this->supported_methods))) { |
| 81 | + $this->routes[strtoupper($method)][$route] = $handler; |
| 82 | + } else { |
| 83 | + throw new RouterException(sprintf('Unsupported HTTP verb provided for route: %s', $method)); |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * Shorthand to register GET handlers |
| 89 | + * |
| 90 | + * @param {string} $route The route pattern to register |
| 91 | + * @param {function} $handler The handler to execute when route matches |
| 92 | + * @return void |
| 93 | + */ |
61 | 94 | public function get($route, $handler) { |
62 | | - $this->routes['GET'][$route] = $handler; |
| 95 | + $this->route('GET', $route, $handler); |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * Shorthand to register POST handlers |
| 100 | + * |
| 101 | + * @param {string} $route The route pattern to register |
| 102 | + * @param {function} $handler The handler to execute when route matches |
| 103 | + * @return void |
| 104 | + */ |
| 105 | + public function post($route, $handler) { |
| 106 | + $this->route('POST', $route, $handler); |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * Shorthand to register PUT handlers |
| 111 | + * |
| 112 | + * @param {string} $route The route pattern to register |
| 113 | + * @param {function} $handler The handler to execute when route matches |
| 114 | + * @return void |
| 115 | + */ |
| 116 | + public function put($route, $handler) { |
| 117 | + $this->route('PUT', $route, $handler); |
| 118 | + } |
| 119 | + |
| 120 | + /** |
| 121 | + * Shorthand to register DELETE handlers |
| 122 | + * |
| 123 | + * @param {string} $route The route pattern to register |
| 124 | + * @param {function} $handler The handler to execute when route matches |
| 125 | + * @return void |
| 126 | + */ |
| 127 | + public function delete($route, $handler) { |
| 128 | + $this->route('DELETE', $route, $handler); |
63 | 129 | } |
64 | 130 |
|
| 131 | + /** |
| 132 | + * Activates the router to begin handling the HTTP request. If no matching routes are found, |
| 133 | + * we will automatically send a 404. |
| 134 | + * |
| 135 | + * @return void |
| 136 | + */ |
65 | 137 | public function process() { |
66 | 138 | $request = new Request(); |
67 | 139 | $response = new Response(); |
|
0 commit comments