View source
<?php
namespace Drupal\wsdata\Plugin;
use Drupal\Component\Plugin\PluginBase;
use Drupal\Core\Utility\Token;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Symfony\Component\DependencyInjection\ContainerInterface;
abstract class WSConnectorBase extends PluginBase implements WSConnectorInterface {
use StringTranslationTrait;
protected $expires;
protected $cacheDefaultTime;
protected $cacheDefaultOverride;
protected $staleCache;
protected $endpoint;
protected $error;
protected $status;
protected $languagePlugins = [
'default',
];
public function __construct(array $configuration, $plugin_id, $plugin_definition, Token $token) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->token = $token;
$this->expires = 0;
$this->cacheDefaultTime = 0;
$this->cacheDefaultOverride = FALSE;
$this->staleCache = FALSE;
$this->status = [];
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('token'));
}
public abstract function getOptions();
public abstract function getMethods();
public abstract function getReplacements(array $options);
public function getOptionsForm($options = []) {
return [];
}
public function getCache() {
return NULL;
}
public abstract function call($options, $method, $replacements = [], $data = NULL, array $tokens = []);
public function setEndpoint($endpoint) {
$this->endpoint = trim($endpoint);
}
public function getEndpoint() {
return $this->endpoint;
}
public function supportsCaching($method = NULL) {
return FALSE;
}
public function getError() {
return $this->error;
}
public function getStatus() {
return $this->status;
}
public function getSupportedLanguagePlugins() {
return $this->languagePlugins;
}
public function defaultCache($mintime = 0, $override = FALSE, $stale = FALSE) {
$this->cacheDefaultTime = $mintime;
$this->cacheDefaultOverride = $override;
$this->staleCache = $stale;
}
public function expires() {
if ($this->expires > 0) {
return $this->expires;
}
else {
return FALSE;
}
}
public function isDegraded() {
return FALSE;
}
protected function setError($code, $message) {
$this->error = [
'code' => $code,
'message' => $message,
];
}
protected function clearError() {
$this->error = NULL;
}
public function saveOptions($values) {
$options = [];
foreach (array_keys($this
->getOptionsForm()) as $option) {
if (isset($values[$option])) {
$options[$option] = $values[$option];
}
}
return $options;
}
protected function findTokens($string) {
preg_match_all('/\\[([\\w:]+)\\]/', $string, $matches);
return $matches[1];
}
protected function applyReplacements($string, array $replacements = [], array $tokens = []) {
foreach ($replacements as $token => $replace) {
$string = str_replace('[' . $token . ']', $replace, $string);
}
$string = $this->token
->replace($string, $tokens);
return $string;
}
}