Commit dd907896 authored by catch's avatar catch
Browse files

Issue #3035312 by kim.pepper, andypost, martin107, yogeshmpawar,...

Issue #3035312 by kim.pepper, andypost, martin107, yogeshmpawar, naveenvalecha, pguillard, Berdir, dww, claudiu.cristea, jibran, Mile23: Move file_scan_directory() to file_system service
parent 5d68f9fc
Loading
Loading
Loading
Loading
+12 −70
Original line number Diff line number Diff line
@@ -992,81 +992,23 @@ function file_unmanaged_save_data($data, $destination = NULL, $replace = FILE_EX
 * @return
 *   An associative array (keyed on the chosen key) of objects with 'uri',
 *   'filename', and 'name' properties corresponding to the matched files.
 *
 * @deprecated in drupal:8.8.0 and is removed from drupal:9.0.0.
 *   Use \Drupal\Core\File\FileSystemInterface::scanDirectory() instead.
 *
 * @see https://www.drupal.org/node/3038437
 */
function file_scan_directory($dir, $mask, $options = [], $depth = 0) {
  // Merge in defaults.
  $options += [
    'callback' => 0,
    'recurse' => TRUE,
    'key' => 'uri',
    'min_depth' => 0,
  ];
  // Normalize $dir only once.
  if ($depth == 0) {
    /** @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) === '/');
  }

  // Allow directories specified in settings.php to be ignored. You can use this
  // to not check for files in common special-purpose directories. For example,
  // node_modules and bower_components. Ignoring irrelevant directories is a
  // performance boost.
  if (!isset($options['nomask'])) {
    $ignore_directories = Settings::get('file_scan_ignore_directories', []);
    array_walk($ignore_directories, function (&$value) {
      $value = preg_quote($value, '/');
    });
    $default_nomask = '/^' . implode('|', $ignore_directories) . '$/';
  }

  $options['key'] = in_array($options['key'], ['uri', 'filename', 'name']) ? $options['key'] : 'uri';
  @trigger_error('file_scan_directory() is deprecated in drupal:8.8.0 and is removed from drupal:9.0.0. Use \Drupal\Core\File\FileSystemInterface::scanDirectory() instead. See https://www.drupal.org/node/3038437', E_USER_DEPRECATED);
  $files = [];
  // Avoid warnings when opendir does not have the permissions to open a
  // directory.
  try {
    if (is_dir($dir)) {
    if ($handle = @opendir($dir)) {
      while (FALSE !== ($filename = readdir($handle))) {
        // Skip this file if it matches the nomask or starts with a dot.
        if ($filename[0] != '.'
          && !(isset($options['nomask']) && preg_match($options['nomask'], $filename))
          && !(!empty($default_nomask) && preg_match($default_nomask, $filename))
          ) {
          if ($depth == 0 && $dir_has_slash) {
            $uri = "$dir$filename";
          }
          else {
            $uri = "$dir/$filename";
      $files = \Drupal::service('file_system')->scanDirectory($dir, $mask, $options);
    }
          if ($options['recurse'] && is_dir($uri)) {
            // Give priority to files in this folder by merging them in after
            // any subdirectory files.
            $files = array_merge(file_scan_directory($uri, $mask, $options, $depth + 1), $files);
          }
          elseif ($depth >= $options['min_depth'] && preg_match($mask, $filename)) {
            // Always use this match over anything already set in $files with
            // the same $options['key'].
            $file = new stdClass();
            $file->uri = $uri;
            $file->filename = $filename;
            $file->name = pathinfo($filename, PATHINFO_FILENAME);
            $key = $options['key'];
            $files[$file->$key] = $file;
            if ($options['callback']) {
              $options['callback']($uri);
            }
          }
        }
      }

      closedir($handle);
    }
    else {
      \Drupal::logger('file')->error('@dir can not be opened', ['@dir' => $dir]);
  }
  catch (FileException $e) {
    // Ignore and return empty array for BC.
  }

  return $files;
}

+5 −3
Original line number Diff line number Diff line
@@ -444,7 +444,9 @@ function install_begin_request($class_loader, &$install_state) {
  else {
    $directory = $site_path . '/files/translations';
  }
  $container->set('string_translator.file_translation', new FileTranslation($directory));
  /** @var \Drupal\Core\File\FileSystemInterface $file_system */
  $file_system = $container->get('file_system');
  $container->set('string_translator.file_translation', new FileTranslation($directory, $file_system));
  $container->get('string_translation')
    ->addTranslator($container->get('string_translator.file_translation'));

@@ -1324,9 +1326,9 @@ function _install_select_profile(&$install_state) {
 *
 * @return
 *   An associative array of file URIs keyed by language code. URIs as
 *   returned by file_scan_directory().
 *   returned by FileSystemInterface::scanDirectory().
 *
 * @see file_scan_directory()
 * @see \Drupal\Core\File\FileSystemInterface::scanDirectory()
 */
function install_find_translations() {
  $translations = [];
+8 −6
Original line number Diff line number Diff line
@@ -5,15 +5,15 @@
 * API functions for installing modules and themes.
 */

use Drupal\Core\Extension\Dependency;
use Drupal\Component\Utility\Unicode;
use Drupal\Core\File\FileSystemInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Drupal\Component\Utility\Crypt;
use Drupal\Component\Utility\OpCodeCache;
use Drupal\Component\Utility\Unicode;
use Drupal\Component\Utility\UrlHelper;
use Drupal\Core\Extension\Dependency;
use Drupal\Core\Extension\ExtensionDiscovery;
use Drupal\Core\File\FileSystemInterface;
use Drupal\Core\Site\Settings;
use Symfony\Component\HttpFoundation\RedirectResponse;

/**
 * Requirement severity -- Informational message only.
@@ -166,9 +166,11 @@ function drupal_get_database_types() {

  // The internal database driver name is any valid PHP identifier.
  $mask = '/^' . DRUPAL_PHP_FUNCTION_PATTERN . '$/';
  $files = file_scan_directory(DRUPAL_ROOT . '/core/lib/Drupal/Core/Database/Driver', $mask, ['recurse' => FALSE]);
  /** @var \Drupal\Core\File\FileSystemInterface $file_system */
  $file_system = \Drupal::service('file_system');
  $files = $file_system->scanDirectory(DRUPAL_ROOT . '/core/lib/Drupal/Core/Database/Driver', $mask, ['recurse' => FALSE]);
  if (is_dir(DRUPAL_ROOT . '/drivers/lib/Drupal/Driver/Database')) {
    $files += file_scan_directory(DRUPAL_ROOT . '/drivers/lib/Drupal/Driver/Database/', $mask, ['recurse' => FALSE]);
    $files += $file_system->scanDirectory(DRUPAL_ROOT . '/drivers/lib/Drupal/Driver/Database/', $mask, ['recurse' => FALSE]);
  }
  foreach ($files as $file) {
    if (file_exists($file->uri . '/Install/Tasks.php')) {
+4 −1
Original line number Diff line number Diff line
@@ -216,7 +216,10 @@ function drupal_find_theme_templates($cache, $extension, $path) {
  // Escape the periods in the extension.
  $regex = '/' . str_replace('.', '\.', $extension) . '$/';
  // Get a listing of all template files in the path to search.
  $files = file_scan_directory($path, $regex, ['key' => 'filename']);
  $files = [];
  if (is_dir($path)) {
    $files = \Drupal::service('file_system')->scanDirectory($path, $regex, ['key' => 'filename']);
  }

  // Find templates that implement registered theme hooks and include that in
  // what is returned so that the registry knows that the theme has this
+3 −1
Original line number Diff line number Diff line
@@ -196,7 +196,9 @@ public function deleteAll() {
        $this->fileSystem->delete($uri);
      }
    };
    file_scan_directory('public://css', '/.*/', ['callback' => $delete_stale]);
    if (is_dir('public://css')) {
      $this->fileSystem->scanDirectory('public://css', '/.*/', ['callback' => $delete_stale]);
    }
  }

}
Loading