RouteWrapper.php in Drupal 7 to 8/9 Module Upgrader 8
File
src/Routing/Drupal7/RouteWrapper.php
View source
<?php
namespace Drupal\drupalmoduleupgrader\Routing\Drupal7;
use Doctrine\Common\Collections\ArrayCollection;
use Drupal\drupalmoduleupgrader\Routing\RouterBuiltEvent;
use Drupal\drupalmoduleupgrader\Routing\RouteWrapperInterface;
use Drupal\drupalmoduleupgrader\Utility\Path\Drupal7\PathUtility;
class RouteWrapper extends ArrayCollection implements RouteWrapperInterface {
protected $path;
protected $router;
protected $parent;
protected $children;
protected $siblings;
public function __construct($path, array $item) {
$this->path = new PathUtility($path);
$item += [
'title callback' => 't',
'title arguments' => [],
'access callback' => 'user_access',
'access arguments' => [],
'page arguments' => [],
'type' => 'MENU_NORMAL_ITEM',
];
parent::__construct($item);
}
public function getIdentifier() {
return $this
->getPath()
->__toString();
}
public function getPath() {
return $this->path;
}
public function hasParent() {
return isset($this->parent);
}
public function getParent() {
return $this->parent;
}
public function unwrap() {
return $this
->toArray();
}
public function onRouterBuilt(RouterBuiltEvent $event) {
$this->router = $event
->getRouter();
$my_path = $this
->getPath();
$my_length = sizeof($my_path);
$my_path = (string) $my_path;
$this->children = $this->router
->filter(function (RouteWrapper $route) use ($my_path, $my_length) {
$path = $route
->getPath();
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) {
return;
}
$this->siblings = $this->router
->filter(function (RouteWrapper $route) use ($parent, $my_path, $my_length) {
$path = $route
->getPath();
return $path !== $my_path && sizeof($path) == $my_length && strpos((string) $path, (string) $parent) === 0;
});
}
public function isAbsoluteAccess() {
return is_bool($this
->get('access callback'));
}
public function isPermissionBased() {
return $this
->get('access callback') == 'user_access';
}
public function hasLink() {
return $this
->isLink() || $this
->isLocalTask() || $this
->isDefaultLocalTask() || $this
->isLocalAction();
}
public function isLink() {
return $this
->get('type') == 'MENU_NORMAL_ITEM';
}
public function isLocalTask() {
return $this
->get('type') == 'MENU_LOCAL_TASK';
}
public function getDefaultTask() {
if ($this
->hasSiblings()) {
return $this
->getSiblings()
->ofType('MENU_DEFAULT_LOCAL_TASK')
->first();
}
}
public function isDefaultLocalTask() {
return $this
->get('type') == 'MENU_DEFAULT_LOCAL_TASK';
}
public function isLocalAction() {
return $this
->get('type') == 'MENU_LOCAL_ACTION';
}
public function isContextualLink() {
return $this
->isLocalAction() && $this
->containsKey('context') && $this
->get('context') == 'MENU_CONTEXT_INLINE';
}
public function hasChildren() {
return $this
->getChildren()
->count() > 0;
}
public function getChildren() {
return $this->children;
}
public function hasSiblings() {
return (bool) $this
->getSiblings() > 0;
}
public function getSiblings() {
return $this->siblings;
}
}
Classes
Name |
Description |
RouteWrapper |
Encapsulates a Drupal 7 route (including the link, if any). |