You are here

public function StreamWrapperManager::getWrappers in Drupal 8

Same name and namespace in other branches
  1. 9 core/lib/Drupal/Core/StreamWrapper/StreamWrapperManager.php \Drupal\Core\StreamWrapper\StreamWrapperManager::getWrappers()

Provides Drupal stream wrapper registry.

A stream wrapper is an abstraction of a file system that allows Drupal to use the same set of methods to access both local files and remote resources.

Provide a facility for managing and querying user-defined stream wrappers in PHP. PHP's internal stream_get_wrappers() doesn't return the class registered to handle a stream, which we need to be able to find the handler for class instantiation.

If a module registers a scheme that is already registered with PHP, the existing scheme will be unregistered and replaced with the specified class.

A stream is referenced as "scheme://target".

The optional $filter parameter can be used to retrieve only the stream wrappers that are appropriate for particular usage. For example, this returns only stream wrappers that use local file storage:

$stream_wrapper_manager = \Drupal::service('stream_wrapper_manager');
$local_stream_wrappers = $stream_wrapper_manager
  ->getWrappers(StreamWrapperInterface::LOCAL);

The $filter parameter can only filter to types containing a particular flag. In some cases, you may want to filter to types that do not contain a particular flag. For example, you may want to retrieve all stream wrappers that are not writable, or all stream wrappers that are not local. PHP's array_diff_key() function can be used to help with this. For example, this returns only stream wrappers that do not use local file storage:

$stream_wrapper_manager = \Drupal::service('stream_wrapper_manager');
$remote_stream_wrappers = array_diff_key($stream_wrapper_manager
  ->getWrappers(StreamWrapperInterface::ALL), $stream_wrapper_manager
  ->getWrappers(StreamWrapperInterface::LOCAL));

Parameters

int $filter: (Optional) Filters out all types except those with an on bit for each on bit in $filter. For example, if $filter is StreamWrapperInterface::WRITE_VISIBLE, which is equal to (StreamWrapperInterface::READ | StreamWrapperInterface::WRITE | StreamWrapperInterface::VISIBLE), then only stream wrappers with all three of these bits set are returned. Defaults to StreamWrapperInterface::ALL, which returns all registered stream wrappers.

Return value

array An array keyed by scheme, with values containing an array of information about the stream wrapper, as returned by hook_stream_wrappers(). If $filter is omitted or set to StreamWrapperInterface::ALL, the entire Drupal stream wrapper registry is returned. Otherwise only the stream wrappers whose 'type' bitmask has an on bit for each bit specified in $filter are returned.

Overrides StreamWrapperManagerInterface::getWrappers

2 calls to StreamWrapperManager::getWrappers()
StreamWrapperManager::getDescriptions in core/lib/Drupal/Core/StreamWrapper/StreamWrapperManager.php
Returns registered stream wrapper descriptions.
StreamWrapperManager::getNames in core/lib/Drupal/Core/StreamWrapper/StreamWrapperManager.php
Returns registered stream wrapper names.

File

core/lib/Drupal/Core/StreamWrapper/StreamWrapperManager.php, line 49

Class

StreamWrapperManager
Provides a StreamWrapper manager.

Namespace

Drupal\Core\StreamWrapper

Code

public function getWrappers($filter = StreamWrapperInterface::ALL) {
  if (isset($this->wrappers[$filter])) {
    return $this->wrappers[$filter];
  }
  elseif (isset($this->wrappers[StreamWrapperInterface::ALL])) {
    $this->wrappers[$filter] = [];
    foreach ($this->wrappers[StreamWrapperInterface::ALL] as $scheme => $info) {

      // Bit-wise filter.
      if (($info['type'] & $filter) == $filter) {
        $this->wrappers[$filter][$scheme] = $info;
      }
    }
    return $this->wrappers[$filter];
  }
  else {
    return [];
  }
}