Unverified Commit e08ec139 authored by Alex Pott's avatar Alex Pott
Browse files

Issue #3034072 by kim.pepper, Berdir, alexpott, jibran, larowlan,...

Issue #3034072 by kim.pepper, Berdir, alexpott, jibran, larowlan, claudiu.cristea: Move file uri/scheme functions from file.inc and FileSystem to StreamWrapperManager
parent d2792e13
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -1599,7 +1599,7 @@ services:
      - { name: needs_destruction }
  library.discovery.parser:
    class: Drupal\Core\Asset\LibraryDiscoveryParser
    arguments: ['@app.root', '@module_handler', '@theme.manager']
    arguments: ['@app.root', '@module_handler', '@theme.manager', '@stream_wrapper_manager']
  library.dependency_resolver:
    class: Drupal\Core\Asset\LibraryDependencyResolver
    arguments: ['@library.discovery']
+49 −37
Original line number Diff line number Diff line
@@ -16,6 +16,7 @@
use Drupal\Core\Site\Settings;
use Drupal\Core\StreamWrapper\PrivateStream;
use Drupal\Core\StreamWrapper\PublicStream;
use Drupal\Core\StreamWrapper\StreamWrapperManager;

/**
 * Default mode for new directories.
@@ -98,25 +99,29 @@
/**
 * Returns the scheme of a URI (e.g. a stream).
 *
 * @deprecated in Drupal 8.0.x-dev, will be removed before Drupal 9.0.0.
 *   Use \Drupal\Core\File\FileSystem::uriScheme().
 * @deprecated in drupal:8.8.0 and will be removed from drupal:9.0.0. Use
 *   Drupal\Core\StreamWrapper\StreamWrapperManagerInterface::getScheme()
 *   instead.
 *
 * @see https://www.drupal.org/node/2418133
 * @see https://www.drupal.org/node/3035273
 */
function file_uri_scheme($uri) {
  return \Drupal::service('file_system')->uriScheme($uri);
  @trigger_error('file_uri_scheme() is deprecated in drupal:8.0.0 and will be removed before drupal:9.0.0. Use \Drupal\Core\StreamWrapper\StreamWrapperManagerInterface::getScheme() instead. See https://www.drupal.org/node/3035273', E_USER_DEPRECATED);
  return StreamWrapperManager::getScheme($uri);
}

/**
 * Checks that the scheme of a stream URI is valid.
 *
 * @deprecated in Drupal 8.0.x-dev, will be removed before Drupal 9.0.0.
 *   Use \Drupal\Core\File\FileSystem::validScheme().
 * @deprecated in drupal:8.8.0 and will be removed from Drupal 9.0.0. Use
 *   Drupal\Core\StreamWrapper\StreamWrapperManagerInterface::isValidScheme()
 *   instead.
 *
 * @see https://www.drupal.org/node/2418133
 * @see https://www.drupal.org/node/3035273
 */
function file_stream_wrapper_valid_scheme($scheme) {
  return \Drupal::service('file_system')->validScheme($scheme);
  @trigger_error('file_stream_wrapper_valid_scheme() is deprecated in drupal:8.0.0 and will be removed before drupal:9.0.0. Use \Drupal\Core\StreamWrapper\StreamWrapperManagerInterface::isValidScheme() instead. See https://www.drupal.org/node/3035273', E_USER_DEPRECATED);
  return \Drupal::service('stream_wrapper_manager')->isValidScheme($scheme);
}

/**
@@ -130,15 +135,15 @@ function file_stream_wrapper_valid_scheme($scheme) {
 *   For example, the URI "public://sample/test.txt" would return
 *   "sample/test.txt".
 *
 * @see file_uri_scheme()
 * @deprecated in drupal:8.8.0 and will be removed from drupal:9.0.0. Use
 *   \Drupal\Core\StreamWrapper\StreamWrapperManagerInterface::getTarget()
 *   instead.
 *
 * @see https://www.drupal.org/node/3035273
 */
