You are here

class WSConnectorLocalFile in Web Service Data 8

Same name and namespace in other branches
  1. 2.0.x src/Plugin/WSConnector/WSConnectorLocalFile.php \Drupal\wsdata\Plugin\WSConnector\WSConnectorLocalFile

Local file connector.

Plugin annotation


@WSConnector(
  id = "WSConnectorLocalFile",
  label = @Translation("Local file connector", context = "WSConnector"),
)

Hierarchy

Expanded class hierarchy of WSConnectorLocalFile

File

src/Plugin/WSConnector/WSConnectorLocalFile.php, line 15

Namespace

Drupal\wsdata\Plugin\WSConnector
View source
class WSConnectorLocalFile extends WSConnectorBase {

  /**
   * {@inheritdoc}
   */
  public function getMethods() {
    return [
      'read',
      'write',
      'append',
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function getOptions() {
    return [
      'filename' => NULL,
      'readonly' => TRUE,
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function getOptionsForm($options = []) {
    return [
      'filename' => [
        '#title' => $this
          ->t('Filename'),
        '#type' => 'textfield',
      ],
      'readonly' => [
        '#title' => $this
          ->t('Prevent writing to this file.'),
        '#type' => 'checkbox',
      ],
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function getReplacements(array $options) {
    return $this
      ->findTokens($this->endpoint . '/' . $options['filename']);
  }

  /**
   * {@inheritdoc}
   */
  public function call($options, $method, $replacements = [], $data = NULL, array $tokens = []) {
    $filename = $this->endpoint . '/' . $options['filename'];
    $filename = $this
      ->applyReplacements($filename, $replacements, $tokens);
    $flags = 0;
    switch ($method) {
      case 'append':
        $flags = FILE_APPEND;
      case 'write':
        if (!is_writable($filename)) {
          $this
            ->setError(1, $this
            ->t('%filename is not writable.', [
            '%filename' => $filename,
          ]));
          return FALSE;
        }
        return file_put_contents($filename, $data, $flags);
      case 'read':
      default:
        if (!file_exists($filename)) {
          $this
            ->setError(1, $this
            ->t('%filename does not exist.', [
            '%filename' => $filename,
          ]));
          return FALSE;
        }
        if (!is_readable($filename)) {
          $this
            ->setError(1, $this
            ->t('%filename is not readable.', [
            '%filename' => $filename,
          ]));
          return FALSE;
        }
        return file_get_contents($filename);
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 3
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
WSConnectorBase::$cacheDefaultOverride protected property
WSConnectorBase::$cacheDefaultTime protected property
WSConnectorBase::$endpoint protected property
WSConnectorBase::$error protected property
WSConnectorBase::$expires protected property
WSConnectorBase::$languagePlugins protected property
WSConnectorBase::$staleCache protected property
WSConnectorBase::$status protected property
WSConnectorBase::applyReplacements protected function Internal function for applying replacements.
WSConnectorBase::clearError protected function Clear current error.
WSConnectorBase::create public static function Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface::create 1
WSConnectorBase::defaultCache public function Figure out the overrides for cache times.
WSConnectorBase::expires public function Get the expired time for caching.
WSConnectorBase::findTokens protected function Internal function for finding tokens.
WSConnectorBase::getCache public function Return cache cid for cases cache rules change. 2
WSConnectorBase::getEndpoint public function Getter for the endpoint.
WSConnectorBase::getError public function Return the last error the connector received.
WSConnectorBase::getStatus public function Return the status of the last call.
WSConnectorBase::getSupportedLanguagePlugins public function Return the list of supported language handling plugins.
WSConnectorBase::isDegraded public function Whether the connector is in a dead state and shouldn't be called.
WSConnectorBase::saveOptions public function Saves the options form. 1
WSConnectorBase::setEndpoint public function Setter for the endpoint.
WSConnectorBase::setError protected function Setter for the connector errors.
WSConnectorBase::supportsCaching public function Whether returned data can be cached. 1
WSConnectorBase::__construct public function Constructs a \Drupal\Component\Plugin\PluginBase object. Overrides PluginBase::__construct 1
WSConnectorLocalFile::call public function Make the connector call. Overrides WSConnectorBase::call
WSConnectorLocalFile::getMethods public function Return available methods supported by the connector. Overrides WSConnectorBase::getMethods
WSConnectorLocalFile::getOptions public function Return available options supported by the connector. Overrides WSConnectorBase::getOptions
WSConnectorLocalFile::getOptionsForm public function Return the settings form provided by the connector. Overrides WSConnectorBase::getOptionsForm
WSConnectorLocalFile::getReplacements public function Return an array of replacements. Overrides WSConnectorBase::getReplacements