class DeployServiceRest in Deploy - Content Staging 7.3
Same name and namespace in other branches
- 7.2 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
- class \DeployServiceRest implements DeployService
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(),
'timeout' => 30.0,
);
$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'],
'timeout' => (double) $this->config['timeout'],
);
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'],
),
'timeout' => array(
'#type' => 'textfield',
'#title' => t('Timeout in seconds'),
'#description' => t('A float representing the maximum number of seconds the function call may take. The default is 30 seconds. Note: Change for large data transfers only.'),
'#default_value' => !empty($this->config['timeout']) ? $this->config['timeout'] : 30.0,
),
);
}
/**
* 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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DeployServiceRest:: |
public | property | Configuration options. | |
DeployServiceRest:: |
public | property | An associative array of http headers for the REST request. | |
DeployServiceRest:: |
public | function |
Configuration form. Overrides DeployService:: |
|
DeployServiceRest:: |
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:: |
2 |
DeployServiceRest:: |
protected | function | Fetches the context for the HTTP request. | |
DeployServiceRest:: |
public | function | Makes a HTTP request. | 1 |
DeployServiceRest:: |
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:: |
1 |
DeployServiceRest:: |
public | function |
Constructor for a service plugin. Overrides DeployService:: |
1 |