You are here

function advagg_missing_generate in Advanced CSS/JS Aggregation 7.2

Generates a missing CSS/JS file and send it to client.

Return value

string text if bundle couldn't be generated.

1 call to advagg_missing_generate()
advagg_missing_aggregate in ./advagg.missing.inc
Menu Callback; generates a missing CSS/JS file.

File

./advagg.missing.inc, line 64
Advanced CSS/JS aggregation module.

Code

function advagg_missing_generate($input = '') {

  // Make sure we are not in a redirect loop.
  $redirect_counter = isset($_GET['redirect_counter']) ? intval($_GET['redirect_counter']) : 0;
  if ($redirect_counter > 5) {
    watchdog('advagg', 'This request could not generate correctly. Loop detected. Request data: %info', array(
      '%info' => $_GET['q'],
    ));
    return t('In a loop.');
  }

  // Get filename from request.
  $arg = arg();
  $filename = array_pop($arg);
  $filename = explode('?', $filename);
  $filename = array_shift($filename);

  // Quit ASAP if filename does not match the AdvAgg pattern.
  $data = advagg_get_hashes_from_filename($filename);
  if (is_string($data) || !is_array($data)) {

    // Try again with the function input.
    $filename = $input;
    $data1 = advagg_get_hashes_from_filename($filename);
    if (is_string($data1) || !is_array($data1)) {
      return "{$data} {$data1}";
    }
    else {
      $data = $data1;
    }
  }

  // Check to see if the file exists.
  list($css_path, $js_path) = advagg_get_root_files_dir();
  if ($data[0] === 'css') {
    $uri = $css_path[0] . '/' . $filename;
  }
  elseif ($data[0] === 'js') {
    $uri = $js_path[0] . '/' . $filename;
  }
  if (file_exists($uri) && filesize($uri) >= 0) {

    // File does exist and filesize is bigger than zero, 307 to it.
    $uri = advagg_generate_location_uri($filename, $data[0], $data[3]);
    ++$redirect_counter;
    $uri .= '?redirect_counter=' . $redirect_counter;
    header('Location: ' . $uri, TRUE, 307);
    exit;
  }

  // Get lock so only one process will do the work.
  $lock_name = 'advagg_' . $filename;
  $uri = $GLOBALS['base_path'] . $_GET['q'];
  $created = FALSE;
  $files_to_save = array();
  if (variable_get('advagg_no_locks', ADVAGG_NO_LOCKS)) {
    $return = advagg_missing_create_file($filename, FALSE, $data);
    if (!is_array($return)) {
      return $return;
    }
    else {
      list($files_to_save, $type) = $return;
      $created = TRUE;
    }
  }
  elseif (lock_acquire($lock_name, 10) || $redirect_counter > 4) {
    if ($redirect_counter > 4) {
      $return = advagg_missing_create_file($filename, TRUE, $data);
    }
    else {
      $return = advagg_missing_create_file($filename, FALSE, $data);
    }
    lock_release($lock_name);
    if (!is_array($return)) {
      return $return;
    }
    else {
      list($files_to_save, $type) = $return;
      $created = TRUE;
    }
  }
  else {

    // Wait for another request that is already doing this work.
    // We choose to block here since otherwise the router item may not
    // be available in menu_execute_active_handler() resulting in a 404.
    lock_wait($lock_name, 10);
    if (file_exists($uri) && filesize($uri) > 0) {
      $type = $data[0];
      $created = TRUE;
    }
  }

  // Redirect and try again on failure.
  if (empty($created)) {
    $uri = advagg_generate_location_uri($filename, $data[0], $data[3]);
    ++$redirect_counter;
    $uri .= '?redirect_counter=' . $redirect_counter;
    header('Location: ' . $uri, TRUE, 307);
    exit;
  }
  if ($redirect_counter > 4) {
    watchdog('advagg', 'One of the alter hooks failed when generating this file: %uri. Thus this file was created without any alter hooks.', array(
      '%uri' => $uri,
    ), WATCHDOG_ERROR);
  }

  // Output file's contents if creating the file was successful.
  // This function will call exit.
  advagg_missing_send_saved_file($files_to_save, $uri, $created, $filename, $type, $redirect_counter, $data[3]);
}