function file_uri_target($uri) {
  // Remove the scheme from the URI and remove erroneous leading or trailing,
  // forward-slashes and backslashes.
  $target = trim(preg_replace('/^[\w\-]+:\/\/|^data:/', '', $uri), '\/');

  // If nothing was replaced, the URI doesn't have a valid scheme.
  return $target !== $uri ? $target : FALSE;
  @trigger_error('file_uri_target() is deprecated in drupal:8.8.0 and will be removed before drupal:9.0.0. Use \Drupal\Core\StreamWrapper\StreamWrapperManagerInterface::getTarget() instead. See https://www.drupal.org/node/3035273', E_USER_DEPRECATED);
  return StreamWrapperManager::getTarget($uri);
}

/**
@@ -171,19 +176,16 @@ function file_default_scheme() {
 *
 * @return string
 *   The normalized URI.
 *
 * @deprecated in drupal:8.8.0 and will be removed from drupal:9.0.0. Use
 *   \Drupal\Core\StreamWrapper\StreamWrapperManagerInterface::normalizeUri()
 *   instead.
 *
 * @see https://www.drupal.org/node/3035273
 */
function file_stream_wrapper_uri_normalize($uri) {
  $scheme = \Drupal::service('file_system')->uriScheme($uri);

  if (file_stream_wrapper_valid_scheme($scheme)) {
    $target = file_uri_target($uri);

    if ($target !== FALSE) {
      $uri = $scheme . '://' . $target;
    }
  }

  return $uri;
  @trigger_error('file_stream_wrapper_uri_normalize() is deprecated in drupal:8.8.0 and will be removed before drupal:9.0.0. Use \Drupal\Core\StreamWrapper\StreamWrapperManagerInterface::normalizeUri() instead. See https://www.drupal.org/node/3035273', E_USER_DEPRECATED);
  return \Drupal::service('stream_wrapper_manager')->normalizeUri($uri);
}

/**
@@ -216,7 +218,7 @@ function file_create_url($uri) {
  // file server.
  \Drupal::moduleHandler()->alter('file_url', $uri);

  $scheme = \Drupal::service('file_system')->uriScheme($uri);
  $scheme = StreamWrapperManager::getScheme($uri);

  if (!$scheme) {
    // Allow for:
@@ -364,8 +366,12 @@ function file_ensure_htaccess() {
 *   if one is already present. Defaults to FALSE.
 */
