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

Encapsulates a Drupal 7 route (including the link, if any).

Hierarchy

Expanded class hierarchy of RouteWrapper

10 files declare their use of RouteWrapper
ContentRoute.php in src/Plugin/DMU/Routing/ContentRoute.php
FormRoute.php in src/Plugin/DMU/Routing/FormRoute.php
HookMenu.php in src/Routing/HookMenu.php
LinkBinding.php in src/Routing/LinkBinding/LinkBinding.php
LinkBindingFactory.php in src/Routing/LinkBinding/LinkBindingFactory.php

... See full list

File

src/Routing/Drupal7/RouteWrapper.php, line 13

Namespace

Drupal\drupalmoduleupgrader\Routing\Drupal7
View source
class RouteWrapper extends ArrayCollection implements RouteWrapperInterface {

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

  /**
   * @var \Drupal\drupalmoduleupgrader\Routing\RouterInterface
   */
  protected $router;

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

  /**
   * @var \Drupal\drupalmoduleupgrader\Routing\Drupal7\Router
   */
  protected $children;

  /**
   * @var \Drupal\drupalmoduleupgrader\Routing\Drupal7\Router
   */
  protected $siblings;

  /**
   * Constructs a Route object.
   */
  public function __construct($path, array $item) {
    $this->path = new PathUtility($path);

    // Merge in hook_menu() defaults to normalize things.
    $item += [
      'title callback' => 't',
      'title arguments' => [],
      'access callback' => 'user_access',
      'access arguments' => [],
      'page arguments' => [],
      'type' => 'MENU_NORMAL_ITEM',
    ];
    parent::__construct($item);
  }

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

  /**
   * {@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
      ->toArray();
  }

  /**
   * {@inheritdoc}
   */
  public function onRouterBuilt(RouterBuiltEvent $event) {
    $this->router = $event
      ->getRouter();
    $my_path = $this
      ->getPath();
    $my_length = sizeof($my_path);
    $my_path = (string) $my_path;

    // If trying to get the parent raises an exception, we're going to
    // bail out. But we don't need the parent in order to find our own
    // children, so search for them before searching for the parent.
    $this->children = $this->router
      ->filter(function (RouteWrapper $route) use ($my_path, $my_length) {
      $path = $route
        ->getPath();

      // <WTF>$path needs to be explicitly cast to a string, 'cause strPos() won't do
      // it, even though trim() and similar functions will.</WTF>
      return sizeof($path) == $my_length + 1 && strpos((string) $path, $my_path) === 0;
    })
      ->ofType('MENU_LOCAL_TASK, MENU_DEFAULT_LOCAL_TASK, MENU_LOCAL_ACTION');
    try {
      $parent = $this
        ->getPath()
        ->getParent();
      $this->parent = $this->router
        ->get($parent
        ->__toString());
    } catch (\LengthException $e) {

      // Because there's no parent path, we can't effectively search for siblings.
      // Time to die.
      return;
    }
    $this->siblings = $this->router
      ->filter(function (RouteWrapper $route) use ($parent, $my_path, $my_length) {
      $path = $route
        ->getPath();

      // <WTF>strPos(), <sarcasm>in its wisdom</sarcasm>, won't cast to string.</WTF>
      return $path !== $my_path && sizeof($path) == $my_length && strpos((string) $path, (string) $parent) === 0;
    });
  }

  /**
   * Returns if this route has an absolute access flag (TRUE or FALSE).
   *
   * @return bool
   */
  public function isAbsoluteAccess() {
    return is_bool($this
      ->get('access callback'));
  }

  /**
   * Returns if this route has permission-based access.
   *
   * @return bool
   */
  public function isPermissionBased() {
    return $this
      ->get('access callback') == 'user_access';
  }

  /**
   * Returns if this route exposes a link of any kind.
   *
   * @return bool
   */
  public function hasLink() {
    return $this
      ->isLink() || $this
      ->isLocalTask() || $this
      ->isDefaultLocalTask() || $this
      ->isLocalAction();
  }

  /**
   * Returns if this route is a normal link.
   *
   * @return bool
   */
  public function isLink() {
    return $this
      ->get('type') == 'MENU_NORMAL_ITEM';
  }

  /**
   * Returns if this route is a local task (NOT a default local task).
   *
   * @return bool
   */
  public function isLocalTask() {
    return $this
      ->get('type') == 'MENU_LOCAL_TASK';
  }

  /**
   * Gets the closest default local task, if there is one.
   *
   * @return static|null
   */
  public function getDefaultTask() {
    if ($this
      ->hasSiblings()) {
      return $this
        ->getSiblings()
        ->ofType('MENU_DEFAULT_LOCAL_TASK')
        ->first();
    }
  }

  /**
   * Returns if this route is a default local task.
   *
   * @return bool
   */
  public function isDefaultLocalTask() {
    return $this
      ->get('type') == 'MENU_DEFAULT_LOCAL_TASK';
  }

  /**
   * Returns if this route is a local action.
   *
   * @return bool
   */
  public function isLocalAction() {
    return $this
      ->get('type') == 'MENU_LOCAL_ACTION';
  }

  /**
   * Returns if this route is a contextual link.
   *
   * @return bool
   */
  public function isContextualLink() {
    return $this
      ->isLocalAction() && $this
      ->containsKey('context') && $this
      ->get('context') == 'MENU_CONTEXT_INLINE';
  }

  /**
   * Returns if this route has children.
   *
   * @return bool
   */
  public function hasChildren() {
    return $this
      ->getChildren()
      ->count() > 0;
  }

  /**
   * Returns the immediate children of this route.
   *
   * @return \Drupal\drupalmoduleupgrader\Routing\Drupal7\Router
   */
  public function getChildren() {
    return $this->children;
  }

  /**
   * Returns if this route has siblings.
   *
   * @return bool
   */
  public function hasSiblings() {
    return (bool) $this
      ->getSiblings() > 0;
  }

  /**
   * Gets the siblings of this route.
   *
   * @return \Drupal\drupalmoduleupgrader\Routing\Drupal7\Router
   */
  public function getSiblings() {
    return $this->siblings;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
RouteWrapper::$children protected property
RouteWrapper::$parent protected property
RouteWrapper::$path protected property
RouteWrapper::$router protected property
RouteWrapper::$siblings protected property
RouteWrapper::getChildren public function Returns the immediate children of this route.
RouteWrapper::getDefaultTask public function Gets the closest default local task, if there is one.
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::getSiblings public function Gets the siblings of this route.
RouteWrapper::hasChildren public function Returns if this route has children.
RouteWrapper::hasLink public function Returns if this route exposes a link of any kind.
RouteWrapper::hasParent public function Returns if this route has a parent. Overrides RouteWrapperInterface::hasParent
RouteWrapper::hasSiblings public function Returns if this route has siblings.
RouteWrapper::isAbsoluteAccess public function Returns if this route has an absolute access flag (TRUE or FALSE).
RouteWrapper::isContextualLink public function Returns if this route is a contextual link.
RouteWrapper::isDefaultLocalTask public function Returns if this route is a default local task.
RouteWrapper::isLink public function Returns if this route is a normal link.
RouteWrapper::isLocalAction public function Returns if this route is a local action.
RouteWrapper::isLocalTask public function Returns if this route is a local task (NOT a default local task).
RouteWrapper::isPermissionBased public function Returns if this route has permission-based access.
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::__construct public function Constructs a Route object.