Open Data Catalog v2.0.0
UrlHostTokenResolver.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Drupal\common;
4 
11  const TOKEN = "h-o.st";
12  const PUBLIC_SCHEME = 'public://';
13 
20  public static function getServerPublicFilesUrl(): ?string {
21  // Get public file stream.
22  $public_stream = \Drupal::service('stream_wrapper_manager')
23  ->getViaUri(self::PUBLIC_SCHEME);
24  // Retrieve the URL path for the public stream.
25  return $public_stream ? $public_stream->getExternalUrl() : NULL;
26  }
27 
37  public static function resolve(string $resourceUrl): string {
38  // Get HTTP server public files URL and extract the host.
39  $serverPublicFilesUrl = self::getServerPublicFilesUrl();
40  $serverPublicFilesUrl = isset($serverPublicFilesUrl) ? parse_url($serverPublicFilesUrl) : NULL;
41  $serverHost = $serverPublicFilesUrl['host'] ?? \Drupal::request()->getHost();
42  // Determine whether the localhost token is present in the resource URL, and
43  // replace the token if necessary.
44  if (substr_count($resourceUrl, self::TOKEN) > 0) {
45  $resourceUrl = str_replace(self::TOKEN, $serverHost, $resourceUrl);
46  }
47  return $resourceUrl;
48  }
49 
59  public static function resolveFilePath(string $resourceUrl): string {
60  return urldecode(preg_replace('/^' . preg_quote(self::getServerPublicFilesUrl(), '/') . '/',
61  self::PUBLIC_SCHEME, self::resolve($resourceUrl)));
62  }
63 
64 }
Drupal\common\UrlHostTokenResolver\PUBLIC_SCHEME
const PUBLIC_SCHEME
Definition: UrlHostTokenResolver.php:12
Drupal\common\UrlHostTokenResolver\TOKEN
const TOKEN
Definition: UrlHostTokenResolver.php:11
Drupal\common\UrlHostTokenResolver\resolve
static resolve(string $resourceUrl)
Definition: UrlHostTokenResolver.php:37
Drupal\common\UrlHostTokenResolver
Definition: UrlHostTokenResolver.php:10
Drupal\common
Drupal\common\UrlHostTokenResolver\getServerPublicFilesUrl
static getServerPublicFilesUrl()
Definition: UrlHostTokenResolver.php:20
Drupal\common\UrlHostTokenResolver\resolveFilePath
static resolveFilePath(string $resourceUrl)
Definition: UrlHostTokenResolver.php:59