You are here

function file_test_file_url_alter in Drupal 7

Same name and namespace in other branches
  1. 8 core/modules/file/tests/file_test/file_test.module \file_test_file_url_alter()
  2. 9 core/modules/file/tests/file_test/file_test.module \file_test_file_url_alter()

Implements hook_file_url_alter().

File

modules/simpletest/tests/file_test.module, line 323
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.
  $alter_mode = variable_get('file_test_hook_file_url_alter', FALSE);
  if (!$alter_mode) {
    return;
  }
  elseif ($alter_mode == 'cdn') {
    $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;
      }
    }
  }
  elseif ($alter_mode == 'root-relative') {

    // Only serve shipped files and public created files with root-relative
    // URLs.
    $scheme = file_uri_scheme($uri);
    if (!$scheme || $scheme == 'public') {

      // 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);

      // Generate a root-relative URL.
      $uri = base_path() . '/' . $path;
    }
  }
  elseif ($alter_mode == 'protocol-relative') {

    // Only serve shipped files and public created files with protocol-relative
    // URLs.
    $scheme = file_uri_scheme($uri);
    if (!$scheme || $scheme == 'public') {

      // 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);

      // Generate a protocol-relative URL.
      $uri = '/' . base_path() . '/' . $path;
    }
  }
}