/** * @expectedException \Routy\Exceptions\MethodNotAllowed */ public function testEmulateDisallowedRequestWhenTheMethodMatchesThrowsException() { $_SERVER['REQUEST_METHOD'] = 'POST'; $_SERVER['REQUEST_URI'] = '/some/query'; $_POST['_method'] = 'PUT'; $config = new Configuration(); $config->emulatedMethods = ['POST']; Request::fromCurrentRequest($config); }
public function match(Request $request) { //match static routes $otherMethodMatched = false; $dynamicRoutes = []; /** @var Route $route */ foreach ($this->routeContainer as $route) { if ($route->isStatic()) { if ($route->isPath($request->getPath())) { if ($route->isMethod($request->getMethod())) { return $this->createMatch($route, $request->getExtras()); } else { $otherMethodMatched = true; } } } else { $dynamicRoutes[] = $route; } } //group before matching so always CHUNK_SIZE number of different paths and not routes are matched at once $groupedDynamicRoutes = $this->groupRoutesByPattern($dynamicRoutes); //match dynamic routes $chunks = array_chunk($groupedDynamicRoutes, self::CHUNK_SIZE, true); foreach ($chunks as $routes) { $match = $this->matchVariableRouteChunk($request->getPath(), $routes); if ($match) { list($matchedRoutes, $params) = $match; foreach ($matchedRoutes as $route) { if ($route->isMethod($request->getMethod())) { //Map parameters to their names $parameters = $this->mapOrderedParametersToNames($route->getParsed()->getParameterNames(), $params); return $this->createMatch($route, $parameters + $request->getExtras()); } else { $otherMethodMatched = true; } } } } if ($otherMethodMatched) { throw new MethodNotAllowed("{$request->getMethod()} is not allowed for {$request->getPath()}"); } else { throw new NotFound("Route not found: {$request->getPath()}"); } }
public function matchCurrentRequest() { return $this->match(Request::fromCurrentRequest($this->configuration)); }