public function testGetRouteCollectionForRequest() { $request = Request::create('/my/path'); $candidates = array('/my/path', '/my', '/'); $this->candidatesMock->expects($this->once())->method('getCandidates')->with($request)->will($this->returnValue($candidates)); $this->routeMock->expects($this->once())->method('getName')->will($this->returnValue('/my/path')); $this->route2Mock->expects($this->once())->method('getName')->will($this->returnValue('/my')); $objects = array($this->routeMock, $this->route2Mock); $this->objectRepositoryMock->expects($this->once())->method('findByStaticPrefix')->with($candidates, array('position' => 'ASC'))->will($this->returnValue($objects)); $routeProvider = new RouteProvider($this->managerRegistryMock, $this->candidatesMock, 'Route'); $collection = $routeProvider->getRouteCollectionForRequest($request); $this->assertInstanceOf('Symfony\\Component\\Routing\\RouteCollection', $collection); $this->assertCount(2, $collection); }
protected function createRoute($name, $path) { // split path in static and variable part preg_match('{^(.*?)(/[^/]*\\{.*)?$}', $path, $paths); $route = new Route(); $route->setName($name); $route->setStaticPrefix($paths[1]); if (isset($paths[2])) { $route->setVariablePattern($paths[2]); } $this->getDm()->persist($route); $this->getDm()->flush(); return $route; }
/** * Create a 410 route * * @param Route $route * * @return Route */ protected function deletedRoute(Route $route) { $route->setDefault('_controller', 'FrameworkBundle:Redirect:redirect'); $route->setDefault('path', ''); $route->setDefault('permanent', true); }
public function buildRouteName(Route $route) { $newPathName = str_replace(['/', '-', '.'], '_', ltrim($route->getPath(), '/')); return 'gravity_node_' . $newPathName; }
/** * @param FieldableEntity $entity * @param bool $nonConflict * * @return Route */ public function build(FieldableEntity $entity, $nonConflict = true) { $class = get_class($entity); $routeMapping = $this->nodeRouteManager->getNodeMapping($class); $router = $this->nodeRouteManager->getRouter(); $i = 0; $path = null; $fullPath = $entity->getPath(); $route = new Route(); $route->setVariablePattern(""); if ($fullPath) { $urlVars = []; $route->setStaticPrefix($fullPath); } else { $route->setStaticPrefix($routeMapping['path']); $compiledRoute = $route->compile(); $urlVars = $compiledRoute->getPathVariables(); } $routeName = 'node_route'; $route->setName($routeName); $routeCollection = new RouteCollection(); $routeCollection->add($routeName, $route); $urlGenerator = new UrlGenerator($routeCollection, new RequestContext('')); $urlVarCount = count($urlVars) - 1; while (true) { $params = []; foreach ($urlVars as $vi => $var) { $method = new \ReflectionMethod($class, "get{$var}"); $value = (string) $method->invoke($entity); // if ($i > 0 && $urlVarCount == $vi) { $value .= " {$i}"; } $params[$var] = $this->slugger->slugify($value); } if ($fullPath) { $path = $this->slugify($fullPath, $i); } else { $path = $urlGenerator->generate($routeName, $params); } // if we don't care about conflicts, skip if (!$nonConflict) { break; } ++$i; try { $match = $router->match($path); // if we've fallen back to the original route, stop searching for an existing route, or if we've found // the node's route if (isset($match['nodeId'])) { if ($match['nodeId'] != $entity->getId()) { continue; } else { break; } } else { break; } } catch (ResourceNotFoundException $e) { break; } } $route = new Route(); $route->setDefaults(['_format' => 'html', '_controller' => 'Gravity\\CmsBundle\\Controller\\NodeController::viewAction', 'nodeId' => $entity->getId(), 'type' => $class]); $route->setOptions(['add_format_pattern' => true]); $route->setName($this->buildRouteName($path)); $route->setStaticPrefix($path); $route->setPath($path); $entity->setPath($path); return $route; }
/** * Redirect a route to a new url * * @param Route $route * @param string $newPath * * @return \Symfony\Component\Routing\Route */ protected function redirectRoute(Route $route, $newPath) { return $route->setDefaults(['_controller' => 'FrameworkBundle:Redirect:urlRedirect', 'path' => $newPath, 'permanent' => true]); }