You are here

protected static function UrlBag::toLocal in Mini site 8

Convert URL to local URL.


http://example.com => /
http://example.com/path => /path
http://example.com/path/subpath => /path/subpath
/path => /path
/path/subpath => /path/subpath
public://path => public://path
public://path/subpath => public://path/subpath
path=>/path
path/subpath=>/path/subpath

Parameters

string $url: URL to convert.

string $base_url: Bae URL to use for conversion.

Return value

string Converted URl as relative url.

Throws

\Drupal\minisite\Exception\UrlBagException If provided url is not within domain or does not contain a path.

1 call to UrlBag::toLocal()
UrlBag::setParentAlias in src/UrlBag.php
Set parent alias.

File

src/UrlBag.php, line 226

Class

UrlBag
Class UrlBag.

Namespace

Drupal\minisite

Code

protected static function toLocal($url, $base_url) {

  // Provided URL cannot be from another external domain.
  if (UrlHelper::isExternal($url) && !UrlHelper::externalIsLocal($url, $base_url)) {
    throw new UrlBagException('Provided external path points to another domain');
  }

  // Parsing URL with Core's parser to handle relative paths.
  $parsed = UrlHelper::parse($url);

  // Re-parsing the 'path' component again to extract actual path.
  $parsed = isset($parsed['path']) ? parse_url($parsed['path']) : $parsed;

  // Check that this is a file starting with a register wrapper scheme and
  // return the URL as-is.
  if (!empty($parsed['scheme'])) {
    if (in_array($parsed['scheme'], static::getLocalWrapperSchemas())) {
      return $url;
    }
  }
  if (empty($parsed['path'])) {
    throw new UrlBagException('Provided URL does not contain path');
  }
  $parsed_base = parse_url($base_url);
  $url = isset($parsed_base['path']) ? str_replace($parsed_base['path'], '', $parsed['path']) : $parsed['path'];
  return '/' . ltrim($url, '/');
}