You are here

class RouteWrapper in Drupal 7 to 8/9 Module Upgrader 8

Same name in this branch
  1. 8 src/Routing/Drupal7/RouteWrapper.php \Drupal\drupalmoduleupgrader\Routing\Drupal7\RouteWrapper
  2. 8 src/Routing/Drupal8/RouteWrapper.php \Drupal\drupalmoduleupgrader\Routing\Drupal8\RouteWrapper

Wraps around a Symfony Route object, providing helper methods.

Hierarchy

Expanded class hierarchy of RouteWrapper

7 files declare their use of RouteWrapper
ContentRoute.php in src/Plugin/DMU/Routing/ContentRoute.php
LinkBinding.php in src/Routing/LinkBinding/LinkBinding.php
LinkBindingFactory.php in src/Routing/LinkBinding/LinkBindingFactory.php
LinkBindingTest.php in tests/src/Unit/Routing/LinkBinding/LinkBindingTest.php
LocalTaskLinkBinding.php in src/Routing/LinkBinding/LocalTaskLinkBinding.php

... See full list

File

src/Routing/Drupal8/RouteWrapper.php, line 14

Namespace

Drupal\drupalmoduleupgrader\Routing\Drupal8
View source
class RouteWrapper implements RouteWrapperInterface {

  /**
   * @var string
   */
  protected $name;

  /**
   * @var \Symfony\Component\Routing\Route
   */
  protected $route;

  /**
   * @var \Drupal\drupalmoduleupgrader\Utility\Path\Drupal8\PathUtility
   */
  protected $path;

  /**
   * @var \Drupal\Core\Routing\RouteProviderInterface
   */
  protected $routeProvider;

  /**
   * @var \Symfony\Component\Routing\RouteCollection
   */
  protected $router;

  /**
   * @var static
   */
  protected $parent;

  /**
   * Constructs a Route object.
   */
  public function __construct($name, Route $route, RouteProviderInterface $route_provider) {
    $this->name = $name;
    $this->route = $route;
    $this->routeProvider = $route_provider ? $route_provider : \Drupal::service('router.route_provider');
    $this->path = new PathUtility($route
      ->getPath());
  }

  /**
   * Forwards unknown function calls to the wrapped Route.
   */
  public function __call($method, array $arguments) {
    return call_user_func_array([
      $this->route,
      $method,
    ], $arguments);
  }

  /**
   * {@inheritdoc}
   */
  public function getIdentifier() {
    return $this->name;
  }

  /**
   * {@inheritdoc}
   */
  public function getPath() {
    return $this->path;
  }

  /**
   * {@inheritdoc}
   */
  public function hasParent() {
    return isset($this->parent);
  }

  /**
   * {@inheritdoc}
   */
  public function getParent() {
    return $this->parent;
  }

  /**
   * {@inheritdoc}
   */
  public function unwrap() {
    return $this->route;
  }

  /**
   * {@inheritdoc}
   */
  public function onRouterBuilt(RouterBuiltEvent $event) {
    $this->router = $event
      ->getRouter();
    try {
      $parent = $this
        ->getPath()
        ->getParent()
        ->__toString();
    } catch (\LengthException $e) {
      return;
    }

    // First, search the injected router for the parent route.
    foreach ($this->router as $route) {
      if ($route
        ->getPath() == $parent) {
        $this->parent = $route;
      }
    }

    // Next, search the core route provider if no parent was found.
    if (empty($this->parent)) {
      $parents = $this->routeProvider
        ->getRoutesByPattern($parent)
        ->getIterator();
      if (sizeof($parents) > 0) {
        $this->parent = new static($parents
          ->key(), $parents
          ->current(), $this->routeProvider);
      }
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
RouteWrapper::$name protected property
RouteWrapper::$parent protected property
RouteWrapper::$path protected property
RouteWrapper::$route protected property
RouteWrapper::$routeProvider protected property
RouteWrapper::$router protected property
RouteWrapper::getIdentifier public function Returns an identifier for this route. Overrides RouteWrapperInterface::getIdentifier
RouteWrapper::getParent public function Gets the parent route, if there is one. The parent should also be wrapped. Overrides RouteWrapperInterface::getParent
RouteWrapper::getPath public function Returns a PathUtilityInterface implementation for the route. Overrides RouteWrapperInterface::getPath
RouteWrapper::hasParent public function Returns if this route has a parent. Overrides RouteWrapperInterface::hasParent
RouteWrapper::onRouterBuilt public function React to the router (i.e., the collection of routes defined by the module) being completely built. Overrides RouteWrapperInterface::onRouterBuilt
RouteWrapper::unwrap public function Returns the original, unwrapped route. Overrides RouteWrapperInterface::unwrap
RouteWrapper::__call public function Forwards unknown function calls to the wrapped Route.
RouteWrapper::__construct public function Constructs a Route object.