public function FileUrlGenerator::generate in Drupal 10
Same name and namespace in other branches
- 9 core/lib/Drupal/Core/File/FileUrlGenerator.php \Drupal\Core\File\FileUrlGenerator::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 {
$this->moduleHandler
->alter('file_url', $uri);
$scheme = StreamWrapperManager::getScheme($uri);
if (!$scheme) {
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 {
$options = UrlHelper::parse($uri);
return Url::fromUri('base:' . UrlHelper::encodePath($options['path']), $options);
}
}
elseif ($scheme == 'http' || $scheme == 'https' || $scheme == 'data') {
$options = UrlHelper::parse($uri);
return Url::fromUri(urldecode($options['path']), $options);
}
elseif ($wrapper = $this->streamWrapperManager
->getViaUri($uri)) {
$external_url = $wrapper
->getExternalUrl();
$options = UrlHelper::parse($external_url);
if (UrlHelper::externalIsLocal($external_url, \Drupal::service('router.request_context')
->getCompleteBaseUrl())) {
return Url::fromUri('base:' . $this
->transformRelative(urldecode($options['path']), FALSE), $options);
}
else {
return Url::fromUri(urldecode($options['path']), $options);
}
}
throw new InvalidStreamWrapperException();
}