Version 1
[yaffs-website] / web / core / lib / Drupal / Core / Routing / CurrentRouteMatch.php
1 <?php
2
3 namespace Drupal\Core\Routing;
4
5 use Symfony\Component\HttpFoundation\Request;
6 use Symfony\Component\HttpFoundation\RequestStack;
7
8 /**
9  * Default object for current_route_match service.
10  */
11 class CurrentRouteMatch implements ResettableStackedRouteMatchInterface {
12
13   /**
14    * The related request stack.
15    *
16    * @var \Symfony\Component\HttpFoundation\RequestStack
17    */
18   protected $requestStack;
19
20   /**
21    * Internal cache of RouteMatch objects.
22    *
23    * @var \SplObjectStorage
24    */
25   protected $routeMatches;
26
27   /**
28    * Constructs a CurrentRouteMatch object.
29    *
30    * @param \Symfony\Component\HttpFoundation\RequestStack $request_stack
31    *   The request stack.
32    */
33   public function __construct(RequestStack $request_stack) {
34     $this->requestStack = $request_stack;
35     $this->routeMatches = new \SplObjectStorage();
36   }
37
38   /**
39    * {@inheritdoc}
40    */
41   public function getRouteName() {
42     return $this->getCurrentRouteMatch()->getRouteName();
43   }
44
45   /**
46    * {@inheritdoc}
47    */
48   public function getRouteObject() {
49     return $this->getCurrentRouteMatch()->getRouteObject();
50   }
51
52   /**
53    * {@inheritdoc}
54    */
55   public function getParameter($parameter_name) {
56     return $this->getCurrentRouteMatch()->getParameter($parameter_name);
57   }
58
59   /**
60    * {@inheritdoc}
61    */
62   public function getParameters() {
63     return $this->getCurrentRouteMatch()->getParameters();
64   }
65
66   /**
67    * {@inheritdoc}
68    */
69   public function getRawParameter($parameter_name) {
70     return $this->getCurrentRouteMatch()->getRawParameter($parameter_name);
71   }
72
73   /**
74    * {@inheritdoc}
75    */
76   public function getRawParameters() {
77     return $this->getCurrentRouteMatch()->getRawParameters();
78   }
79
80   /**
81    * Returns the route match for the current request.
82    *
83    * @return \Drupal\Core\Routing\RouteMatchInterface
84    *   The current route match object.
85    */
86   public function getCurrentRouteMatch() {
87     return $this->getRouteMatch($this->requestStack->getCurrentRequest());
88   }
89
90   /**
91    * Returns the route match for a passed in request.
92    *
93    * @param \Symfony\Component\HttpFoundation\Request $request
94    *   A request object.
95    *
96    * @return \Drupal\Core\Routing\RouteMatchInterface
97    *   A route match object created from the request.
98    */
99   protected function getRouteMatch(Request $request) {
100     if (isset($this->routeMatches[$request])) {
101       $route_match = $this->routeMatches[$request];
102     }
103     else {
104       $route_match = RouteMatch::createFromRequest($request);
105
106       // Since getRouteMatch() might be invoked both before and after routing
107       // is completed, only statically cache the route match after there's a
108       // matched route.
109       if ($route_match->getRouteObject()) {
110         $this->routeMatches[$request] = $route_match;
111       }
112     }
113     return $route_match;
114   }
115
116   /**
117    * {@inheritdoc}
118    */
119   public function resetRouteMatch() {
120     $this->routeMatches = new \SplObjectStorage();
121   }
122
123   /**
124    * {@inheritdoc}
125    */
126   public function getMasterRouteMatch() {
127     return $this->getRouteMatch($this->requestStack->getMasterRequest());
128   }
129
130   /**
131    * {@inheritdoc}
132    */
133   public function getParentRouteMatch() {
134     return $this->getRouteMatch($this->requestStack->getParentRequest());
135   }
136
137   /**
138    * {@inheritdoc}
139    */
140   public function getRouteMatchFromRequest(Request $request) {
141     return $this->getRouteMatch($request);
142   }
143
144 }