class WSDataService in Web Service Data 8
Same name and namespace in other branches
- 2.0.x src/WSDataService.php \Drupal\wsdata\WSDataService
Service for processing WSData requests.
Hierarchy
- class \Drupal\wsdata\WSDataService uses StringTranslationTrait
Expanded class hierarchy of WSDataService
3 files declare their use of WSDataService
- WSCallTestForm.php in src/
Form/ WSCallTestForm.php - WSDataBlock.php in modules/
wsdata_block/ src/ Plugin/ Block/ WSDataBlock.php - WSFieldConfigForm.php in modules/
wsdata_field/ src/ Form/ WSFieldConfigForm.php
1 string reference to 'WSDataService'
1 service uses WSDataService
File
- src/
WSDataService.php, line 14
Namespace
Drupal\wsdataView source
class WSDataService {
use StringTranslationTrait;
protected $error;
protected $performance;
protected $status;
protected $logger;
/**
* {@inheritdoc}
*/
public function __construct(EntityTypeManagerInterface $entity_type_manager, State $state, LoggerChannelFactoryInterface $logger) {
$this->entity_type_manager = $entity_type_manager;
$this->state = $state;
$this->logger = $logger;
$this->performance = [
'calls' => 0,
'runtime' => 0.0,
'log' => [],
];
$this->status = [];
}
/**
* {@inheritdoc}
*/
public function __destruct() {
if ($this->performance['calls'] > 0 and $this->state
->get('wsdata_performance_log', 0)) {
$message = $this
->t('WSData Performance - %calls calls in %runtime seconds.', [
'%calls' => $this->performance['calls'],
'%runtime' => round($this->performance['runtime'], 3),
]);
$message .= "<br>\nCall list:\n<ol>\n";
foreach ($this->performance['log'] as $log) {
$method = '';
if (isset($method)) {
$method = ':' . $log['method'];
}
$message .= '<li>' . $log['wscall'] . $method . ' - ' . round($log['runtime'], 3) . "s (" . $log['cached'] . ")</li>\n";
}
$message .= '</ol>';
$this->logger
->get('wsdata')
->debug($message);
}
}
/**
* Call method to make the WSCall.
*/
public function call($wscall, $method = NULL, $replacements = [], $data = NULL, $options = [], $key = NULL, $tokens = [], $cache_tag = []) {
$this->status = [];
if (!is_object($wscall)) {
$wscall = $this->entity_type_manager
->getStorage('wscall')
->load($wscall);
}
$start = microtime(TRUE);
$data = $wscall
->call($method, $replacements, $data, $options, $key, $tokens, $cache_tag);
$end = microtime(TRUE);
$this->status = $wscall
->lastCallStatus();
if ($this->state
->get('wsdata_debug_mode')) {
ksm($this->status);
}
// Track performance information.
$duration = $end - $start;
$this->performance['calls']++;
$this->performance['runtime'] += $duration;
$this->performance['log'][] = [
'wscall' => $wscall
->label(),
'method' => $method,
'runtime' => $duration,
'cached' => $this->status['cache']['debug'] ?? '',
];
return $data;
}
/**
* Return the error from the last call.
*/
public function getError() {
if ($this->error) {
$error = $this->error;
$this->error = NULL;
return $error;
}
return NULL;
}
/**
* Generid WSCall setting form.
*/
public function wscallForm($configurations = [], $wscall_option = NULL) {
$wscalls = $this->entity_type_manager
->getStorage('wscall')
->loadMultiple();
$options = [
'' => $this
->t('- Select -'),
];
foreach ($wscalls as $wscall) {
$options[$wscall
->id()] = $wscall
->label();
}
$element['wscall'] = [
'#type' => 'select',
'#title' => $this
->t('Web Service Call'),
'#options' => $options,
'#required' => TRUE,
'#ajax' => [
'callback' => 'Drupal\\wsdata\\WSDataService::wscallConfigurationsReplacements',
'wrapper' => 'wscall-replacement-tokens-wrapper',
],
'#default_value' => isset($configurations['wscall']) ? $configurations['wscall'] : '',
];
// Fetch the replacement tokens for this wscall.
$element['replacements'] = [
'#id' => 'wscall-replacement-tokens-wrapper',
'#type' => 'container',
];
// Based on the wscall create the replacements section of the wscall.
if (!empty($wscall_option)) {
foreach ($wscalls[$wscall_option]
->getReplacements() as $replacement) {
$element['replacements'][$replacement] = [
'#type' => 'textfield',
'#title' => $replacement,
'#default_value' => isset($configurations['replacements'][$replacement]) ? $configurations['replacements'][$replacement] : '',
];
}
}
$element['data'] = [
'#type' => 'textarea',
'#title' => $this
->t('Data'),
'#default_value' => isset($configurations['data']) ? $configurations['data'] : '',
];
$element['returnToken'] = [
'#type' => 'textfield',
'#title' => $this
->t('Token to select'),
'#description' => $this
->t('Seperate element names with a ":" to select nested elements.'),
'#default_value' => isset($configurations['returnToken']) ? $configurations['returnToken'] : '',
];
return $element;
}
/**
* Expose the status of the last call.
*/
public function lastCallStatus() {
return $this->status;
}
/**
* Ajax call back for the replacement pattern.
*/
public function wscallConfigurationsReplacements(array &$form, FormStateInterface $form_state) {
if (isset($form['replacements'])) {
return $form['replacements'];
}
elseif (isset($form['settings']['replacements'])) {
return $form['settings']['replacements'];
}
else {
return $form;
}
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. | |
WSDataService:: |
protected | property | ||
WSDataService:: |
protected | property | ||
WSDataService:: |
protected | property | ||
WSDataService:: |
protected | property | ||
WSDataService:: |
public | function | Call method to make the WSCall. | |
WSDataService:: |
public | function | Return the error from the last call. | |
WSDataService:: |
public | function | Expose the status of the last call. | |
WSDataService:: |
public | function | Ajax call back for the replacement pattern. | |
WSDataService:: |
public | function | Generid WSCall setting form. | |
WSDataService:: |
public | function | ||
WSDataService:: |
public | function |