You are here

function file_test_file_url_alter in SimpleTest 7

Implement hook_file_url_alter().

File

tests/file_test.module, line 274
Helper module for the file tests.

Code

function file_test_file_url_alter(&$uri) {

  // Only run this hook when this variable is set. Otherwise, we'd have to add
  // another hidden test module just for this hook.
  if (!variable_get('file_test_hook_file_url_alter', FALSE)) {
    return;
  }
  $cdn_extensions = array(
    'css',
    'js',
    'gif',
    'jpg',
    'jpeg',
    'png',
  );

  // Most CDNs don't support private file transfers without a lot of hassle,
  // so don't support this in the common case.
  $schemes = array(
    'public',
  );
  $scheme = file_uri_scheme($uri);

  // Only serve shipped files and public created files from the CDN.
  if (!$scheme || in_array($scheme, $schemes)) {

    // Shipped files.
    if (!$scheme) {
      $path = $uri;
    }
    else {
      $wrapper = file_stream_wrapper_get_instance_by_scheme($scheme);
      $path = $wrapper
        ->getDirectoryPath() . '/' . file_uri_target($uri);
    }

    // Clean up Windows paths.
    $path = str_replace('\\', '/', $path);

    // Serve files with one of the CDN extensions from CDN 1, all others from
    // CDN 2.
    $pathinfo = pathinfo($path);
    if (array_key_exists('extension', $pathinfo) && in_array($pathinfo['extension'], $cdn_extensions)) {
      $uri = FILE_URL_TEST_CDN_1 . '/' . $path;
    }
    else {
      $uri = FILE_URL_TEST_CDN_2 . '/' . $path;
    }
  }
}