Skip to content
Snippets Groups Projects
Commit a049601c authored by David Pascoe-Deslauriers's avatar David Pascoe-Deslauriers
Browse files

feat (wsconfig): allowing variable replacements in the wsconfig type endpoints

Generally in different environments our web service endpoint are different.  To facilitate this, I've added variable replacements to wsconfig type endpoint.
The syntax is to include  into the endpoint urls.  This is parsed out when the webservice call is made and the replacement is done
using variable_get(variable_name, 'default value');
parent c8c229e0
No related branches found
No related tags found
No related merge requests found
......@@ -22,7 +22,7 @@ class WsConfig extends Entity {
$this->wsconfig_type = wsconfig_type_load($this->type);
if (isset($this->wsconfig_type->data['connector']) and class_exists($this->wsconfig_type->data['connector'])) {
$this->connector = new $this->wsconfig_type->data['connector']($this->wsconfig_type->data['endpoint']);
$this->connector = new $this->wsconfig_type->data['connector']($this->wsconfig_type->getEndpoint());
// Configure connector with caching settings.
if ($this->connector->supportsCaching()) {
if (is_string($this->data)) {
......@@ -44,7 +44,8 @@ class WsConfig extends Entity {
*/
public function setEndpoint($endpoint) {
if (isset($this->wsconfig_type->data['connector']) and class_exists($this->wsconfig_type->data['connector'])) {
$this->connector = new $this->wsconfig_type->data['connector']($endpoint);
$this->wsconfig_type->setEndpoint($endpoint);
$this->connector = new $this->wsconfig_type->data['connector']($this->getEndpoint());
return TRUE;
}
return FALSE;
......@@ -54,10 +55,9 @@ class WsConfig extends Entity {
* Method wsconfig->getEndpoint().
*/
public function getEndpoint() {
if (isset($this->connector)) {
if (isset($this->connector) and is_object($this->connector)) {
return $this->connector->getEndpoint();
}
return FALSE;
}
......@@ -395,7 +395,20 @@ class WsConfigType extends Entity {
* API function to set the endpoint in the WSConfig Type.
*/
public function getEndpoint() {
return $this->data['endpoint'];
$endpoint = $this->data['endpoint'];
$matches = array();
preg_match_all('/\$\{(.+?)(:.+){0,1}\}/', $endpoint, $matches);
if (!empty($matches[0])) {
for ($n = 0; $n < count($matches[0]); $n++) {
$default = '';
if (isset($matches[2][$n])) {
$default = drupal_substr($matches[2][$n], 1);
}
$replacements[] = (string)variable_get($matches[1][$n], $default);
}
$endpoint = str_replace($matches[0], $replacements, $endpoint);
}
return $endpoint;
}
/**
......
......@@ -39,7 +39,7 @@ function wsconfig_type_form($form, &$form_state, $wsconfig_type, $op = 'edit') {
$form['data']['endpoint'] = array(
'#type' => 'textfield',
'#title' => t('Web service endpoint'),
'#description' => t('Full URL to the service endpoint Ex: http://example.com/rest'),
'#description' => t('Full URL to the service endpoint Ex: http://example.com/rest.<br>Endpoints can contain replacement tokens which will be replaced by values from Drupal variables. Any number of variable replacements can be added in the endpoint and the replacement will be done when the service is called. Variable replacement is done with the syntax "${variable_name:default value}".'),
'#default_value' => !empty($wsconfig_type->data['endpoint']) ? $wsconfig_type->data['endpoint'] : '',
);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment