Webmozart\PathUtil\Path::canonicalize PHP Method

canonicalize() public static method

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("\webmozart\puli\..\css\style.css"); => /webmozart/css/style.css echo Path::canonicalize("../css/./style.css"); => ../css/style.css This method is able to deal with both UNIX and Windows paths.
Since: 1.0 Added method.
Since: 2.0 Method now fails if $path is not a string.
Since: 2.1 Added support for `~`.
public static canonicalize ( string $path ) : string
$path string A path string.
return string The canonical path.
    public static function canonicalize($path)
    {
        if ('' === $path) {
            return '';
        }
        Assert::string($path, 'The path must be a string. Got: %s');
        // 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 = static::getHomeDirectory() . substr($path, 1);
        }
        $path = str_replace('\\', '/', $path);
        list($root, $pathWithoutRoot) = self::split($path);
        $parts = explode('/', $pathWithoutRoot);
        $canonicalParts = array();
        // Collapse "." and "..", if possible
        foreach ($parts as $part) {
            if ('.' === $part || '' === $part) {
                continue;
            }
            // Collapse ".." with the previous part, if one exists
            // Don't collapse ".." if the previous part is also ".."
            if ('..' === $part && count($canonicalParts) > 0 && '..' !== $canonicalParts[count($canonicalParts) - 1]) {
                array_pop($canonicalParts);
                continue;
            }
            // Only add ".." prefixes for relative paths
            if ('..' !== $part || '' === $root) {
                $canonicalParts[] = $part;
            }
        }
        // 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;
    }

Usage Example

Ejemplo n.º 1
0
 public function __construct($baseDir = null, array $mappings = array(), UriRetrieverInterface $fallbackRetriever = null)
 {
     $this->baseDir = $baseDir ? Path::canonicalize($baseDir) : null;
     $this->mappings = $mappings;
     $this->filesystemRetriever = new FileGetContents();
     $this->fallbackRetriever = $fallbackRetriever ?: $this->filesystemRetriever;
 }
All Usage Examples Of Webmozart\PathUtil\Path::canonicalize