public function load($resource, $type = null) { if (true === $this->loaded) { throw new \RuntimeException('Do not add the "zikularoutesmodule" loader twice'); } unset($type); $routeCollection = new RouteCollection(); try { // clear entity manager to ensure that we also fetch new routes which have been newly inserted during a module's installation $this->em->clear(); // fetch all approved routes $routes = $this->em->getRepository('ZikulaRoutesModule:RouteEntity')->findBy(array('workflowState' => 'approved'), array('group' => 'ASC', 'sort' => 'ASC')); } catch (DBALException $e) { // It seems like the module is not yet installed. Fail silently. return $routeCollection; } if (!empty($routes)) { $helper = new RoutingUtil(); /** * @var \Zikula\RoutesModule\Entity\RouteEntity $dbRoute */ foreach ($routes as $dbRoute) { // Add modname, type and func to the route's default values. $defaults = $dbRoute->getDefaults(); $defaults['_zkModule'] = $dbRoute->getBundle(); list(, $type) = $helper->sanitizeController($dbRoute->getController()); list(, $func) = $helper->sanitizeAction($dbRoute->getAction()); $defaults['_zkType'] = $type; $defaults['_zkFunc'] = $func; // We have to prepend the bundle prefix if // - routes are _not_ currently extracted via the command line and // - the route has i18n set to false. // This is because when extracting the routes, a bundle author only wants to translate the bare route // patterns, without a redundant and potentially customized bundle prefix in front of them. // If i18n is set to true, Zikula's customized pattern generation strategy will take care of it. // See Zikula\RoutesModule\Translation\ZikulaPatternGenerationStrategy $options = $dbRoute->getOptions(); $prependBundle = !isset($GLOBALS['translation_extract_routes']) && isset($options['i18n']) && !$options['i18n']; if ($prependBundle) { $path = $dbRoute->getPathWithBundlePrefix($this->container); } else { $path = $dbRoute->getPath(); } $route = new Route($path, $defaults, $dbRoute->getRequirements(), $options, $dbRoute->getHost(), $dbRoute->getSchemes(), $dbRoute->getMethods(), $dbRoute->getCondition()); $routeCollection->add($dbRoute->getName(), $route); } } $this->loaded = true; // We would need a DatabaseResource or similar, which does not exist in Symfony (yet). // See https://github.com/symfony/symfony/issues/7176 // $routeCollection->addResource(new FileResource()) return $routeCollection; }
private function convertInputToRouteSettings($entityData) { $routingHelper = new RoutingUtil(); $bundle = $entityData['bundle']; list($controller, $type) = $routingHelper->sanitizeController($entityData['controller']); list($action, $func) = $routingHelper->sanitizeAction($entityData['action']); $entityData['controller'] = $controller; $entityData['action'] = $action; $controllerNamespace = $this->getControllerClass($bundle, $type); $entityData['defaults']['_controller'] = "{$controllerNamespace}::{$action}"; if (empty($entityData['name'])) { // This route is newly created and not edited. $routeName = strtolower($bundle . "_" . $type . "_" . substr($action, 0, -6)); // Check if name already exists. $dbRoute = $this->entityManager->getRepository('ZikulaRoutesModule:RouteEntity')->findOneBy(array('name' => $routeName)); if ($dbRoute) { // This name already exists. We need to query the database using regexp. This function needs to be manually added. $config = $this->entityManager->getConfiguration(); $config->addCustomStringFunction('REGEXP', 'Zikula\\RoutesModule\\Util\\Regexp'); // Get all entities with the same routename having a prefix. $query = $this->entityManager->createQuery('SELECT r.name FROM ZikulaRoutesModule:RouteEntity r WHERE REGEXP(r.name, :regexp) = 1'); $query->setParameter('regexp', '^' . preg_quote($routeName) . '_[[:digit:]]*$'); $results = $query->getArrayResult(); $highestSuffix = 0; foreach ($results as $result) { $name = explode('_', $result['name']); $suffix = (int) $name[count($name) - 1]; $highestSuffix = $suffix > $highestSuffix ? $suffix : $highestSuffix; } $highestSuffix += 1; $routeName .= "_{$highestSuffix}"; } $entityData['name'] = $routeName; } $entityData['options']['i18n'] = $entityData['i18n']; $entityData['options']['zkNoBundlePrefix'] = !$entityData['bundlePrefix']; unset($entityData['i18n'], $entityData['bundlePrefix']); $entityData['group'] = RouteEntity::POSITION_MIDDLE; $entityData['sort'] = 0; $entityData['userRoute'] = true; return $entityData; }