/** * 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); }
/** * @covers Imbo\Http\Response\ResponseFormatter::negotiate */ public function testDoesNotForceContentNegotiationOnErrorModelsWhenResourceIsNotAnImage() { $route = new Route(); $route->setName('user'); $this->request->expects($this->once())->method('getExtension')->will($this->returnValue('json')); $this->request->expects($this->once())->method('getRoute')->will($this->returnValue($route)); $this->response->expects($this->never())->method('setVary'); $this->response->expects($this->once())->method('getModel')->will($this->returnValue($this->getMock('Imbo\\Model\\Error'))); $this->responseFormatter->negotiate($this->event); $this->assertSame('json', $this->responseFormatter->getFormatter()); }
/** * @covers Imbo\Router\Route::setName * @covers Imbo\Router\Route::__toString */ public function testReturnsTheSetName() { $this->assertSame($this->route, $this->route->setName('name')); $this->assertSame('name', (string) $this->route); }