CurrentRouteMatch.php in Drupal 9        
                          
                  
                        
  
  
  
  
File
  core/lib/Drupal/Core/Routing/CurrentRouteMatch.php
  
    View source  
  <?php
namespace Drupal\Core\Routing;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
class CurrentRouteMatch implements ResettableStackedRouteMatchInterface {
  
  protected $requestStack;
  
  protected $routeMatches;
  
  public function __construct(RequestStack $request_stack) {
    $this->requestStack = $request_stack;
    $this->routeMatches = new \SplObjectStorage();
  }
  
  public function getRouteName() {
    return $this
      ->getCurrentRouteMatch()
      ->getRouteName();
  }
  
  public function getRouteObject() {
    return $this
      ->getCurrentRouteMatch()
      ->getRouteObject();
  }
  
  public function getParameter($parameter_name) {
    return $this
      ->getCurrentRouteMatch()
      ->getParameter($parameter_name);
  }
  
  public function getParameters() {
    return $this
      ->getCurrentRouteMatch()
      ->getParameters();
  }
  
  public function getRawParameter($parameter_name) {
    return $this
      ->getCurrentRouteMatch()
      ->getRawParameter($parameter_name);
  }
  
  public function getRawParameters() {
    return $this
      ->getCurrentRouteMatch()
      ->getRawParameters();
  }
  
  public function getCurrentRouteMatch() {
    return $this
      ->getRouteMatch($this->requestStack
      ->getCurrentRequest());
  }
  
  protected function getRouteMatch(Request $request) {
    if (isset($this->routeMatches[$request])) {
      $route_match = $this->routeMatches[$request];
    }
    else {
      $route_match = RouteMatch::createFromRequest($request);
      
      if ($route_match
        ->getRouteObject()) {
        $this->routeMatches[$request] = $route_match;
      }
    }
    return $route_match;
  }
  
  public function resetRouteMatch() {
    $this->routeMatches = new \SplObjectStorage();
  }
  
  public function getMasterRouteMatch() {
    return $this
      ->getRouteMatch($this->requestStack
      ->getMainRequest());
  }
  
  public function getParentRouteMatch() {
    return $this
      ->getRouteMatch($this->requestStack
      ->getParentRequest());
  }
  
  public function getRouteMatchFromRequest(Request $request) {
    return $this
      ->getRouteMatch($request);
  }
}