You are here

protected function RestGenerator::getRouteForResourceMethod in OpenAPI 8

Gets the matching for route for the resource and method.

Parameters

\Drupal\rest\RestResourceConfigInterface $resource_config: The REST config resource.

string $method: The HTTP method.

Return value

\Symfony\Component\Routing\Route The route.

Throws

\Exception If no route is found.

1 call to RestGenerator::getRouteForResourceMethod()
RestGenerator::getPaths in src/Plugin/openapi/OpenApiGenerator/RestGenerator.php
Returns the paths information.

File

src/Plugin/openapi/OpenApiGenerator/RestGenerator.php, line 226

Class

RestGenerator
Defines an OpenApi Schema Generator for the Rest module.

Namespace

Drupal\openapi\Plugin\openapi\OpenApiGenerator

Code

protected function getRouteForResourceMethod(RestResourceConfigInterface $resource_config, $method) {
  if ($this
    ->isEntityResource($resource_config)) {
    $route_name = 'rest.' . $resource_config
      ->id() . ".{$method}";
    $routes = $this->routingProvider
      ->getRoutesByNames([
      $route_name,
    ]);
    if (empty($routes)) {
      $formats = $resource_config
        ->getFormats($method);
      if (count($formats) > 0) {
        $route_name .= ".{$formats[0]}";
        $routes = $this->routingProvider
          ->getRoutesByNames([
          $route_name,
        ]);
      }
    }
    if ($routes) {
      return array_pop($routes);
    }
  }
  else {
    $resource_plugin = $resource_config
      ->getResourcePlugin();
    foreach ($resource_plugin
      ->routes() as $route) {
      $methods = $route
        ->getMethods();
      if (array_search($method, $methods) !== FALSE) {
        return $route;
      }
    }
  }
  throw new \Exception("No route found for REST resource, {$resource_config->id()}, for method {$method}");
}