You are here

class WsConfig in Web Service Data 7

The class used for wsconfig entities

Hierarchy

Expanded class hierarchy of WsConfig

3 string references to 'WsConfig'
wsconfig.inc in modules/wsconfig/plugins/contexts/wsconfig.inc
wsconfig_entity_info in modules/wsconfig/wsconfig.module
Implements hook_entity_info().
wsdata.inc in modules/wsconfig/plugins/access/wsdata.inc

File

modules/wsconfig/wsconfig.entity.inc, line 16
Entity classes

View source
class WsConfig extends Entity {
  public $connector;
  public function __construct($values = array()) {
    parent::__construct($values, 'wsconfig');
    $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
        ->getEndpoint());

      // Configure connector with caching settings.
      if ($this->connector
        ->supportsCaching()) {
        if (is_string($this->data)) {
          $data = unserialize($this->data);
        }
        else {
          $data = $this->data;
        }
        $cache_default_time = isset($data['cache_default_time']) ? $data['cache_default_time'] : 0;
        $cache_default_override = isset($data['cache_default_override']) ? $data['cache_default_override'] : FALSE;
        $stale_cache = isset($data['stale_cache']) ? $data['stale_cache'] : FALSE;
        $this->connector
          ->defaultCache($cache_default_time, $cache_default_override, $stale_cache);
      }
    }
  }

  /**
   * Method wsconfig->setEndpoint().
   *  Overide's the wsconfig_type's default endpoint
   */
  public function setEndpoint($endpoint) {
    if (isset($this->wsconfig_type->data['connector']) and class_exists($this->wsconfig_type->data['connector'])) {
      $this->wsconfig_type
        ->setEndpoint($endpoint);
      $this->connector = new $this->wsconfig_type->data['connector']($endpoint);
      return TRUE;
    }
    return FALSE;
  }

  /**
   * Method wsconfig->getEndpoint().
   */
  public function getEndpoint() {
    if (isset($this->connector) and is_object($this->connector)) {
      return $this->connector
        ->getEndpoint();
    }
    return FALSE;
  }

  /**
   * Get the currently configured language plugin and its settings
   */
  public function getLanguagePlugin() {
    $plugin = $this->wsconfig_type
      ->getEnabledLanguagePlugin();
    if (!empty($plugin)) {
      return $plugin;
    }
    return FALSE;
  }

  /**
   * Method for calling a webservice method.
   *
   * @param string $type
   *  Name of the type of call. Generally "CRUD" but could include other methods
   * @param array $replacements [optional]
   *  Replacements values for placeholders in the request URI
   * @param array $argument [optional]
   *  Payload data for POST requests. Ex: body => 'body data'
   * @param array $options [optional]
   *  Options to pass to the connector. Ex: header data, special triggers.
   *  See the documentation for your given connector
   * @see http://drupal.org/project/restclient
   * @param string $string [reference]
   *  Reference to the URL which was called
   * @return array
   *  Returns the result of the method call.
   */
  public function call($type, $replacement = array(), $argument = array(), $options = array(), &$method = '') {
    if ($this->wsconfig_type
      ->isDisabled()) {
      return FALSE;
    }
    if (isset($this->data['options'][$this
      ->getMethodKey($type)]) and is_array($this->data['options'][$this
      ->getMethodKey($type)])) {
      $options += $this->data['options'][$this
        ->getMethodKey($type)];
    }

    // Pass a reference to the config to the connector
    $options['wsconfig'] = $this;
    if (isset($this->wsconfig_type->data['language always']) and $this->wsconfig_type->data['language always'] and empty($options['language'])) {
      global $language;
      $options['language'] = $language->language;
    }

    // Add the language handling if a language was requested
    if (!empty($options['language'])) {
      $plugin = $this
        ->getLanguagePlugin();
      $options['language plugin'] = $plugin;
    }
    $replacements = $this
      ->getReplacements($type);
    $method = $this
      ->getMethod($type, $replacement);
    $matches = array();
    preg_match_all("/(%[a-zA-Z0-9]+)/", $method, $matches);

    // Compare the tokens extracted to see if some haven't been replaced.
    if (sizeof($matches[0])) {
      foreach ($matches[0] as $match) {
        if (in_array($match, $replacements)) {
          throw new WsConfigException(t('Replacement tokens not all replaced before wscall: @tokens', array(
            '@tokens' => implode(',', $matches[0]),
          )));
        }
      }
    }
    $start_time = microtime(true);
    $result = FALSE;
    if (isset($this->connector)) {
      $result = $this->connector
        ->wscall($type, $method, $argument, $options);
    }
    if ($result === FALSE and isset($this->connector) and $this->connector
      ->isDegraded()) {
      $this->wsconfig_type
        ->disable(TRUE);
    }
    if (module_exists('ws_performance')) {
      $run_time = round((microtime(true) - $start_time) * 1000);
      ws_performance_record_performance($this, $type, $method, $run_time, array(
        'replacement' => $replacement,
        'arguments' => $argument,
        'options' => $options,
      ), $result);
    }
    return $result;
  }

  /**
   * Get a list of defined methods.
   */
  public function getMethod($type, $replacement = array()) {
    if (!isset($this->data[$type . '_data_method'])) {
      return FALSE;
    }
    $method = $this->data[$type . '_data_method'];
    foreach ($replacement as $token => $replace) {
      $method = str_replace($token, $replace, $method);
    }
    return $method;
  }
  public function getReplacements($type) {
    if (!isset($this->data[$type . '_data_method'])) {
      return FALSE;
    }
    $method = $this->data[$type . '_data_method'];
    $matches = array();
    preg_match_all("/(%\\w+)/", $method, $matches);
    return $matches[0];
  }
  public function getOperations() {
    $ops = array();
    foreach ($this->data as $key => $val) {
      if (drupal_substr($key, -1 * drupal_strlen('_data_method')) == '_data_method') {
        $ops[] = drupal_substr($key, 0, -1 * drupal_strlen('_data_method'));
      }
    }
    if (empty($ops)) {
      return array();
    }
    return $ops;
  }
  public function addMethod($type, $name = '') {
    $methods = $this
      ->getPossibleMethods();
    if (!isset($methods[$type])) {
      return FALSE;
    }
    $methodname = $type;
    $supported = $this->connector
      ->getMethods();
    if (isset($supported['multiple'][$type])) {
      $name = drupal_strtolower(preg_replace('/\\W/', '', $name));
      if (empty($name)) {
        return FALSE;
      }
      $methodname .= '_' . $name;
    }
    $this->data[$this
      ->getMethodKey($methodname)] = '';
    return TRUE;
  }
  public function getPossibleMethods() {
    $supported = $this->connector
      ->getMethods();
    $methods = array_merge($supported['single'], $supported['multiple']);
    foreach ($this
      ->getOperations() as $op) {
      if (isset($supported['single'][$op])) {
        unset($methods[$op]);
      }
    }
    return $methods;
  }
  public function getMethodKey($operation) {
    return $operation . '_data_method';
  }
  public function getMethodName($operation) {
    $supported = $this->connector
      ->getMethods();
    foreach ($supported['multiple'] as $key => $val) {
      if (drupal_substr($operation, 0, drupal_strlen($key) + 1) == $key . '_') {
        $operation = ucfirst($key) . ': ' . ucfirst(drupal_substr($operation, drupal_strlen($key) + 1));
        return $operation;
      }
    }
    return ucfirst($operation);
  }
  protected function defaultLabel() {
    return $this->title;
  }
  protected function defaultUri() {
    return array(
      'path' => 'wsconfig/' . $this->name,
    );
  }

}

