Compress Javascript using via command line.

Paramètres

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 dans advagg_ext_compress/advagg_ext_compress.module
Compress CSS using via command line.

Fichier

advagg_ext_compress/advagg_ext_compress.module, line 94

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;
}