Exemplo n.º 1
0
 /**
  * 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.
  *
  * @param \Drupal\rest\RestResourceConfigInterface $rest_resource_config
  *   The rest resource config.
  *
  * @return \Symfony\Component\Routing\RouteCollection
  *   The route collection.
  */
 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;
 }