You are here

function advagg_ext_compress_execute_cmd in Advanced CSS/JS Aggregation 7.2

Compress Javascript using via command line.

Parameters

string $input_file: The file containing the uncompressed css/js data.

string $ext: The string css or js.

array $debug: Optional debug array.

Return value

string The filename containing the compressed css/js data.

1 call to advagg_ext_compress_execute_cmd()
advagg_ext_compress_string in advagg_ext_compress/advagg_ext_compress.module
Compress CSS using via command line.

File

advagg_ext_compress/advagg_ext_compress.module, line 94
Advanced CSS/JS aggregation external compression module.

Code

function advagg_ext_compress_execute_cmd($input_file, $ext = '', array &$debug = array()) {
  $run = variable_get("advagg_ext_compress_{$ext}_cmd", '');
  if (empty($run)) {
    return FALSE;
  }

  // Get file extension.
  if (empty($ext)) {
    $ext = strtolower(pathinfo($input_file, PATHINFO_EXTENSION));
    if ($ext !== 'css' && $ext !== 'js') {

      // Get the $ext from the database.
      $row = db_select('advagg_files', 'af')
        ->fields('af')
        ->condition('filename', $input_file)
        ->execute()
        ->fetchAssoc();
      if (!empty($row['filetype'])) {
        $ext = $row['filetype'];
      }
      if ($ext === 'less') {
        $ext = 'css';
      }
    }
  }

  // Generate temp file.
  $temp_file = drupal_tempnam('temporary://', 'advagg_file_');
  $new_temp_file = $temp_file . '.' . basename($input_file);
  @rename($temp_file, $new_temp_file);

  // Set the permissions on the temp file.
  drupal_chmod($new_temp_file);
  $output = advagg_get_relative_path($new_temp_file);

  // Create command to run.
  $cmd = str_replace(array(
    '{%CWD%}',
    '{%IN%}',
    '{%IN_URL_ENC%}',
    '{%OUT%}',
  ), array(
    DRUPAL_ROOT,
    $input_file,
    urlencode(file_create_url($input_file)),
    escapeshellarg(realpath($output)),
  ), $run);

  // Run command and return the output file.
  $shell_output = array();
  $return_var = 0;
  $shell = exec($cmd, $shell_output, $return_var);
  $debug = array(
    $cmd,
    $shell_output,
    $return_var,
    $shell,
  );

  // Cleanup leftover files.
  if (file_exists($temp_file)) {
    @unlink($temp_file);
  }
  return $output;
}