You are here

public function FileManager::scan in Minify JS 8

Same name and namespace in other branches
  1. 8.2 src/Controller/FileManager.php \Drupal\minifyjs\Controller\FileManager::scan()

Scans the system for javascript.

Parameters

bool $drush:

Return value

\Symfony\Component\HttpFoundation\RedirectResponse Returns a redirect to the manage javascript page.

2 calls to FileManager::scan()
drush_minifyjs_scan_js in ./minifyjs.drush.inc
Drush command logic. drush_[COMMAND_NAME]().
MinifyJsCommands::scanJs in src/Commands/MinifyJsCommands.php
Drush command to find all JS files.
1 string reference to 'FileManager::scan'
minifyjs.routing.yml in ./minifyjs.routing.yml
minifyjs.routing.yml

File

src/Controller/FileManager.php, line 63

Class

FileManager
Controller routines for minifyjs routes.

Namespace

Drupal\minifyjs\Controller

Code

public function scan($drush = FALSE) {

  // Recursive scan of the entire doc root to find .js files. Include
  // minified files as well so they can be re-minified (comments removed).
  $directory = new \RecursiveDirectoryIterator(DRUPAL_ROOT);
  $iterator = new \RecursiveIteratorIterator($directory);
  $regex = new \RegexIterator($iterator, '/\\.js$/i');

  // Process files.
  $new_files = [];
  $old_files = [];
  $changed_files = [];
  $existing = minifyjs_load_all_files();
  $exclusions = \Drupal::config('minifyjs.config')
    ->get('exclusion_list');
  foreach ($regex as $info) {
    $new_absolute = $info
      ->getPathname();
    $new_relative = str_replace(DRUPAL_ROOT . '/', '', $new_absolute);

    // skip exclusions
    if (\Drupal::service('path.matcher')
      ->matchPath($new_relative, $exclusions)) {
      continue;
    }

    // Loop existing and see if it already exists from previous scans.
    $exists = FALSE;
    foreach ($existing as $file) {
      if ($file->uri == $new_relative) {

        // See if the size and modified time differ from the last time the scan
        // checked this file. If the file has changed (based on those two
        // pieces of data), mark the minified version for removal if a minified
        // version of the file exists.
        if (!empty($file->minified_uri)) {
          $size = filesize($new_absolute);
          $modified = filemtime($new_absolute);
          if ($size != $file->size || $modified != $file->modified) {
            $changed_files[$new_relative] = $file;
          }
        }
        $exists = TRUE;
        $old_files[$new_relative] = TRUE;
        break;
      }
    }

    // File not found in the existing array, so it's new.
    if (!$exists) {
      $new_files[$new_absolute] = TRUE;
    }
  }

  // Build a list of files that currently exist in the minifyjs_file table but
  // no longer exist in the file system. These files should be removed.
  foreach ($existing as $file) {
    if (!isset($old_files[$file->uri])) {
      $this
        ->remove_file($file->uri);
    }
  }

  // Remove changed files.
  foreach ($changed_files as $file_uri => $file) {
    $this
      ->remove_file($file->uri);
    $new_files[$file_uri] = TRUE;
    drupal_set_message(t('Original file %file has been modified and was restored.', [
      '%file' => $file_uri,
    ]));
  }

  // Add all new files to the database.
  foreach ($new_files as $file => $junk) {
    \Drupal::database()
      ->insert('minifyjs_file')
      ->fields(array(
      'uri' => str_replace(DRUPAL_ROOT . '/', '', $file),
      'size' => filesize($file),
      'modified' => filemtime($file),
    ))
      ->execute();
  }

  // Clear the cache so all of these new files will be picked up.
  \Drupal::cache()
    ->delete(MINIFYJS_CACHE_CID);
  $return = TRUE;
  if (!$drush) {
    $return = $this
      ->redirect('minifyjs.manage');
  }
  return $return;
}