RouteSubscriber.php 3.17 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
<?php

/**
 * Definition of \Drupal\rest\EventSubscriber\RouteSubscriber.
 */

namespace Drupal\rest\EventSubscriber;

use Drupal\Core\Config\ConfigFactory;
use Drupal\Core\Routing\RouteBuildEvent;
use Drupal\Core\Routing\RoutingEvents;
use Drupal\rest\Plugin\Type\ResourcePluginManager;

use Symfony\Component\EventDispatcher\EventSubscriberInterface;

/**
 * Subscriber for REST-style routes.
 */
class RouteSubscriber implements EventSubscriberInterface {

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

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

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

  /**
   * Adds routes to enabled REST resources.
   *
   * @param \Drupal\Core\Routing\RouteBuildEvent $event
   *   The route building event.
   */
  public function dynamicRoutes(RouteBuildEvent $event) {

    $collection = $event->getRouteCollection();
57
    $enabled_resources = $this->config->get('rest.settings')->load()->get('resources');
58

59
60
61
    // Iterate over all enabled resource plugins.
    foreach ($enabled_resources as $id => $enabled_methods) {
      $plugin = $this->manager->getInstance(array('id' => $id));
62

63
64
65
66
      foreach ($plugin->routes() as $name => $route) {
        $method = $route->getRequirement('_method');
        // Only expose routes where the method is enabled in the configuration.
        if ($method && isset($enabled_methods[$method])) {
67
          $route->setRequirement('_access_rest_csrf',  'TRUE');
68
69
70
71
72
73
74

          // If the array of configured format restrictions is empty for a
          // method always add the route.
          if (empty($enabled_methods[$method])) {
            $collection->add("rest.$name", $route);
            continue;
          }
75
76
          // Check if there are authentication provider restrictions in the
          // configuration and apply them to the route.
77
          if (!empty($enabled_methods[$method]['supported_auth']) && is_array($enabled_methods[$method]['supported_auth'])) {
78
79
            $route->setOption('_auth', $enabled_methods[$method]['supported_auth']);
          }
80
81
82
          // If there is no format requirement or if it matches the
          // configuration also add the route.
          $format_requirement = $route->getRequirement('_format');
83
          if (!$format_requirement || empty($enabled_methods[$method]['supported_formats']) || in_array($format_requirement, $enabled_methods[$method]['supported_formats'])) {
84
85
            $collection->add("rest.$name", $route);
          }
86
87
88
89
90
91
92
93
94
95
96
97
98
99
        }
      }
    }
  }

  /**
   * Implements EventSubscriberInterface::getSubscribedEvents().
   */
  static function getSubscribedEvents() {
    $events[RoutingEvents::DYNAMIC] = 'dynamicRoutes';
    return $events;
  }
}