/** * Get the path for a given route * * This looks up the route by its passed name and returns * the path/url for that route, with its URL params as * placeholders unless you pass a valid key-value pair array * of the placeholder params and their values * * If a pathname is a complex/custom regular expression, this * method will simply return the regular expression used to * match the request pathname, unless an optional boolean is * passed "flatten_regex" which will flatten the regular * expression into a simple path string * * This method, and its style of reverse-compilation, was originally * inspired by a similar effort by Gilles Bouthenot (@gbouthenot) * * @link https://github.com/gbouthenot * * @param string $route_name The name of the route * @param array $params The array of placeholder fillers * @param boolean $flatten_regex Optionally flatten custom regular expressions to "/" * * @throws OutOfBoundsException If the route requested doesn't exist * @return string */ public function getPathFor($route_name, array $params = null, $flatten_regex = true) { // First, grab the route $route = $this->routes->get($route_name); // Make sure we are getting a valid route if (null === $route) { throw new OutOfBoundsException('No such route with name: ' . $route_name); } $path = $route->getPath(); if (preg_match_all(static::ROUTE_COMPILE_REGEX, $path, $matches, PREG_SET_ORDER)) { foreach ($matches as $match) { list($block, $pre, $inner_block, $type, $param, $optional) = $match; if (isset($params[$param])) { $path = str_replace($block, $pre . $params[$param], $path); } elseif ($optional) { $path = str_replace($block, '', $path); } } } elseif ($flatten_regex && strpos($path, '@') === 0) { // get the parent path preg_match('#^\\@\\^(?<parentPath>.*)\\.\\*#', $path, $matches); $parentPath = isset($matches['parentPath']) ? $matches['parentPath'] : ''; // If the path is a custom regular expression and we're //"flattening", just return the parent path and a slash $path = $parentPath . '/'; } return $path; }
/** * @dataProvider sampleDataProvider */ public function testRouteOrderDoesntChangeAfterPreparing() { // Get the provided data dynamically $array_of_routes = func_get_args(); // Set the number of times we should loop $loop_num = 10; // Loop a set number of times to check different permutations for ($i = 0; $i < $loop_num; $i++) { // Shuffle the sample routes array shuffle($array_of_routes); // Create our collection and prepare the routes $routes = new RouteCollection($array_of_routes); $routes->prepareNamed(); $this->assertSame(array_values($routes->all()), array_values($array_of_routes)); } }