/** * Route the current request * * @param Request $request The current request */ public function route(Request $request) { $httpMethod = $request->getMethod(); if ($httpMethod === 'BREW') { throw new RuntimeException('I\'m a teapot!', 418); } if (!isset(self::$supportedHttpMethods[$httpMethod])) { throw new RuntimeException('Unsupported HTTP method: ' . $httpMethod, 501); } $path = $request->getPathInfo(); $matches = []; foreach ($this->routes as $resourceName => $route) { if (preg_match($route, $path, $matches)) { break; } } // Path matched no route if (!$matches) { throw new RuntimeException('Not Found', 404); } // Create and populate a route instance that we want to inject into the request $route = new Route(); $route->setName($resourceName); // Inject all matches into the route as parameters foreach ($matches as $key => $value) { if (is_string($key)) { $route->set($key, $value); } } // Store the route in the request $request->setRoute($route); }