public function MinifyJs::scan in Minify JS 8.2
Scan for files.
Recursively scan the entire doc tree looking for JS files, ignoring based on the exclusion list.
File
- src/
MinifyJs.php, line 105
Class
- MinifyJs
- Minify JS Service.
Namespace
Drupal\minifyjsCode
public function scan() {
// 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 = $this
->loadAllFiles();
$exclusions = \Drupal::config('minifyjs.config')
->get('exclusion_list');
foreach ($regex as $info) {
$new_absolute = $info
->getPathname();
$new_relative = str_replace(DRUPAL_ROOT . DIRECTORY_SEPARATOR, '', $new_absolute);
// Skip exclusions.
if ($this->pathMatcher
->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
->removeFile($file->uri);
}
}
// Remove changed files.
foreach ($changed_files as $file_uri => $file) {
$this
->removeFile($file->uri);
$new_files[$file_uri] = TRUE;
\Drupal::messenger()
->addMessage(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([
'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.
\Drupal::cache()
->delete(MINIFYJS_CACHE_CID);
}