diff --git a/s3fs.services.yml b/s3fs.services.yml index f6d9c2b..86d35d1 100644 --- a/s3fs.services.yml +++ b/s3fs.services.yml @@ -4,6 +4,7 @@ services: arguments: ['@database', '@config.factory', '@datetime.time'] stream_wrapper.s3fs: class: Drupal\s3fs\StreamWrapper\S3fsStream + arguments: ['@stream_wrapper_manager'] tags: - { name: stream_wrapper, scheme: s3 } s3fs.path_processor.image_styles: diff --git a/src/StreamWrapper/S3fsStream.php b/src/StreamWrapper/S3fsStream.php index 4166038..a3a9ac1 100644 --- a/src/StreamWrapper/S3fsStream.php +++ b/src/StreamWrapper/S3fsStream.php @@ -132,16 +132,28 @@ class S3fsStream extends StreamWrapper implements StreamWrapperInterface { */ protected $errorState = FALSE; + /** + * The stream wrapper manager. + * + * @var \Drupal\Core\StreamWrapper\StreamWrapperManagerInterface + */ + protected $streamWrapperManager; + /** * S3fsStream constructor. * * Creates the \Aws\S3\S3Client client object and activates the options * specified on the S3 File System Settings page. * + * @param \Drupal\Core\StreamWrapper\StreamWrapperManagerInterface + * The stream wrapper manager. + * * @throws \Drupal\s3fs\S3fsException * Any exception raised. */ - public function __construct() { + public function __construct(StreamWrapperManagerInterface $stream_wrapper_manager) { + $this->streamWrapperManager = $stream_wrapper_manager; + // Since S3fsStreamWrapper is always constructed with the same inputs (the // file URI is not part of construction), we store the constructed settings // statically. This is important for performance because the way Drupal's @@ -359,7 +371,7 @@ class S3fsStream extends StreamWrapper implements StreamWrapperInterface { // Note that $uri is the unaltered value of the File's URI, while // $s3_key may be changed at various points to account for implementation // details on the S3 side (e.g. root_folder, s3fs-public). - $s3_key = str_replace('\\', '/', StreamWrapperManagerInterface::getTarget($this->uri)); + $s3_key = str_replace('\\', '/', $this->streamWrapperManager::getTarget($this->uri)); // If this is a private:// file, it must be served through the // system/files/$path URL, which allows Drupal to restrict access @@ -731,7 +743,7 @@ class S3fsStream extends StreamWrapper implements StreamWrapperInterface { $uri = $this->uri; } $scheme = \Drupal::service('file_system')->uriScheme($uri); - $dirname = dirname(StreamWrapperManagerInterface::getTarget($uri)); + $dirname = dirname($this->streamWrapperManager::getTarget($uri)); // When the dirname() call above is given '$scheme://', it returns '.'. // But '$scheme://.' is an invalid uri, so we return "$scheme://" instead. @@ -779,7 +791,7 @@ class S3fsStream extends StreamWrapper implements StreamWrapperInterface { // If the STREAM_MKDIR_RECURSIVE option was specified, also create all the // ancestor folders of this uri, except for the root directory. $parent_dir = \Drupal::service('file_system')->dirname($uri); - if (($options & STREAM_MKDIR_RECURSIVE) && StreamWrapperManagerInterface::getTarget($parent_dir) != '') { + if (($options & STREAM_MKDIR_RECURSIVE) && $this->streamWrapperManager::getTarget($parent_dir) != '') { return $this->mkdir($parent_dir, $mode, $options); } return TRUE; @@ -1042,7 +1054,7 @@ class S3fsStream extends StreamWrapper implements StreamWrapperInterface { */ protected function getS3fsObject($uri) { // For the root directory, return metadata for a generic folder. - if (StreamWrapperManagerInterface::getTarget($uri) == '') { + if ($this->streamWrapperManager::getTarget($uri) == '') { return $this->s3fs->convertMetadata('/', []); } @@ -1076,7 +1088,7 @@ class S3fsStream extends StreamWrapper implements StreamWrapperInterface { * An array of metadata if the $uri is in the cache. Otherwise, FALSE. */ protected function readCache($uri) { - $uri = StreamWrapperManagerInterface::normalizeUri($uri); + $uri = $this->streamWrapperManager->normalizeUri($uri); // Cache DB reads so that faster caching mechanisms (e.g. redis, memcache) // can further improve performance. @@ -1124,7 +1136,7 @@ class S3fsStream extends StreamWrapper implements StreamWrapperInterface { * Exceptions which occur in the database call will percolate. */ protected function writeCache(array $metadata) { - $metadata['uri'] = StreamWrapperManagerInterface::normalizeUri($metadata['uri']); + $metadata['uri'] = $this->streamWrapperManager->normalizeUri($metadata['uri']); \Drupal::database()->merge('s3fs_file') ->key(['uri' => $metadata['uri']]) @@ -1140,7 +1152,7 @@ class S3fsStream extends StreamWrapper implements StreamWrapperInterface { $dirname = \Drupal::service('file_system')->dirname($metadata['uri']); // If this file isn't in the root directory, also write this file's // ancestor folders to the cache. - if (StreamWrapperManagerInterface::getTarget($dirname) != '') { + if ($this->streamWrapperManager::getTarget($dirname) != '') { $this->mkdir($dirname, NULL, STREAM_MKDIR_RECURSIVE); } } @@ -1330,7 +1342,7 @@ class S3fsStream extends StreamWrapper implements StreamWrapperInterface { $convertedPath = $this->convertUriToKeyedPath($uri, FALSE); $params = $this->getOptions(TRUE); $params['Bucket'] = $this->config['bucket']; - $params['Key'] = StreamWrapperManagerInterface::getTarget($convertedPath); + $params['Key'] = $this->streamWrapperManager::getTarget($convertedPath); return $params; }