ResourceRoutes::getRoutesForResourceConfig

protected ResourceRoutes::getRoutesForResourceConfig(RestResourceConfigInterface $rest_resource_config)

Provides all routes for a given REST resource config.

This method determines where a resource is reachable, what path replacements are used, the required HTTP method for the operation etc.

Parameters

\Drupal\rest\RestResourceConfigInterface $rest_resource_config: The rest resource config.

Return value

\Symfony\Component\Routing\RouteCollection The route collection.

File

core/modules/rest/src/Routing/ResourceRoutes.php, line 84

Class

ResourceRoutes
Subscriber for REST-style routes.

Namespace

Drupal\rest\Routing

Code

protected function getRoutesForResourceConfig(RestResourceConfigInterface $rest_resource_config) {
  $plugin = $rest_resource_config->getResourcePlugin();
  $collection = new RouteCollection();

  foreach ($plugin->routes() as $name => $route) {
    /** @var \Symfony\Component\Routing\Route $route */
    // @todo: Are multiple methods possible here?
    $methods = $route->getMethods();
    // Only expose routes where the method is enabled in the configuration.
    if ($methods && ($method = $methods[0]) && $supported_formats = $rest_resource_config->getFormats($method)) {
      $route->setRequirement('_csrf_request_header_token', 'TRUE');

      // Check that authentication providers are defined.
      if (empty($rest_resource_config->getAuthenticationProviders($method))) {
        $this->logger->error('At least one authentication provider must be defined for resource @id', array(':id' => $rest_resource_config->id()));
        continue;
      }

      // Check that formats are defined.
      if (empty($rest_resource_config->getFormats($method))) {
        $this->logger->error('At least one format must be defined for resource @id', array(':id' => $rest_resource_config->id()));
        continue;
      }

      // If the route has a format requirement, then verify that the
      // resource has it.
      $format_requirement = $route->getRequirement('_format');
      if ($format_requirement && !in_array($format_requirement, $rest_resource_config->getFormats($method))) {
        continue;
      }

      // The configuration seems legit at this point, so we set the
      // authentication provider and add the route.
      $route->setOption('_auth', $rest_resource_config->getAuthenticationProviders($method));
      $route->setDefault('_rest_resource_config', $rest_resource_config->id());
      $collection->add("rest.$name", $route);
    }

  }
  return $collection;
}
doc_Drupal
2016-10-29 09:37:52
Comments
Leave a Comment

Please login to continue.