Members

Namesort descending Modifiers Type Description Overrides
Entity::$defaultLabel protected property 1
Entity::$entityInfo protected property
Entity::$entityType protected property
Entity::$idKey protected property
Entity::$wrapper protected property
Entity::buildContent public function Builds a structured array representing the entity's content. Overrides EntityInterface::buildContent 1
Entity::bundle public function Returns the bundle of the entity. Overrides EntityInterface::bundle
Entity::delete public function Permanently deletes the entity. Overrides EntityInterface::delete
Entity::entityInfo public function Returns the info of the type of the entity. Overrides EntityInterface::entityInfo
Entity::entityType public function Returns the type of the entity. Overrides EntityInterface::entityType
Entity::export public function Exports the entity. Overrides EntityInterface::export
Entity::getTranslation public function Gets the raw, translated value of a property or field. Overrides EntityInterface::getTranslation
Entity::hasStatus public function Checks if the entity has a certain exportable status. Overrides EntityInterface::hasStatus
Entity::identifier public function Returns the entity identifier, i.e. the entities name or numeric id. Overrides EntityInterface::identifier
Entity::internalIdentifier public function Returns the internal, numeric identifier. Overrides EntityInterface::internalIdentifier
Entity::isDefaultRevision public function Checks whether the entity is the default revision. Overrides EntityInterface::isDefaultRevision
Entity::label public function Returns the label of the entity. Overrides EntityInterface::label
Entity::save public function Permanently saves the entity. Overrides EntityInterface::save
Entity::setUp protected function Set up the object instance on construction or unserializiation.
Entity::uri public function Returns the uri of the entity just as entity_uri(). Overrides EntityInterface::uri
Entity::view public function Generate an array for rendering the entity. Overrides EntityInterface::view
Entity::wrapper public function Returns the EntityMetadataWrapper of the entity. Overrides EntityInterface::wrapper
Entity::__sleep public function Magic method to only serialize what's necessary.
Entity::__wakeup public function Magic method to invoke setUp() on unserialization.
WsConfig::$connector public property
WsConfig::addMethod public function
WsConfig::call public function Method for calling a webservice method.
WsConfig::defaultLabel protected function Defines the entity label if the 'entity_class_label' callback is used. Overrides Entity::defaultLabel
WsConfig::defaultUri protected function Override this in order to implement a custom default URI and specify 'entity_class_uri' as 'uri callback' hook_entity_info(). Overrides Entity::defaultUri
WsConfig::getEndpoint public function Method wsconfig->getEndpoint().
WsConfig::getLanguagePlugin public function Get the currently configured language plugin and its settings
WsConfig::getMethod public function Get a list of defined methods.
WsConfig::getMethodKey public function
WsConfig::getMethodName public function
WsConfig::getOperations public function
WsConfig::getPossibleMethods public function
WsConfig::getReplacements public function
WsConfig::setEndpoint public function Method wsconfig->setEndpoint(). Overide's the wsconfig_type's default endpoint
WsConfig::__construct public function Overrides Entity::__construct