Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. Filesystem.php

function Filesystem::normalizePath

Normalize a path. This replaces backslashes with slashes, removes ending slash and collapses redundant separators and up-level references.

Parameters

string $path Path to the file or directory:

Return value

string

4 calls to Filesystem::normalizePath()
Filesystem::copy in vendor/composer/composer/src/Composer/Util/Filesystem.php
Copies a file or directory from $source to $target.
Filesystem::ensureDirectoryExists in vendor/composer/composer/src/Composer/Util/Filesystem.php
Filesystem::findShortestPath in vendor/composer/composer/src/Composer/Util/Filesystem.php
Returns the shortest path from $from to $to
Filesystem::findShortestPathCode in vendor/composer/composer/src/Composer/Util/Filesystem.php
Returns PHP code that, when executed in $from, will return the path to $to

File

vendor/composer/composer/src/Composer/Util/Filesystem.php, line 597

Class

Filesystem
@author Jordi Boggiano <j.boggiano@seld.be> @author Johannes M. Schmitt <schmittjoh@gmail.com>

Namespace

Composer\Util

Code

public function normalizePath(string $path) {
    $parts = [];
    $path = strtr($path, '\\', '/');
    $prefix = '';
    $absolute = '';
    // extract windows UNC paths e.g. \\foo\bar
    if (strpos($path, '//') === 0 && \strlen($path) > 2) {
        $absolute = '//';
        $path = substr($path, 2);
    }
    // extract a prefix being a protocol://, protocol:, protocol://drive: or simply drive:
    if (Preg::isMatchStrictGroups('{^( [0-9a-z]{2,}+: (?: // (?: [a-z]: )? )? | [a-z]: )}ix', $path, $match)) {
        $prefix = $match[1];
        $path = substr($path, \strlen($prefix));
    }
    if (strpos($path, '/') === 0) {
        $absolute = '/';
        $path = substr($path, 1);
    }
    $up = false;
    foreach (explode('/', $path) as $chunk) {
        if ('..' === $chunk && (\strlen($absolute) > 0 || $up)) {
            array_pop($parts);
            $up = !(\count($parts) === 0 || '..' === end($parts));
        }
        elseif ('.' !== $chunk && '' !== $chunk) {
            $parts[] = $chunk;
            $up = '..' !== $chunk;
        }
    }
    // ensure c: is normalized to C:
    $prefix = Preg::replaceCallback('{(^|://)[a-z]:$}i', static function (array $m) {
        return strtoupper($m[0]);
    }, $prefix);
    return $prefix . $absolute . implode('/', $parts);
}

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal