You are here

function minifyjs_scan_for_javascript_files in Minify JS 7

Helper function to scan the file system for javascript files:

  • admin/config/development/performance/js/scan.
1 call to minifyjs_scan_for_javascript_files()
drush_minifyjs_scan_js in ./minifyjs.drush.inc
Drush command logic. drush_[COMMAND_NAME]().
1 string reference to 'minifyjs_scan_for_javascript_files'
minifyjs_menu in ./minifyjs.module
Implements hook_menu()

File

./minifyjs.admin.inc, line 210
Hook and helper functions for the Minify JS module.

Code

function minifyjs_scan_for_javascript_files($drush = FALSE) {

  // Recursive scan of the entire doc root to find .js files (excluding .min.js).
  $directory = new RecursiveDirectoryIterator(DRUPAL_ROOT);
  $iterator = new RecursiveIteratorIterator($directory);
  $regex = new RegexIterator($iterator, '/^.+(?<!\\.min)\\.js$/i');

  // Process files.
  $new_files = array();
  $old_files = array();
  $changed_files = array();
  $existing = minifyjs_load_all_files();
  $exclusions = variable_get('minifyjs_exclusion_list');
  foreach ($regex as $info) {
    $new_absolute = $info
      ->getPathname();
    $new_relative = str_replace(DRUPAL_ROOT . DIRECTORY_SEPARATOR, '', $new_absolute);

    // skip exclusions
    if (drupal_match_path($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])) {
      minifyjs_remove_file($file->uri);
    }
  }

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

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

  // Clear the cache so all of these new files will be picked up.
  cache_clear_all(MINIFYJS_CACHE_CID, 'cache');
  if (!$drush) {
    drupal_goto('admin/config/development/performance/js');
  }
}