You are here

public function SessionStreamWrapper::stream_open in Examples for Developers 3.x

Same name and namespace in other branches
  1. 8 stream_wrapper_example/src/StreamWrapper/SessionStreamWrapper.php \Drupal\stream_wrapper_example\StreamWrapper\SessionStreamWrapper::stream_open()

Opens file or URL.

This method is called immediately after the wrapper is initialized (e.g. by fopen() and file_get_contents()).

Note the streamWrapper::$context property is updated if a valid context is passed to the caller function.

Parameters

string $path: Specifies the URL that was passed to the original function. Note that the URL can be broken apart with parse_url(). Note that only URLs delimited by "://" are supported. ":" and ":/" while technically valid URLs, are not.

string $mode: The mode used to open the file, as detailed for fopen(). Note, remember to check if the mode is valid for the path requested.

int $options: Holds additional flags set by the streams API. It can hold one or more of the following values ORed together:

  • STREAM_USE_PATH: If path is relative, search for the resource using the include_path.
  • STREAM_REPORT_ERRORS: If this flag is set, you are responsible for raising errors using trigger_error() during opening of the stream. If this flag is not set, you should not raise any errors.

string $opened_path: If the path is opened successfully, and STREAM_USE_PATH is set in options, opened_path should be set to the full path of the file/resource that was actually opened.

Return value

bool Returns TRUE on success or FALSE on failure.

Overrides PhpStreamWrapperInterface::stream_open

See also

fopen()

parse_url()

http://php.net/manual/en/streamwrapper.stream-open.php

File

modules/stream_wrapper_example/src/StreamWrapper/SessionStreamWrapper.php, line 268

Class

SessionStreamWrapper
Example stream wrapper class to handle session:// streams.

Namespace

Drupal\stream_wrapper_example\StreamWrapper

Code

public function stream_open($uri, $mode, $options, &$opened_path) {

  // @codingStandardsIgnoreEnd
  $this->uri = $uri;
  $path = $this
    ->getLocalPath($uri);

  // We will support two modes only, 'r' and 'w'.  If the key is 'r',
  // check to make sure the file is there.
  if (stristr($mode, 'r') !== FALSE) {
    if (!$this->sessionHelper
      ->checkPath($path)) {
      return FALSE;
    }
    else {
      $buffer = $this->sessionHelper
        ->getPath($path);
      if (!is_string($buffer)) {
        return FALSE;
      }
      $this->sessionContent = $buffer;
    }
    $this->streamMode = 'r';
  }
  else {
    $this->sessionContent = '';
    $this->streamMode = 'w';
  }

  // Reset the stream pointer since this is an open.
  $this->streamPointer = 0;
  return TRUE;
}