target = $target; $this->routeConverters = $route_converters; // If the hook_menu() implementation doesn't exist, get the implementation // from the indexer and eval it into existence. It's the calling code's // responsibility to ensure that the implementation doesn't contain anything // which will blow up on execution. $hook = $target->id() . '_menu'; if (! function_exists($hook)) { eval($target->getIndexer('function')->get('hook_menu')->getText()); } } /** * Returns the collection of routes in the source. * * @return RouterInterface * The requested link collection. */ public function getSourceRoutes() { if (empty($this->sourceRoutes)) { $this->sourceRoutes = new Drupal7Router(); $items = call_user_func($this->target->id() . '_menu'); foreach ($items as $path => $item) { $this->sourceRoutes->addRoute(new Drupal7Route($path, $item)); } // Now that all routes have been loaded, tell them to resolve their // hierarchical relationships. $this->sourceRoutes->finalize(); } return $this->sourceRoutes; } /** * Returns the collection of routes in the destination. * * @return RouterInterface * The requested route collection. */ public function getDestinationRoutes() { if (empty($this->destinationRoutes)) { $this->destinationRoutes = $this->buildDestinationRoutes(); } return $this->destinationRoutes; } /** * Returns the destination route for the given source path. * * @param string $path * The source path, as defined in hook_menu(). * * @return \Drupal\drupalmoduleupgrader\Routing\Drupal8\RouteWrapper|NULL * The destination route. */ public function getDestinationRoute($path) { return $this->getDestinationRoutes()->get($this->routeMap[$path]); } /** * Builds the Drupal 8 router by running the Drupal 7 router items through * the appropriate route converters. * * @return RouterInterface */ private function buildDestinationRoutes() { // @todo These are currently hardcoded on the D7 -> D8 conversion. Make this // configurable. $router = new Drupal8Router(); $this->routeMap = []; foreach ($this->getSourceRoutes() as $path => $route) { /** @var Drupal7\RouteWrapper $route */ // If the route hasn't got a page callback...don't even try. if (!$route->containsKey('page callback')) { continue; } // Get the appropriate route converter, which will build the route // definition. $plugin_id = $route['page callback']; if (!$this->routeConverters->hasDefinition($plugin_id)) { $plugin_id = 'default'; } /** @var Drupal8\RouteWrapper $d8_route */ $d8_route = $this->routeConverters->createInstance($plugin_id)->buildRouteDefinition($this->target, $route); $router->addRoute($d8_route); $this->routeMap[$path] = $d8_route->getIdentifier(); } $router->finalize(); foreach ($this->getSourceRoutes()->getDefaultLocalTasks() as $path => $route) { /** @var Drupal7\RouteWrapper $route */ if ($route->hasParent()) { $parent = (string) $route->getParent()->getPath(); $this->routeMap[$path] = $this->routeMap[$parent]; } } return $router; } }