class FileUrlGenerator implements FileUrlGeneratorInterface (View source)

Default implementation for the file URL generator service.

Properties

protected StreamWrapperManagerInterface $streamWrapperManager

The stream wrapper manager.

protected RequestStack $requestStack

The request stack.

protected ModuleHandlerInterface $moduleHandler

The module handler.

Methods

__construct(StreamWrapperManagerInterface $stream_wrapper_manager, RequestStack $request_stack, ModuleHandlerInterface $module_handler)

Constructs a new file URL generator object.

string
generateString(string $uri)

Creates a root-relative web-accessible URL string.

string
generateAbsoluteString(string $uri)

Creates an absolute web-accessible URL string.

string
doGenerateString(string $uri, bool $relative)

Creates an absolute web-accessible URL string.

string
generatePath(string $base_url, string $uri)

Generate a URL path.

Url
generate(string $uri)

Creates a root-relative web-accessible URL object.

string
transformRelative(string $file_url, bool $root_relative = TRUE)

Transforms an absolute URL of a local file to a relative URL.

Details

__construct(StreamWrapperManagerInterface $stream_wrapper_manager, RequestStack $request_stack, ModuleHandlerInterface $module_handler)

Constructs a new file URL generator object.

Parameters

StreamWrapperManagerInterface $stream_wrapper_manager

The stream wrapper manager.

RequestStack $request_stack

The request stack.

ModuleHandlerInterface $module_handler

The module handler.

string generateString(string $uri)

Creates a root-relative web-accessible URL string.

Parameters

string $uri

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

Return Value

string

For a local URL (matching domain), a root-relative string containing a URL that may be used to access the file. An absolute URL may be returned when using a CDN or a remote stream wrapper.

Exceptions

InvalidStreamWrapperException

string generateAbsoluteString(string $uri)

Creates an absolute web-accessible URL string.

Parameters

string $uri

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

Return Value

string

An absolute string containing a URL that may be used to access the file.

Exceptions

InvalidStreamWrapperException

protected string doGenerateString(string $uri, bool $relative)

Creates an absolute web-accessible URL string.

Parameters

string $uri

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

bool $relative

Whether to return an relative or absolute URL.

Return Value

string

An absolute string containing a URL that may be used to access the file.

Exceptions

InvalidStreamWrapperException

protected string generatePath(string $base_url, string $uri)

Generate a URL path.

Parameters

string $base_url

The base URL.

string $uri

The URI.

Return Value

string

The URL path.

Url generate(string $uri)

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

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.

Exceptions

InvalidStreamWrapperException

string transformRelative(string $file_url, bool $root_relative = TRUE)

Transforms an absolute URL of a local file to a relative URL.

May be useful to prevent problems on multisite set-ups and prevent mixed content errors when using HTTPS + HTTP.

Parameters

string $file_url

A file URL of a local file as generated by \Drupal\Core\File\FileUrlGenerator::generate().

bool $root_relative

(optional) TRUE if the URL should be relative to the root path or FALSE if relative to the Drupal base path.

Return Value

string

If the file URL indeed pointed to a local file and was indeed absolute, then the transformed, relative URL to the local file. Otherwise: the original value of $file_url.