function StreamWrapperManager::normalizeUri

Same name in other branches
  1. 8.9.x core/lib/Drupal/Core/StreamWrapper/StreamWrapperManager.php \Drupal\Core\StreamWrapper\StreamWrapperManager::normalizeUri()
  2. 10 core/lib/Drupal/Core/StreamWrapper/StreamWrapperManager.php \Drupal\Core\StreamWrapper\StreamWrapperManager::normalizeUri()
  3. 11.x core/lib/Drupal/Core/StreamWrapper/StreamWrapperManager.php \Drupal\Core\StreamWrapper\StreamWrapperManager::normalizeUri()

Normalizes a URI by making it syntactically correct.

A stream is referenced as "scheme://target".

The following actions are taken:

  • Remove trailing slashes from target
  • Trim erroneous leading slashes from target. e.g. ":///" becomes "://".

Parameters

string $uri: String reference containing the URI to normalize.

Return value

string The normalized URI.

Overrides StreamWrapperManagerInterface::normalizeUri

File

core/lib/Drupal/Core/StreamWrapper/StreamWrapperManager.php, line 239

Class

StreamWrapperManager
Provides a StreamWrapper manager.

Namespace

Drupal\Core\StreamWrapper

Code

public function normalizeUri($uri) {
    $scheme = $this->getScheme($uri);
    if ($this->isValidScheme($scheme)) {
        $target = $this->getTarget($uri);
        if ($target !== FALSE) {
            if (!in_array($scheme, Settings::get('file_sa_core_2023_005_schemes', []))) {
                $class = $this->getClass($scheme);
                $is_local = is_subclass_of($class, LocalStream::class);
                if ($is_local) {
                    $target = str_replace(DIRECTORY_SEPARATOR, '/', $target);
                }
                $parts = explode('/', $target);
                $normalized_parts = [];
                while ($parts) {
                    $part = array_shift($parts);
                    if ($part === '' || $part === '.') {
                        continue;
                    }
                    elseif ($part === '..' && $is_local && $normalized_parts === []) {
                        $normalized_parts[] = $part;
                        break;
                    }
                    elseif ($part === '..') {
                        array_pop($normalized_parts);
                    }
                    else {
                        $normalized_parts[] = $part;
                    }
                }
                $target = implode('/', array_merge($normalized_parts, $parts));
            }
            $uri = $scheme . '://' . $target;
        }
    }
    return $uri;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.