function FileTestHooks::fileUrlAlter

Implements hook_file_url_alter().

File

core/modules/file/tests/file_test/src/Hook/FileTestHooks.php, line 88

Class

FileTestHooks
Hook implementations for file_test.

Namespace

Drupal\file_test\Hook

Code

public function fileUrlAlter(&$uri) : void {
    
    /** @var \Drupal\Core\StreamWrapper\StreamWrapperManagerInterface $stream_wrapper_manager */
    $stream_wrapper_manager = \Drupal::service('stream_wrapper_manager');
    // 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 = \Drupal::state()->get('file_test.hook_file_url_alter');
    if (!$alter_mode) {
        return;
    }
    elseif ($alter_mode == 'cdn') {
        $cdn_extensions = [
            '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 = [
            'public',
        ];
        $scheme = $stream_wrapper_manager::getScheme($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 = $stream_wrapper_manager->getViaScheme($scheme);
                $path = $wrapper->getDirectoryPath() . '/' . $stream_wrapper_manager::getTarget($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 = $stream_wrapper_manager::getScheme($uri);
        if (!$scheme || $scheme == 'public') {
            // Shipped files.
            if (!$scheme) {
                $path = $uri;
            }
            else {
                $wrapper = $stream_wrapper_manager->getViaScheme($scheme);
                $path = $wrapper->getDirectoryPath() . '/' . $stream_wrapper_manager::getTarget($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 = $stream_wrapper_manager::getScheme($uri);
        if (!$scheme || $scheme == 'public') {
            // Shipped files.
            if (!$scheme) {
                $path = $uri;
            }
            else {
                $wrapper = $stream_wrapper_manager->getViaScheme($scheme);
                $path = $wrapper->getDirectoryPath() . '/' . $stream_wrapper_manager::getTarget($uri);
            }
            // Clean up Windows paths.
            $path = str_replace('\\', '/', $path);
            // Generate a protocol-relative URL.
            $uri = '/' . base_path() . '/' . $path;
        }
    }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.