function minifyjs_minify_file in Minify JS 8
Same name and namespace in other branches
- 7 minifyjs.admin.inc \minifyjs_minify_file()
Helper function that sends the JS off to be minified, handles the response, stores the file in the filesystem and stores the file info in the managed file tables.
Parameters
int $fid:
bool $reset:
Return value
mixed
6 calls to minifyjs_minify_file()
- drush_minifyjs_minify_js in ./
minifyjs.drush.inc - Drush command logic. drush_[COMMAND_NAME]().
- drush_minifyjs_minify_js_skip in ./
minifyjs.drush.inc - Drush command logic. drush_[COMMAND_NAME]().
- FileManager::minify in src/
Controller/ FileManager.php - Minify a single file.
- MinifyJsCommands::minifyJs in src/
Commands/ MinifyJsCommands.php - All js files minification.
- MinifyJsCommands::minifyJsSkip in src/
Commands/ MinifyJsCommands.php - Minify all JS files that are not currently minified.
File
- ./
minifyjs.module, line 168
Code
function minifyjs_minify_file($fid, $reset = FALSE) {
// Load the file by fid.
$files = minifyjs_load_all_files();
$file = $files[$fid];
$js = file_get_contents(DRUPAL_ROOT . '/' . $file->uri);
// Minify the JS, if it has a length. 0 byte files should pass by the
// minification process.
$minified = $js;
if (strlen($js)) {
$minifier = new JSqueeze();
$minified = $minifier
->squeeze($js, TRUE, FALSE);
}
// Create the directory tree if it doesn't exist.
$minifyjs_folder = 'public://minifyjs/' . dirname($file->uri);
if (!is_dir($minifyjs_folder)) {
mkdir($minifyjs_folder, Settings::get('file_chmod_directory', 0775), TRUE);
}
// Save the file first to the temp folder and then copy to the
// public filesystem.
$file_name = str_replace('.js', '.min.js', basename($file->uri));
$tmp_file = file_directory_temp() . '/' . $file_name;
$file_uri = 'public://minifyjs/' . dirname($file->uri) . '/' . $file_name;
if (file_put_contents($tmp_file, $minified) !== FALSE) {
if (copy($tmp_file, $file_uri)) {
// save the file in the managed file table.
if (empty($file->minified_uri)) {
$file = File::create([
'uid' => \Drupal::currentUser()
->id(),
'uri' => $file_uri,
'filename' => $file_name,
'filemime' => \Drupal::service('file.mime_type.guesser')
->guess($file->uri),
'status' => FILE_STATUS_PERMANENT,
]);
$file
->save();
\Drupal::service('file.usage')
->add($file, 'minifyjs', 'node', 1);
}
$filesize = filesize($file_uri);
// update the minifyjs table
\Drupal::database()
->update('minifyjs_file')
->fields([
'minified_uri' => $file_uri,
'minified_size' => $filesize ? $filesize : 0,
'minified_modified' => REQUEST_TIME,
])
->condition('fid', $fid)
->execute();
// Clean up temp folder
unlink($tmp_file);
// Clear the cache so this change will be reflected in
// minifyjs_load_all_files()
if ($reset) {
\Drupal::cache()
->delete(MINIFYJS_CACHE_CID);
}
return TRUE;
}
else {
return t('Could not copy the file from the %tmp folder.', [
'%tmp' => file_directory_temp(),
]);
}
}
else {
return t('Could not save the file - %file', [
'%file' => file_directory_temp() . '/' . $file_name,
]);
}
}