You are here

public function WSConnectorLocalFile::call 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::call()

Make the connector call.

Overrides WSConnectorBase::call

File

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

Class

WSConnectorLocalFile
Local file connector.

Namespace

Drupal\wsdata\Plugin\WSConnector

Code

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);
  }
}