You are here

public function FileUrlGenerator::generate in Drupal 9

Creates a root-relative web-accessible URL object.

Parameters

string $uri: The URI to a file for which we need an external URL, or the path to a shipped file.

Return value

\Drupal\Core\Url For a local URL (matching domain), a base-relative Url object containing a URL that may be used to access the file. An Url object with absolute URL may be returned when using a CDN or a remote stream wrapper. Use setAbsolute() on the Url object to build an absolute URL.

Throws

\Drupal\Core\File\Exception\InvalidStreamWrapperException If a stream wrapper could not be found to generate an external URL.

Overrides FileUrlGeneratorInterface::generate

File

core/lib/Drupal/Core/File/FileUrlGenerator.php, line 154

Class

FileUrlGenerator
Default implementation for the file URL generator service.

Namespace

Drupal\Core\File

Code

public function generate(string $uri) : Url {

  // Allow the URI to be altered, e.g. to serve a file from a CDN or static
  // file server.
  $this->moduleHandler
    ->alter('file_url', $uri);
  $scheme = StreamWrapperManager::getScheme($uri);
  if (!$scheme) {

    // Allow for:
    // - root-relative URIs (e.g. /foo.jpg in http://example.com/foo.jpg)
    // - protocol-relative URIs (e.g. //bar.jpg, which is expanded to
    //   http://example.com/bar.jpg by the browser when viewing a page over
    //   HTTP and to https://example.com/bar.jpg when viewing a HTTPS page)
    // Both types of relative URIs are characterized by a leading slash, hence
    // we can use a single check.
    if (mb_substr($uri, 0, 2) == '//') {
      return Url::fromUri($uri);
    }
    elseif (mb_substr($uri, 0, 1) == '/') {
      return Url::fromUri('base:' . str_replace($this->requestStack
        ->getCurrentRequest()
        ->getBasePath(), '', $uri));
    }
    else {

      // If this is not a properly formatted stream, then it is a shipped
      // file. Therefore, return the urlencoded URI.
      $options = UrlHelper::parse($uri);
      return Url::fromUri('base:' . UrlHelper::encodePath($options['path']), $options);
    }
  }
  elseif ($scheme == 'http' || $scheme == 'https' || $scheme == 'data') {

    // Check for HTTP and data URI-encoded URLs so that we don't have to
    // implement getExternalUrl() for the HTTP and data schemes.
    $options = UrlHelper::parse($uri);
    return Url::fromUri(urldecode($options['path']), $options);
  }
  elseif ($wrapper = $this->streamWrapperManager
    ->getViaUri($uri)) {

    // Attempt to return an external URL using the appropriate wrapper.
    return Url::fromUri('base:' . $this
      ->transformRelative(urldecode($wrapper
      ->getExternalUrl()), FALSE));
  }
  throw new InvalidStreamWrapperException();
}