You are here

class ResourceRoutes in Zircon Profile 8.0

Same name and namespace in other branches
  1. 8 core/modules/rest/src/Routing/ResourceRoutes.php \Drupal\rest\Routing\ResourceRoutes

Subscriber for REST-style routes.

Hierarchy

Expanded class hierarchy of ResourceRoutes

1 string reference to 'ResourceRoutes'
rest.services.yml in core/modules/rest/rest.services.yml
core/modules/rest/rest.services.yml
1 service uses ResourceRoutes
rest.resource_routes in core/modules/rest/rest.services.yml
Drupal\rest\Routing\ResourceRoutes

File

core/modules/rest/src/Routing/ResourceRoutes.php, line 19
Contains \Drupal\rest\Routing\ResourceRoutes.

Namespace

Drupal\rest\Routing
View source
class ResourceRoutes extends RouteSubscriberBase {

  /**
   * The plugin manager for REST plugins.
   *
   * @var \Drupal\rest\Plugin\Type\ResourcePluginManager
   */
  protected $manager;

  /**
   * The Drupal configuration factory.
   *
   * @var \Drupal\Core\Config\ConfigFactoryInterface
   */
  protected $config;

  /**
   * A logger instance.
   *
   * @var \Psr\Log\LoggerInterface
   */
  protected $logger;

  /**
   * Constructs a RouteSubscriber object.
   *
   * @param \Drupal\rest\Plugin\Type\ResourcePluginManager $manager
   *   The resource plugin manager.
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config
   *   The configuration factory holding resource settings.
   * @param \Psr\Log\LoggerInterface $logger
   *   A logger instance.
   */
  public function __construct(ResourcePluginManager $manager, ConfigFactoryInterface $config, LoggerInterface $logger) {
    $this->manager = $manager;
    $this->config = $config;
    $this->logger = $logger;
  }

  /**
   * Alters existing routes for a specific collection.
   *
   * @param \Symfony\Component\Routing\RouteCollection $collection
   *   The route collection for adding routes.
   * @return array
   */
  protected function alterRoutes(RouteCollection $collection) {
    $routes = array();
    $enabled_resources = $this->config
      ->get('rest.settings')
      ->get('resources') ?: array();

    // Iterate over all enabled resource plugins.
    foreach ($enabled_resources as $id => $enabled_methods) {
      $plugin = $this->manager
        ->getInstance(array(
        'id' => $id,
      ));
      foreach ($plugin
        ->routes() as $name => $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]) && $method && isset($enabled_methods[$method])) {
          $route
            ->setRequirement('_access_rest_csrf', 'TRUE');

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

          // Check that formats are defined.
          if (empty($enabled_methods[$method]['supported_formats']) || !is_array($enabled_methods[$method]['supported_formats'])) {
            $this->logger
              ->error('At least one format must be defined for resource @id', array(
              ':id' => $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, $enabled_methods[$method]['supported_formats'])) {
            continue;
          }

          // The configuration seems legit at this point, so we set the
          // authentication provider and add the route.
          $route
            ->setOption('_auth', $enabled_methods[$method]['supported_auth']);
          $routes["rest.{$name}"] = $route;
          $collection
            ->add("rest.{$name}", $route);
        }
      }
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ResourceRoutes::$config protected property The Drupal configuration factory.
ResourceRoutes::$logger protected property A logger instance.
ResourceRoutes::$manager protected property The plugin manager for REST plugins.
ResourceRoutes::alterRoutes protected function Alters existing routes for a specific collection. Overrides RouteSubscriberBase::alterRoutes
ResourceRoutes::__construct public function Constructs a RouteSubscriber object.
RouteSubscriberBase::getSubscribedEvents public static function Returns an array of event names this subscriber wants to listen to. Overrides EventSubscriberInterface::getSubscribedEvents 5
RouteSubscriberBase::onAlterRoutes public function Delegates the route altering to self::alterRoutes(). 1