You are here

function advagg_js_compress_jshrink in Advanced CSS/JS Aggregation 7.2

Compress a JS string using jshrink.

Parameters

string $contents: Javascript string.

bool $log_errors: FALSE to disable logging to watchdog on failure.

1 string reference to 'advagg_js_compress_jshrink'
advagg_js_compress_configuration in advagg_js_compress/advagg_js_compress.module
Generate the js compress configuration.

File

advagg_js_compress/advagg_js_compress.php53.inc, line 19
Advanced CSS/JS aggregation js compression php 5.3+ functions.

Code

function advagg_js_compress_jshrink(&$contents, $log_errors = TRUE) {
  $no_errors = TRUE;
  $contents_before = $contents;

  // Set max nesting level.
  if (!class_exists('JShrink\\Minifier')) {
    $nesting_level = ini_get('xdebug.max_nesting_level');
    if (!empty($nesting_level) && $nesting_level < 200) {
      ini_set('xdebug.max_nesting_level', 200);
    }
  }

  // Try libraries for JShrink.
  if (is_callable('libraries_load')) {
    libraries_load('JShrink');
  }

  // Only include jshrink.inc if the JShrink\Minifier class doesn't exist.
  if (!class_exists('JShrink\\Minifier')) {
    include drupal_get_path('module', 'advagg_js_compress') . '/jshrink.inc';
  }
  ob_start();
  try {

    // JShrink the contents of the aggregated file.
    $contents = Minifier::minify($contents, array(
      'flaggedComments' => FALSE,
    ));

    // Capture any output from JShrink.
    $error = trim(ob_get_contents());
    if (!empty($error)) {
      $no_errors = FALSE;
      throw new Exception($error);
    }
  } catch (Exception $e) {
    $no_errors = FALSE;

    // Log the JShrink exception and rollback to uncompressed content.
    if ($log_errors) {
      watchdog('advagg_js_compress', '@message <pre> @contents </pre>', array(
        '@message' => $e
          ->getMessage(),
        '@contents' => $contents_before,
      ), WATCHDOG_WARNING);
    }
    $contents = $contents_before;
  }
  ob_end_clean();
  return $no_errors;
}