3 namespace Drupal\Core\Routing;
5 use Symfony\Component\HttpFoundation\Request;
6 use Symfony\Component\HttpFoundation\RequestStack;
9 * Default object for current_route_match service.
11 class CurrentRouteMatch implements ResettableStackedRouteMatchInterface {
14 * The related request stack.
16 * @var \Symfony\Component\HttpFoundation\RequestStack
18 protected $requestStack;
21 * Internal cache of RouteMatch objects.
23 * @var \SplObjectStorage
25 protected $routeMatches;
28 * Constructs a CurrentRouteMatch object.
30 * @param \Symfony\Component\HttpFoundation\RequestStack $request_stack
33 public function __construct(RequestStack $request_stack) {
34 $this->requestStack = $request_stack;
35 $this->routeMatches = new \SplObjectStorage();
41 public function getRouteName() {
42 return $this->getCurrentRouteMatch()->getRouteName();
48 public function getRouteObject() {
49 return $this->getCurrentRouteMatch()->getRouteObject();
55 public function getParameter($parameter_name) {
56 return $this->getCurrentRouteMatch()->getParameter($parameter_name);
62 public function getParameters() {
63 return $this->getCurrentRouteMatch()->getParameters();
69 public function getRawParameter($parameter_name) {
70 return $this->getCurrentRouteMatch()->getRawParameter($parameter_name);
76 public function getRawParameters() {
77 return $this->getCurrentRouteMatch()->getRawParameters();
81 * Returns the route match for the current request.
83 * @return \Drupal\Core\Routing\RouteMatchInterface
84 * The current route match object.
86 public function getCurrentRouteMatch() {
87 return $this->getRouteMatch($this->requestStack->getCurrentRequest());
91 * Returns the route match for a passed in request.
93 * @param \Symfony\Component\HttpFoundation\Request $request
96 * @return \Drupal\Core\Routing\RouteMatchInterface
97 * A route match object created from the request.
99 protected function getRouteMatch(Request $request) {
100 if (isset($this->routeMatches[$request])) {
101 $route_match = $this->routeMatches[$request];
104 $route_match = RouteMatch::createFromRequest($request);
106 // Since getRouteMatch() might be invoked both before and after routing
107 // is completed, only statically cache the route match after there's a
109 if ($route_match->getRouteObject()) {
110 $this->routeMatches[$request] = $route_match;
119 public function resetRouteMatch() {
120 $this->routeMatches = new \SplObjectStorage();
126 public function getMasterRouteMatch() {
127 return $this->getRouteMatch($this->requestStack->getMasterRequest());
133 public function getParentRouteMatch() {
134 return $this->getRouteMatch($this->requestStack->getParentRequest());
140 public function getRouteMatchFromRequest(Request $request) {
141 return $this->getRouteMatch($request);