You are here

class DeployServiceRest in Deploy - Content Staging 7.2

Same name and namespace in other branches
  1. 7.3 includes/DeployServiceRest.inc \DeployServiceRest

Base class for REST-based service plugins.

This is not a full plugin, but an abstract class that plugins can extend.

Hierarchy

Expanded class hierarchy of DeployServiceRest

File

includes/DeployServiceRest.inc, line 12
Implementation of a REST based client for deploying entities.

View source
class DeployServiceRest implements DeployService {

  /**
   * Configuration options.
   *
   * @var array
   */
  public $config = array();

  /**
   * An associative array of http headers for the REST request.
   *
   * @var array
   */
  public $headers = array();

  /**
   * Constructor for a service plugin.
   *
   * @param array $config
   *   An associative array of configuration settings for the service plugin.
   */
  public function __construct(array $config = array()) {
    $this->config += array(
      'debug' => FALSE,
      'url' => '',
      'headers' => array(),
      'context' => $this
        ->fetchContext(),
    );
    $this->config = array_merge($this->config, $config);
  }
  public function deploy(Traversable $iterator) {
  }
  public function publish(Traversable $iterator) {
  }

  /**
   * Makes a HTTP request.
   *
   * @param string $url
   *   The URL to request.
   * @param string $method
   *   The request method to use, like 'POST', 'GET' or 'PUT'.
   * @param string $data
   *   The payload to send with the request.
   */
  public function httpRequest($url, $method, $data = NULL) {
    $options = array(
      'method' => $method,
      'headers' => $this->config['headers'],
      'data' => $data,
      'context' => $this->config['context'],
    );
    if ($this->config['debug']) {
      watchdog('deploy', 'Service request: %url <pre>@options</pre>', array(
        '%url' => $url,
        '@options' => print_r($options, TRUE),
      ), WATCHDOG_DEBUG);
    }
    $response = drupal_http_request($url, $options);
    if ($this->config['debug']) {
      watchdog('deploy', 'Service response: <pre>@response</pre>', array(
        '@response' => print_r($response, TRUE),
      ), WATCHDOG_DEBUG);
    }
    if (isset($response->error) || !in_array($response->code, array(
      200,
      304,
    ))) {
      throw new DeployServiceException(t('Service error: @code @error', array(
        '@code' => $response->code,
        '@error' => $response->error,
      )));
    }
  }

  /**
   * Configuration form.
   *
   * @param array $form_state
   *   The complete form state.
   */
  public function configForm(&$form_state) {
    return array(
      'url' => array(
        '#type' => 'textfield',
        '#title' => t('Endpoint URL'),
        '#description' => t('Enter endpoint URL. Example: %url', array(
          '%url' => 'http://example.com/services/rest',
        )),
        '#default_value' => $this->config['url'],
      ),
    );
  }

  /**
   * Fetches the context for the HTTP request.
   *
   * @see stream_context_create()
   *
   * @return resource
   *   The stream context resource.
   */
  protected function fetchContext() {
    $verify_ssl = variable_get('deploy_verify_ssl', TRUE);
    $context_options = array(
      'ssl' => array(
        'verify_peer' => $verify_ssl,
        'verify_peer_name' => $verify_ssl,
        'allow_self_signed' => !$verify_ssl,
      ),
    );
    $context = stream_context_create($context_options);
    return $context;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DeployServiceRest::$config public property Configuration options.
DeployServiceRest::$headers public property An associative array of http headers for the REST request.
DeployServiceRest::configForm public function Configuration form. Overrides DeployService::configForm
DeployServiceRest::deploy public function Deploy all entities in the $iterator. This method should only move entities over to the endpoint and create unpublished revisions (if supported). The 'publish' method is responsible for publishing all successfully deployed entities. Overrides DeployService::deploy 1
DeployServiceRest::fetchContext protected function Fetches the context for the HTTP request.
DeployServiceRest::httpRequest public function Makes a HTTP request.
DeployServiceRest::publish public function Publish all entities in the $iterator. This method should only publish entities on the endpoint (if supported). The 'deploy' method is responsible for moving the entities to the endpoint, prior to the publishing. Overrides DeployService::publish 1
DeployServiceRest::__construct public function Constructor for a service plugin. Overrides DeployService::__construct