/** * Gets list of candidate Route objects for request * * @return array List of Route objects */ public function getCandidates() { $candidates = array(); foreach ($this->collection->all() as $name => $route) { $specs = array(); preg_match_all('/\\{\\w+\\}/', $route->getPath(), $matches); if (isset($matches[0])) { $specs = $matches[0]; } foreach ($specs as $spec) { $param = substr($spec, 1, -1); $regexSpec = '\\' . $spec . '\\'; $requirements = $route->getRequirements(); if (isset($requirements[$param])) { $route->setRegex(str_replace($spec, $this->getRegexOperand($requirements[$param]), $route->getRegex())); } } // Build regeular expression to match routes $route->setRegex('^' . '/' . ltrim(trim($route->getRegex()), '/') . '/?$'); $route->setRegex('/' . str_replace('/', '\\/', $route->getRegex()) . '/'); if (preg_match($route->getRegex(), $this->request->getPathInfo())) { // We have a match $candidates[] = $route; } } return $candidates; }
/** * Loads RouteCollection from source * * @param string $source Path to resource * @return RouteCollection */ public function loadRouteCollection($source = null) { $collection = new RouteCollection(); $routes = $this->loadResource($source); foreach ($routes as $name => $route) { $collection->add($name, $this->parseRoute($route)); } return $collection; }
public function testAll() { $routeCollection = new RouteCollection(); $expected = array(); $routes = array($route1 = new Route('/', array('GET'), 'Orders', 'getAll'), $route2 = new Route('/products', array('GET', 'POST'), 'Producs', 'get'), $route3 = new Route(null, array('PUT'), 'Home', 'getHome')); foreach ($routes as $route) { $routeCollection->add($route->getCalls(), $route); $expected[$route->getCalls()] = $route; } $this->assertSame($routeCollection->all(), $expected); }