/** * @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; }