You are here

function minifyjs_minify_file in Minify JS 7

Same name and namespace in other branches
  1. 8 minifyjs.module \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.

4 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]().
minifyjs_create_minified_file in ./minifyjs.admin.inc
Helper function to create the minified file:
minifyjs_minify_file_operation in ./minifyjs.admin.inc
Helper function for batch, minify file operation.

File

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

Code

function minifyjs_minify_file($fid, $reset = FALSE) {
  global $user;

  // Load the file by fid.
  $files = minifyjs_load_all_files();
  $file = $files[$fid];
  $js = file_get_contents(DRUPAL_ROOT . DIRECTORY_SEPARATOR . $file->uri);

  // Minify the 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, variable_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() . DIRECTORY_SEPARATOR . $file_name;
  $file_uri = 'public://minifyjs/' . dirname($file->uri) . DIRECTORY_SEPARATOR . $file_name;
  if (file_put_contents($tmp_file, $minified)) {
    if (copy($tmp_file, $file_uri)) {

      // save the file in the managed file table.
      if (empty($file->minified_uri)) {
        $file = new stdClass();
        $file->fid = NULL;
        $file->uri = $file_uri;
        $file->filename = $file_name;
        $file->filemime = file_get_mimetype($file->uri);
        $file->uid = $user->uid;
        $file->status = FILE_STATUS_PERMANENT;
        $file = file_save($file);
        file_usage_add($file, 'minifyjs', 'node', 1);
      }
      $filesize = filesize($file_uri);

      // update the minifyjs table
      db_update('minifyjs_file')
        ->fields(array(
        '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) {
        cache_clear_all(MINIFYJS_CACHE_CID, 'cache');
      }
      return TRUE;
    }
    else {
      return t('Could not copy the file from the %tmp folder.', array(
        '%tmp' => file_directory_temp(),
      ));
    }
  }
  else {
    return t('Could not save the file - %file', array(
      '%file' => file_directory_temp() . DIRECTORY_SEPARATOR . $file_name,
    ));
  }
}