You are here

function speedy_js_alter in Speedy 7

Implements hook_library_alter().

If minified core JS exists for the current version of Drupal swap it out.

File

./speedy.module, line 32
Improve the front end performance of your site.

Code

function speedy_js_alter(&$javascript) {

  // Ensure that Speedy is enabled
  if (!variable_get('speedy_js_production', TRUE)) {
    return;
  }

  // Manage the map of replacements. This is cached so we don't have to hit the
  // file system on each pageload to do the swap.
  static $replacement_map = NULL;
  if (is_null($replacement_map)) {
    if ($cache = cache_get('speedy_min_js_files')) {
      $replacement_map = $cache->data;
    }
    else {
      $directory = DRUPAL_ROOT . '/' . drupal_get_path('module', 'speedy') . '/js/' . VERSION;
      $directory_strlen = strlen($directory . '/');
      $all_files = file_scan_directory($directory, '/.js/');
      $replacement_map = array();
      foreach ($all_files as $file) {
        $replacement_map[] = substr($file->uri, $directory_strlen);
      }
      cache_set('speedy_min_js_files', $replacement_map, 'cache', CACHE_TEMPORARY);
    }
  }
  $new_path = drupal_get_path('module', 'speedy') . '/js/' . VERSION . '/';
  foreach ($javascript as $name => $data) {
    if ($data['type'] == 'file' && in_array($data['data'], $replacement_map)) {
      $javascript[$name]['data'] = $new_path . $data['data'];
    }
  }
}