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

Breadcrumb

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

function Path::canonicalize

Canonicalizes the given path.

During normalization, all slashes are replaced by forward slashes ("/"). Furthermore, all "." and ".." segments are removed as far as possible. ".." segments at the beginning of relative paths are not removed.

```php echo Path::canonicalize("\symfony\puli\..\css\style.css"); // => /symfony/css/style.css

echo Path::canonicalize("../css/./style.css"); // => ../css/style.css ```

This method is able to deal with both UNIX and Windows paths.

8 calls to Path::canonicalize()
Path::getDirectory in vendor/symfony/filesystem/Path.php
Returns the directory part of the path.
Path::getHomeDirectory in vendor/symfony/filesystem/Path.php
Returns canonical path of the user's home directory.
Path::getLongestCommonBasePath in vendor/symfony/filesystem/Path.php
Returns the longest common base path in canonical form of a set of paths or `null` if the paths are on different Windows partitions.
Path::isBasePath in vendor/symfony/filesystem/Path.php
Returns whether a path is a base path of another path.
Path::join in vendor/symfony/filesystem/Path.php
Joins two or more path strings into a canonical path.

... See full list

File

vendor/symfony/filesystem/Path.php, line 66

Class

Path
Contains utility methods for handling path strings.

Namespace

Symfony\Component\Filesystem

Code

public static function canonicalize(string $path) : string {
    if ('' === $path) {
        return '';
    }
    // This method is called by many other methods in this class. Buffer
    // the canonicalized paths to make up for the severe performance
    // decrease.
    if (isset(self::$buffer[$path])) {
        return self::$buffer[$path];
    }
    // Replace "~" with user's home directory.
    if ('~' === $path[0]) {
        $path = self::getHomeDirectory() . substr($path, 1);
    }
    $path = self::normalize($path);
    [
        $root,
        $pathWithoutRoot,
    ] = self::split($path);
    $canonicalParts = self::findCanonicalParts($root, $pathWithoutRoot);
    // Add the root directory again
    self::$buffer[$path] = $canonicalPath = $root . implode('/', $canonicalParts);
    ++self::$bufferSize;
    // Clean up regularly to prevent memory leaks
    if (self::$bufferSize > self::CLEANUP_THRESHOLD) {
        self::$buffer = \array_slice(self::$buffer, -self::CLEANUP_SIZE, null, true);
        self::$bufferSize = self::CLEANUP_SIZE;
    }
    return $canonicalPath;
}

API Navigation

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