abstract class ResourceBase

Common base class for resource plugins.

Hierarchy

Expanded class hierarchy of ResourceBase

2 files declare their use of ResourceBase
DBLogResource.php in drupal/core/modules/rest/lib/Drupal/rest/Plugin/rest/resource/DBLogResource.php
Definition of Drupal\rest\Plugin\rest\resource\DBLogResource.
EntityResource.php in drupal/core/modules/rest/lib/Drupal/rest/Plugin/rest/resource/EntityResource.php
Definition of Drupal\rest\Plugin\rest\resource\EntityResource.

File

drupal/core/modules/rest/lib/Drupal/rest/Plugin/ResourceBase.php, line 17
Definition of Drupal\rest\Plugin\ResourceBase.

Namespace

Drupal\rest\Plugin
View source
abstract class ResourceBase extends PluginBase {

  /**
   * Provides an array of permissions suitable for hook_permission().
   *
   * Every plugin operation method gets its own user permission. Example:
   * "restful delete entity:node" with the title "Access DELETE on Node
   * resource".
   *
   * @reutrn array
   *   The permission array.
   */
  public function permissions() {
    $permissions = array();
    $definition = $this
      ->getDefinition();
    foreach ($this
      ->requestMethods() as $method) {
      $lowered_method = strtolower($method);

      // Only expose permissions where the HTTP request method exists on the
      // plugin.
      if (method_exists($this, $lowered_method)) {
        $permissions["restful {$lowered_method} {$this->plugin_id}"] = array(
          'title' => t('Access @method on %label resource', array(
            '@method' => $method,
            '%label' => $definition['label'],
          )),
        );
      }
    }
    return $permissions;
  }

  /**
   * Returns a collection of routes with URL path information for the resource.
   *
   * This method determines where a resource is reachable, what path
   * replacements are used, the required HTTP method for the operation etc.
   *
   * @return \Symfony\Component\Routing\RouteCollection
   *   A collection of routes that should be registered for this resource.
   */
  public function routes() {
    $collection = new RouteCollection();
    $methods = $this
      ->requestMethods();
    foreach ($methods as $method) {
      $lower_method = strtolower($method);

      // Only expose routes where the HTTP request method exists on the plugin.
      if (method_exists($this, $lower_method)) {
        $prefix = strtr($this->plugin_id, ':', '/');
        $route = new Route("/{$prefix}/{id}", array(
          '_controller' => 'Drupal\\rest\\RequestHandler::handle',
          // @todo Once http://drupal.org/node/1793520 is committed we will have
          // route object avaialble in the controller so 'plugin' property
          // should be changed to '_plugin'.
          // @see RequestHandler::handle().
          'plugin' => $this->plugin_id,
        ), array(
          // The HTTP method is a requirement for this route.
          '_method' => $method,
          '_permission' => "restful {$lower_method} {$this->plugin_id}",
        ));
        $name = strtr($this->plugin_id, ':', '.');
        $collection
          ->add("{$name}.{$method}", $route);
      }
    }
    return $collection;
  }

  /**
   * Provides predefined HTTP request methods.
   *
   * Plugins can override this method to provide additional custom request
   * methods.
   *
   * @return array
   *   The list of allowed HTTP request method strings.
   */
  protected function requestMethods() {
    return drupal_map_assoc(array(
      'HEAD',
      'GET',
      'POST',
      'PUT',
      'DELETE',
      'TRACE',
      'OPTIONS',
      'CONNECT',
      'PATCH',
    ));
  }

}

Members

Namesort descending Modifiers Type Description Overrides
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$discovery protected property The discovery object.
PluginBase::$plugin_id protected property The plugin_id.
PluginBase::getDefinition public function Implements Drupal\Component\Plugin\PluginInterface::getDefinition(). Overrides PluginInspectionInterface::getDefinition
PluginBase::getPluginId public function Implements Drupal\Component\Plugin\PluginInterface::getPluginId(). Overrides PluginInspectionInterface::getPluginId
PluginBase::__construct public function Constructs a Drupal\Component\Plugin\PluginBase object. 8
ResourceBase::permissions public function Provides an array of permissions suitable for hook_permission().
ResourceBase::requestMethods protected function Provides predefined HTTP request methods.
ResourceBase::routes public function Returns a collection of routes with URL path information for the resource. 1