Further modules included.
[yaffs-website] / web / modules / contrib / drupalmoduleupgrader / src / Routing / Drupal7 / RouteWrapper.php
1 <?php
2
3 /**
4  * @file
5  * Contains \Drupal\drupalmoduleupgrader\Converter\Routing\Drupal7\Route.
6  */
7
8 namespace Drupal\drupalmoduleupgrader\Routing\Drupal7;
9
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;
14
15 /**
16  * Encapsulates a Drupal 7 route (including the link, if any).
17  */
18 class RouteWrapper extends ArrayCollection implements RouteWrapperInterface {
19
20   /**
21    * @var \Drupal\drupalmoduleupgrader\Utility\Path\Drupal7\PathUtility
22    */
23   protected $path;
24
25   /**
26    * @var \Drupal\drupalmoduleupgrader\Routing\RouterInterface
27    */
28   protected $router;
29
30   /**
31    * @var static|NULL
32    */
33   protected $parent;
34
35   /**
36    * @var \Drupal\drupalmoduleupgrader\Routing\Drupal7\Router
37    */
38   protected $children;
39
40   /**
41    * @var \Drupal\drupalmoduleupgrader\Routing\Drupal7\Router
42    */
43   protected $siblings;
44
45   /**
46    * Constructs a Route object.
47    */
48   public function __construct($path, array $item) {
49     $this->path = new PathUtility($path);
50
51     // Merge in hook_menu() defaults to normalize things.
52     $item += [
53       'title callback' => 't',
54       'title arguments' => [],
55       'access callback' => 'user_access',
56       'access arguments' => [],
57       'page arguments' => [],
58       'type' => 'MENU_NORMAL_ITEM',
59     ];
60     parent::__construct($item);
61   }
62
63   /**
64    * {@inheritdoc}
65    */
66   public function getIdentifier() {
67     return $this->getPath()->__toString();
68   }
69
70   /**
71    * {@inheritdoc}
72    */
73   public function getPath() {
74     return $this->path;
75   }
76
77   /**
78    * {@inheritdoc}
79    */
80   public function hasParent() {
81     return isset($this->parent);
82   }
83
84   /**
85    * {@inheritdoc}
86    */
87   public function getParent() {
88     return $this->parent;
89   }
90
91   /**
92    * {@inheritdoc}
93    */
94   public function unwrap() {
95     return $this->toArray();
96   }
97
98   /**
99    * {@inheritdoc}
100    */
101   public function onRouterBuilt(RouterBuiltEvent $event) {
102     $this->router = $event->getRouter();
103
104     $my_path = $this->getPath();
105     $my_length = sizeof($my_path);
106     $my_path = (string) $my_path;
107
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);
117       })
118       ->ofType('MENU_LOCAL_TASK, MENU_DEFAULT_LOCAL_TASK, MENU_LOCAL_ACTION');
119
120     try {
121       $parent = $this->getPath()->getParent();
122       $this->parent = $this->router->get($parent->__toString());
123     }
124     catch (\LengthException $e) {
125       // Because there's no parent path, we can't effectively search for siblings.
126       // Time to die.
127       return;
128     }
129
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);
135       });
136   }
137
138   /**
139    * Returns if this route has an absolute access flag (TRUE or FALSE).
140    *
141    * @return boolean
142    */
143   public function isAbsoluteAccess() {
144     return is_bool($this->get('access callback'));
145   }
146
147   /**
148    * Returns if this route has permission-based access.
149    *
150    * @return boolean
151    */
152   public function isPermissionBased() {
153     return ($this->get('access callback') == 'user_access');
154   }
155
156   /**
157    * Returns if this route exposes a link of any kind.
158    *
159    * @return boolean
160    */
161   public function hasLink() {
162     return ($this->isLink() || $this->isLocalTask() || $this->isDefaultLocalTask() || $this->isLocalAction());
163   }
164
165   /**
166    * Returns if this route is a normal link.
167    *
168    * @return boolean
169    */
170   public function isLink() {
171     return $this->get('type') == 'MENU_NORMAL_ITEM';
172   }
173
174   /**
175    * Returns if this route is a local task (NOT a default local task).
176    *
177    * @return boolean
178    */
179   public function isLocalTask() {
180     return $this->get('type') == 'MENU_LOCAL_TASK';
181   }
182
183   /**
184    * Gets the closest default local task, if there is one.
185    *
186    * @return static|NULL
187    */
188   public function getDefaultTask() {
189     if ($this->hasSiblings()) {
190       return $this->getSiblings()->ofType('MENU_DEFAULT_LOCAL_TASK')->first();
191     }
192   }
193
194   /**
195    * Returns if this route is a default local task.
196    *
197    * @return boolean
198    */
199   public function isDefaultLocalTask() {
200     return $this->get('type') == 'MENU_DEFAULT_LOCAL_TASK';
201   }
202
203   /**
204    * Returns if this route is a local action.
205    *
206    * @return boolean
207    */
208   public function isLocalAction() {
209     return $this->get('type') == 'MENU_LOCAL_ACTION';
210   }
211
212   /**
213    * Returns if this route is a contextual link.
214    *
215    * @return boolean
216    */
217   public function isContextualLink() {
218     return ($this->isLocalAction() && $this->containsKey('context') && $this->get('context') == 'MENU_CONTEXT_INLINE');
219   }
220
221   /**
222    * Returns if this route has children.
223    *
224    * @return boolean
225    */
226   public function hasChildren() {
227     return $this->getChildren()->count() > 0;
228   }
229
230   /**
231    * Returns the immediate children of this route.
232    *
233    * @return \Drupal\drupalmoduleupgrader\Routing\Drupal7\Router
234    */
235   public function getChildren() {
236     return $this->children;
237   }
238
239   /**
240    * Returns if this route has siblings.
241    *
242    * @return boolean
243    */
244   public function hasSiblings() {
245     return $this->getSiblings()->count() > 0;
246   }
247
248   /**
249    * Gets the siblings of this route.
250    *
251    * @return \Drupal\drupalmoduleupgrader\Routing\Drupal7\Router
252    */
253   public function getSiblings() {
254     return $this->siblings;
255   }
256
257 }