Further modules included.
[yaffs-website] / web / modules / contrib / drupalmoduleupgrader / src / Routing / Drupal8 / RouteWrapper.php
1 <?php
2
3 /**
4  * @file
5  * Contains \Drupal\drupalmoduleupgrader\Converter\Routing\Drupal8\Route.
6  */
7
8 namespace Drupal\drupalmoduleupgrader\Routing\Drupal8;
9
10 use Drupal\Core\Routing\RouteProviderInterface;
11 use Drupal\drupalmoduleupgrader\Routing\RouterBuiltEvent;
12 use Drupal\drupalmoduleupgrader\Routing\RouteWrapperInterface;
13 use Drupal\drupalmoduleupgrader\Utility\Path\Drupal8\PathUtility;
14 use Symfony\Component\Routing\Route;
15
16 /**
17  * Wraps around a Symfony Route object, providing helper methods.
18  */
19 class RouteWrapper implements RouteWrapperInterface {
20
21   /**
22    * @var string
23    */
24   protected $name;
25
26   /**
27    * @var \Symfony\Component\Routing\Route
28    */
29   protected $route;
30
31   /**
32    * @var \Drupal\drupalmoduleupgrader\Utility\Path\Drupal8\PathUtility
33    */
34   protected $path;
35
36   /**
37    * @var \Drupal\Core\Routing\RouteProviderInterface
38    */
39   protected $routeProvider;
40
41   /**
42    * @var \Symfony\Component\Routing\RouteCollection
43    */
44   protected $router;
45
46   /**
47    * @var static
48    */
49   protected $parent;
50
51   /**
52    * Constructs a Route object.
53    */
54   public function __construct($name, Route $route, RouteProviderInterface $route_provider) {
55     $this->name = $name;
56     $this->route = $route;
57     $this->routeProvider = $route_provider ? $route_provider: \Drupal::service('router.route_provider');
58     $this->path = new PathUtility($route->getPath());
59   }
60
61   /**
62    * Forwards unknown function calls to the wrapped Route.
63    */
64   public function __call($method, array $arguments) {
65     return call_user_func_array([ $this->route, $method ], $arguments);
66   }
67
68   /**
69    * {@inheritdoc}
70    */
71   public function getIdentifier() {
72     return $this->name;
73   }
74
75   /**
76    * {@inheritdoc}
77    */
78   public function getPath() {
79     return $this->path;
80   }
81
82   /**
83    * {@inheritdoc}
84    */
85   public function hasParent() {
86     return isset($this->parent);
87   }
88
89   /**
90    * {@inheritdoc}
91    */
92   public function getParent() {
93     return $this->parent;
94   }
95
96   /**
97    * {@inheritdoc}
98    */
99   public function unwrap() {
100     return $this->route;
101   }
102
103   /**
104    * {@inheritdoc}
105    */
106   public function onRouterBuilt(RouterBuiltEvent $event) {
107     $this->router = $event->getRouter();
108
109     try {
110       $parent = $this->getPath()->getParent()->__toString();
111     }
112     catch (\LengthException $e) {
113       return;
114     }
115
116     // First, search the injected router for the parent route.
117     foreach ($this->router as $route) {
118       if ($route->getPath() == $parent) {
119         $this->parent = $route;
120       }
121     }
122
123     // Next, search the core route provider if no parent was found.
124     if (empty($this->parent)) {
125       $parents = $this->routeProvider->getRoutesByPattern($parent)->getIterator();
126
127       if (sizeof($parents) > 0) {
128         $this->parent = new static($parents->key(), $parents->current(), $this->routeProvider);
129       }
130     }
131   }
132
133 }