5 * Contains \Drupal\drupalmoduleupgrader\Converter\Routing\Drupal7\Route.
8 namespace Drupal\drupalmoduleupgrader\Routing\Drupal7;
10 use Doctrine\Common\Collections\ArrayCollection;
11 use Drupal\drupalmoduleupgrader\Routing\RouterBuiltEvent;
12 use Drupal\drupalmoduleupgrader\Routing\RouteWrapperInterface;
13 use Drupal\drupalmoduleupgrader\Utility\Path\Drupal7\PathUtility;
16 * Encapsulates a Drupal 7 route (including the link, if any).
18 class RouteWrapper extends ArrayCollection implements RouteWrapperInterface {
21 * @var \Drupal\drupalmoduleupgrader\Utility\Path\Drupal7\PathUtility
26 * @var \Drupal\drupalmoduleupgrader\Routing\RouterInterface
36 * @var \Drupal\drupalmoduleupgrader\Routing\Drupal7\Router
41 * @var \Drupal\drupalmoduleupgrader\Routing\Drupal7\Router
46 * Constructs a Route object.
48 public function __construct($path, array $item) {
49 $this->path = new PathUtility($path);
51 // Merge in hook_menu() defaults to normalize things.
53 'title callback' => 't',
54 'title arguments' => [],
55 'access callback' => 'user_access',
56 'access arguments' => [],
57 'page arguments' => [],
58 'type' => 'MENU_NORMAL_ITEM',
60 parent::__construct($item);
66 public function getIdentifier() {
67 return $this->getPath()->__toString();
73 public function getPath() {
80 public function hasParent() {
81 return isset($this->parent);
87 public function getParent() {
94 public function unwrap() {
95 return $this->toArray();
101 public function onRouterBuilt(RouterBuiltEvent $event) {
102 $this->router = $event->getRouter();
104 $my_path = $this->getPath();
105 $my_length = sizeof($my_path);
106 $my_path = (string) $my_path;
108 // If trying to get the parent raises an exception, we're going to
109 // bail out. But we don't need the parent in order to find our own
110 // children, so search for them before searching for the parent.
111 $this->children = $this->router
112 ->filter(function(RouteWrapper $route) use ($my_path, $my_length) {
113 $path = $route->getPath();
114 // <WTF>$path needs to be explicitly cast to a string, 'cause strPos() won't do
115 // it, even though trim() and similar functions will.</WTF>
116 return (sizeof($path) == ($my_length + 1) && strPos((string) $path, $my_path) === 0);
118 ->ofType('MENU_LOCAL_TASK, MENU_DEFAULT_LOCAL_TASK, MENU_LOCAL_ACTION');
121 $parent = $this->getPath()->getParent();
122 $this->parent = $this->router->get($parent->__toString());
124 catch (\LengthException $e) {
125 // Because there's no parent path, we can't effectively search for siblings.
130 $this->siblings = $this->router
131 ->filter(function(RouteWrapper $route) use ($parent, $my_path, $my_length) {
132 $path = $route->getPath();
133 // <WTF>strPos(), <sarcasm>in its wisdom</sarcasm>, won't cast to string.</WTF>
134 return ($path !== $my_path && sizeof($path) == $my_length && strPos((string) $path, (string) $parent) === 0);
139 * Returns if this route has an absolute access flag (TRUE or FALSE).
143 public function isAbsoluteAccess() {
144 return is_bool($this->get('access callback'));
148 * Returns if this route has permission-based access.
152 public function isPermissionBased() {
153 return ($this->get('access callback') == 'user_access');
157 * Returns if this route exposes a link of any kind.
161 public function hasLink() {
162 return ($this->isLink() || $this->isLocalTask() || $this->isDefaultLocalTask() || $this->isLocalAction());
166 * Returns if this route is a normal link.
170 public function isLink() {
171 return $this->get('type') == 'MENU_NORMAL_ITEM';
175 * Returns if this route is a local task (NOT a default local task).
179 public function isLocalTask() {
180 return $this->get('type') == 'MENU_LOCAL_TASK';
184 * Gets the closest default local task, if there is one.
186 * @return static|NULL
188 public function getDefaultTask() {
189 if ($this->hasSiblings()) {
190 return $this->getSiblings()->ofType('MENU_DEFAULT_LOCAL_TASK')->first();
195 * Returns if this route is a default local task.
199 public function isDefaultLocalTask() {
200 return $this->get('type') == 'MENU_DEFAULT_LOCAL_TASK';
204 * Returns if this route is a local action.
208 public function isLocalAction() {
209 return $this->get('type') == 'MENU_LOCAL_ACTION';
213 * Returns if this route is a contextual link.
217 public function isContextualLink() {
218 return ($this->isLocalAction() && $this->containsKey('context') && $this->get('context') == 'MENU_CONTEXT_INLINE');
222 * Returns if this route has children.
226 public function hasChildren() {
227 return $this->getChildren()->count() > 0;
231 * Returns the immediate children of this route.
233 * @return \Drupal\drupalmoduleupgrader\Routing\Drupal7\Router
235 public function getChildren() {
236 return $this->children;
240 * Returns if this route has siblings.
244 public function hasSiblings() {
245 return $this->getSiblings()->count() > 0;
249 * Gets the siblings of this route.
251 * @return \Drupal\drupalmoduleupgrader\Routing\Drupal7\Router
253 public function getSiblings() {
254 return $this->siblings;