function file_save_htaccess($directory, $private = TRUE, $force_overwrite = FALSE) {
  if (\Drupal::service('file_system')->uriScheme($directory)) {
    $htaccess_path = file_stream_wrapper_uri_normalize($directory . '/.htaccess');

  /** @var \Drupal\Core\StreamWrapper\StreamWrapperManagerInterface $stream_wrapper_manager */
  $stream_wrapper_manager = \Drupal::service('stream_wrapper_manager');

  if ($stream_wrapper_manager::getScheme($directory)) {
    $htaccess_path = $stream_wrapper_manager->normalizeUri($directory . '/.htaccess');
  }
  else {
    $directory = rtrim($directory, '/\\');
@@ -421,14 +427,16 @@ function file_htaccess_lines($private = TRUE) {
 *
 * @return
 *   TRUE if the URI is allowed.
 *
 * @deprecated in drupal:8.8.0 and will be removed before drupal:9.0.0. Use
 *   \Drupal\Core\StreamWrapper\StreamWrapperManagerInterface::isValidUri()
 *   instead.
 *
 * @see https://www.drupal.org/node/3035273
 */
function file_valid_uri($uri) {
  // Assert that the URI has an allowed scheme. Bare paths are not allowed.
  $uri_scheme = \Drupal::service('file_system')->uriScheme($uri);
  if (!file_stream_wrapper_valid_scheme($uri_scheme)) {
    return FALSE;
  }
  return TRUE;
  @trigger_error('file_valid_uri() is deprecated in drupal:8.8.0 and will be removed before drupal:9.0.0. Use \Drupal\Core\StreamWrapper\StreamWrapperManagerInterface::isValidUri() instead. See https://www.drupal.org/node/3035273', E_USER_DEPRECATED);
  return \Drupal::service('stream_wrapper_manager')->isValidUri($uri);
}

/**
@@ -586,7 +594,9 @@ function file_unmanaged_prepare($source, &$destination = NULL, $replace = FILE_E
 */
function file_build_uri($path) {
  $uri = \Drupal::config('system.file')->get('default_scheme') . '://' . $path;
  return file_stream_wrapper_uri_normalize($uri);
  /** @var \Drupal\Core\StreamWrapper\StreamWrapperManagerInterface $stream_wrapper_manager */
  $stream_wrapper_manager = \Drupal::service('stream_wrapper_manager');
  return $stream_wrapper_manager->normalizeUri($uri);
}

/**
@@ -1001,7 +1011,9 @@ function file_scan_directory($dir, $mask, $options = [], $depth = 0) {
  ];
  // Normalize $dir only once.
  if ($depth == 0) {
    $dir = file_stream_wrapper_uri_normalize($dir);
    /** @var \Drupal\Core\StreamWrapper\StreamWrapperManagerInterface $stream_wrapper_manager */
    $stream_wrapper_manager = \Drupal::service('stream_wrapper_manager');
    $dir = $stream_wrapper_manager->normalizeUri($dir);
    $dir_has_slash = (substr($dir, -1) === '/');
  }

+2 −1
Original line number Diff line number Diff line
@@ -3,6 +3,7 @@
namespace Drupal\Core\Asset;

use Drupal\Component\Utility\Unicode;
use Drupal\Core\StreamWrapper\StreamWrapperManager;

/**
 * Optimizes a CSS asset.
@@ -103,7 +104,7 @@ public function loadFile($file, $optimize = NULL, $reset_basepath = TRUE) {

    // Stylesheets are relative one to each other. Start by adding a base path
    // prefix provided by the parent stylesheet (if necessary).
    if ($basepath && !file_uri_scheme($file)) {
    if ($basepath && !StreamWrapperManager::getScheme($file)) {
      $file = $basepath . '/' . $file;
    }
    // Store the parent base path to restore it later.
+24 −4
Original line number Diff line number Diff line
@@ -8,6 +8,7 @@
use Drupal\Core\Asset\Exception\LibraryDefinitionMissingLicenseException;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Serialization\Yaml;
use Drupal\Core\StreamWrapper\StreamWrapperManagerInterface;
use Drupal\Core\Theme\ThemeManagerInterface;
use Drupal\Component\Serialization\Exception\InvalidDataTypeException;
use Drupal\Component\Utility\NestedArray;
@@ -38,6 +39,13 @@ class LibraryDiscoveryParser {
   */
  protected $root;

  /**
   * The stream wrapper manager.
   *
   * @var \Drupal\Core\StreamWrapper\StreamWrapperManagerInterface
   */
  protected $streamWrapperManager;

  /**
   * Constructs a new LibraryDiscoveryParser instance.
   *
@@ -47,11 +55,18 @@ class LibraryDiscoveryParser {
   *   The module handler.
   * @param \Drupal\Core\Theme\ThemeManagerInterface $theme_manager
   *   The theme manager.
   * @param \Drupal\Core\StreamWrapper\StreamWrapperManagerInterface $stream_wrapper_manager
   *   The stream wrapper manager.
   */
  public function __construct($root, ModuleHandlerInterface $module_handler, ThemeManagerInterface $theme_manager) {
  public function __construct($root, ModuleHandlerInterface $module_handler, ThemeManagerInterface $theme_manager, StreamWrapperManagerInterface $stream_wrapper_manager = NULL) {
    $this->root = $root;
    $this->moduleHandler = $module_handler;
    $this->themeManager = $theme_manager;
    if (!$stream_wrapper_manager) {
      @trigger_error('Calling LibraryDiscoveryParser::__construct() without the $stream_wrapper_manager argument is deprecated in drupal:8.8.0. The $stream_wrapper_manager argument will be required in drupal:9.0.0. See https://www.drupal.org/node/3035273', E_USER_DEPRECATED);
      $stream_wrapper_manager = \Drupal::service('stream_wrapper_manager');
    }
    $this->streamWrapperManager = $stream_wrapper_manager;
  }

  /**
@@ -183,7 +198,7 @@ public function buildByExtension($extension) {
              }
            }
            // A stream wrapper URI (e.g., public://generated_js/example.js).
            elseif ($this->fileValidUri($source)) {
            elseif ($this->streamWrapperManager->isValidUri($source)) {
              $options['data'] = $source;
            }
            // A regular URI (e.g., http://example.com/example.js) without
@@ -393,10 +408,15 @@ protected function drupalGetPath($type, $name) {
  }

  /**
   * Wraps file_valid_uri().
   * Wraps \Drupal\Core\StreamWrapper\StreamWrapperManagerInterface::isValidUri().
   *
   * @deprecated in drupal:8.8.0 and will be removed before drupal:9.0.0. Use
   *   \Drupal\Core\StreamWrapper\StreamWrapperManagerInterface::isValidUri()
   *   instead.
   */
  protected function fileValidUri($source) {
    return file_valid_uri($source);
    @trigger_error('fileValidUri() is deprecated in Drupal 8.8.0 and will be removed before Drupal 9.0.0. Use \Drupal\Core\StreamWrapper\StreamWrapperManagerInterface::isValidUri() instead. See https://www.drupal.org/node/3035273', E_USER_DEPRECATED);
    return $this->streamWrapperManager->isValidUri($source);
  }

  /**
+15 −23
Original line number Diff line number Diff line
@@ -10,6 +10,7 @@
use Drupal\Core\File\Exception\FileWriteException;
use Drupal\Core\File\Exception\NotRegularFileException;
use Drupal\Core\Site\Settings;
use Drupal\Core\StreamWrapper\StreamWrapperManager;
use Drupal\Core\StreamWrapper\StreamWrapperManagerInterface;
use Psr\Log\LoggerInterface;

@@ -110,8 +111,7 @@ public function chmod($uri, $mode = NULL) {
   * {@inheritdoc}
   */
  public function unlink($uri, $context = NULL) {
    $scheme = $this->uriScheme($uri);
    if (!$this->validScheme($scheme) && (substr(PHP_OS, 0, 3) == 'WIN')) {
    if (!$this->streamWrapperManager->isValidUri($uri) && (substr(PHP_OS, 0, 3) == 'WIN')) {
      chmod($uri, 0600);
    }
    if ($context) {
@@ -140,9 +140,9 @@ public function realpath($uri) {
   * {@inheritdoc}
   */
  public function dirname($uri) {
    $scheme = $this->uriScheme($uri);
    $scheme = StreamWrapperManager::getScheme($uri);

    if ($this->validScheme($scheme)) {
    if ($this->streamWrapperManager->isValidScheme($scheme)) {
      return $this->streamWrapperManager->getViaScheme($scheme)->dirname($uri);
    }
    else {
@@ -181,7 +181,7 @@ public function mkdir($uri, $mode = NULL, $recursive = FALSE, $context = NULL) {

    // If the URI has a scheme, don't override the umask - schemes can handle
    // this issue in their own implementation.
    if ($this->uriScheme($uri)) {
    if (StreamWrapperManager::getScheme($uri)) {
      return $this->mkdirCall($uri, $mode, $recursive, $context);
    }

@@ -252,8 +252,7 @@ protected function mkdirCall($uri, $mode, $recursive, $context) {
   * {@inheritdoc}
   */
  public function rmdir($uri, $context = NULL) {
    $scheme = $this->uriScheme($uri);
    if (!$this->validScheme($scheme) && (substr(PHP_OS, 0, 3) == 'WIN')) {
    if (!$this->streamWrapperManager->isValidUri($uri) && (substr(PHP_OS, 0, 3) == 'WIN')) {
      chmod($uri, 0700);
    }
    if ($context) {
@@ -268,9 +267,9 @@ public function rmdir($uri, $context = NULL) {
   * {@inheritdoc}
   */
  public function tempnam($directory, $prefix) {
    $scheme = $this->uriScheme($directory);
    $scheme = StreamWrapperManager::getScheme($directory);

    if ($this->validScheme($scheme)) {
    if ($this->streamWrapperManager->isValidScheme($scheme)) {
      $wrapper = $this->streamWrapperManager->getViaScheme($scheme);

      if ($filename = tempnam($wrapper->getDirectoryPath(), $prefix)) {
@@ -290,22 +289,16 @@ public function tempnam($directory, $prefix) {
   * {@inheritdoc}
   */
  public function uriScheme($uri) {
    if (preg_match('/^([\w\-]+):\/\/|^(data):/', $uri, $matches)) {
      // The scheme will always be the last element in the matches array.
      return array_pop($matches);
    }

    return FALSE;
    @trigger_error('FileSystem::uriScheme() is deprecated in drupal:8.8.0. It will be removed from drupal:9.0.0. Use \Drupal\Core\StreamWrapper\StreamWrapperManagerInterface::getScheme() instead. See https://www.drupal.org/node/3035273', E_USER_DEPRECATED);
    return StreamWrapperManager::getScheme($uri);
  }

  /**
   * {@inheritdoc}
   */
  public function validScheme($scheme) {
    if (!$scheme) {
      return FALSE;
    }
    return class_exists($this->streamWrapperManager->getClass($scheme));
    @trigger_error('FileSystem::validScheme() is deprecated in drupal:8.8.0 and will be removed before drupal:9.0.0. Use \Drupal\Core\StreamWrapper\StreamWrapperManagerInterface::isValidScheme() instead. See https://www.drupal.org/node/3035273', E_USER_DEPRECATED);
    return $this->streamWrapperManager->isValidScheme($scheme);
  }

  /**
@@ -397,8 +390,7 @@ public function move($source, $destination, $replace = self::EXISTS_RENAME) {

    // Ensure compatibility with Windows.
    // @see \Drupal\Core\File\FileSystemInterface::unlink().
    $scheme = $this->uriScheme($source);
    if (!$this->validScheme($scheme) && (substr(PHP_OS, 0, 3) == 'WIN')) {
    if (!$this->streamWrapperManager->isValidUri($source) && (substr(PHP_OS, 0, 3) == 'WIN')) {
      chmod($source, 0600);
    }
    // Attempt to resolve the URIs. This is necessary in certain
@@ -481,7 +473,7 @@ protected function prepareDestination($source, &$destination, $replace) {
    // Prepare the destination directory.
    if ($this->prepareDirectory($destination)) {
      // The destination is already a directory, so append the source basename.
      $destination = file_stream_wrapper_uri_normalize($destination . '/' . $this->basename($source));
      $destination = $this->streamWrapperManager->normalizeUri($destination . '/' . $this->basename($source));
    }
    else {
      // Perhaps $destination is a dir/file?
@@ -537,7 +529,7 @@ public function saveData($data, $destination, $replace = self::EXISTS_RENAME) {
   * {@inheritdoc}
   */
  public function prepareDirectory(&$directory, $options = self::MODIFY_PERMISSIONS) {
    if (!$this->validScheme($this->uriScheme($directory))) {
    if (!$this->streamWrapperManager->isValidUri($directory)) {
      // Only trim if we're not dealing with a stream.
      $directory = rtrim($directory, '/\\');
    }
Loading