PHP Класс Webmozart\PathUtil\Path

The methods in this class are able to deal with both UNIX and Windows paths with both forward and backward slashes. All methods return normalized parts containing only forward slashes and no excess "." and ".." segments.
С версии: 1.0
Автор: Bernhard Schussek ([email protected])
Автор: Thomas Schulz ([email protected])
Показать файл Открыть проект Примеры использования класса

Открытые методы

Метод Описание
canonicalize ( string $path ) : string Canonicalizes the given path.
changeExtension ( string $path, string $extension ) : string Changes the extension of a path string.
getDirectory ( string $path ) : string Returns the directory part of the path.
getExtension ( string $path, boolean $forceLowerCase = false ) : string Returns the extension from a file path.
getFilename ( string $path ) : string Returns the file name from a file path.
getFilenameWithoutExtension ( string $path, string | null $extension = null ) : string Returns the file name without the extension from a file path.
getHomeDirectory ( ) : string Returns canonical path of the user's home directory.
getLongestCommonBasePath ( array $paths ) : string | null Returns the longest common base path of a set of paths.
getRoot ( string $path ) : string Returns the root directory of a path.
hasExtension ( string $path, string | array | null $extensions = null, boolean $ignoreCase = false ) : boolean Returns whether the path has an extension.
isAbsolute ( string $path ) : boolean Returns whether a path is absolute.
isBasePath ( string $basePath, string $ofPath ) : boolean Returns whether a path is a base path of another path.
isLocal ( string $path ) : boolean Returns whether the given path is on the local filesystem.
isRelative ( string $path ) : boolean Returns whether a path is relative.
join ( string[] | string $paths ) : string Joins two or more path strings.
makeAbsolute ( string $path, string $basePath ) : string Turns a relative path into an absolute path.
makeRelative ( string $path, string $basePath ) : string Turns a path into a relative path.
normalize ( string $path ) : string Normalizes the given path.

Приватные методы

Метод Описание
__construct ( )
split ( string $path ) : string[] Splits a part into its root directory and the remainder.
toLower ( string $str ) : string Converts string to lower-case (multi-byte safe if mbstring is installed).

Описание методов

canonicalize() публичный статический Метод

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.
С версии: 1.0 Added method.
С версии: 2.0 Method now fails if $path is not a string.
С версии: 2.1 Added support for `~`.
public static canonicalize ( string $path ) : string
$path string A path string.
Результат string The canonical path.

changeExtension() публичный статический Метод

Changes the extension of a path string.
С версии: 1.1 Added method.
С версии: 2.0 Method now fails if $path or $extension is not a string.
public static changeExtension ( string $path, string $extension ) : string
$path string The path string with filename.ext to change.
$extension string New extension (with or without leading dot).
Результат string The path string with new file extension.

getDirectory() публичный статический Метод

This method is similar to PHP's dirname(), but handles various cases where dirname() returns a weird result: - dirname() does not accept backslashes on UNIX - dirname("C:/webmozart") returns "C:", not "C:/" - dirname("C:/") returns ".", not "C:/" - dirname("C:") returns ".", not "C:/" - dirname("webmozart") returns ".", not "" - dirname() does not canonicalize the result This method fixes these shortcomings and behaves like dirname() otherwise. The result is a canonical path.
С версии: 1.0 Added method.
С версии: 2.0 Method now fails if $path is not a string.
public static getDirectory ( string $path ) : string
$path string A path string.
Результат string The canonical directory part. Returns the root directory if the root directory is passed. Returns an empty string if a relative path is passed that contains no slashes. Returns an empty string if an empty string is passed.

getExtension() публичный статический Метод

Returns the extension from a file path.
С версии: 1.1 Added method.
С версии: 2.0 Method now fails if $path is not a string.
public static getExtension ( string $path, boolean $forceLowerCase = false ) : string
$path string The path string.
$forceLowerCase boolean Forces the extension to be lower-case (requires mbstring extension for correct multi-byte character handling in extension).
Результат string The extension of the file path (without leading dot).

getFilename() публичный статический Метод

Returns the file name from a file path.
С версии: 1.1 Added method.
С версии: 2.0 Method now fails if $path is not a string.
public static getFilename ( string $path ) : string
$path string The path string.
Результат string The file name.

getFilenameWithoutExtension() публичный статический Метод

Returns the file name without the extension from a file path.
С версии: 1.1 Added method.
С версии: 2.0 Method now fails if $path or $extension have invalid types.
public static getFilenameWithoutExtension ( string $path, string | null $extension = null ) : string
$path string The path string.
$extension string | null If specified, only that extension is cut off (may contain leading dot).
Результат string The file name without extension.

getHomeDirectory() публичный статический Метод

Supported operating systems: - UNIX - Windows8 and upper If your operation system or environment isn't supported, an exception is thrown. The result is a canonical path.
С версии: 2.1 Added method.
public static getHomeDirectory ( ) : string
Результат string The canonical home directory

getLongestCommonBasePath() публичный статический Метод

Dot segments ("." and "..") are removed/collapsed and all slashes turned into forward slashes. php $basePath = Path::getLongestCommonBasePath(array( '/webmozart/css/style.css', '/webmozart/css/..' )); => /webmozart The root is returned if no common base path can be found: php $basePath = Path::getLongestCommonBasePath(array( '/webmozart/css/style.css', '/puli/css/..' )); => / If the paths are located on different Windows partitions, null is returned. php $basePath = Path::getLongestCommonBasePath(array( 'C:/webmozart/css/style.css', 'D:/webmozart/css/..' )); => null
С версии: 1.0 Added method.
С версии: 2.0 Method now fails if $paths are not strings.
public static getLongestCommonBasePath ( array $paths ) : string | null
$paths array A list of paths.
Результат string | null The longest common base path in canonical form or `null` if the paths are on different Windows partitions.

getRoot() публичный статический Метод

The result is a canonical path.
С версии: 1.0 Added method.
С версии: 2.0 Method now fails if $path is not a string.
public static getRoot ( string $path ) : string
$path string A path string.
Результат string The canonical root directory. Returns an empty string if the given path is relative or empty.

hasExtension() публичный статический Метод

Returns whether the path has an extension.
С версии: 1.1 Added method.
С версии: 2.0 Method now fails if $path or $extensions have invalid types.
public static hasExtension ( string $path, string | array | null $extensions = null, boolean $ignoreCase = false ) : boolean
$path string The path string.
$extensions string | array | null If null or not provided, checks if an extension exists, otherwise checks for the specified extension or array of extensions (with or without leading dot).
$ignoreCase boolean Whether to ignore case-sensitivity (requires mbstring extension for correct multi-byte character handling in the extension).
Результат boolean Returns `true` if the path has an (or the specified) extension and `false` otherwise.

isAbsolute() публичный статический Метод

Returns whether a path is absolute.
С версии: 1.0 Added method.
С версии: 2.0 Method now fails if $path is not a string.
public static isAbsolute ( string $path ) : boolean
$path string A path string.
Результат boolean Returns true if the path is absolute, false if it is relative or empty.

isBasePath() публичный статический Метод

Dot segments ("." and "..") are removed/collapsed and all slashes turned into forward slashes. php Path::isBasePath('/webmozart', '/webmozart/css'); => true Path::isBasePath('/webmozart', '/webmozart'); => true Path::isBasePath('/webmozart', '/webmozart/..'); => false Path::isBasePath('/webmozart', '/puli'); => false
С версии: 1.0 Added method.
С версии: 2.0 Method now fails if $basePath or $ofPath is not a string.
public static isBasePath ( string $basePath, string $ofPath ) : boolean
$basePath string The base path to test.
$ofPath string The other path.
Результат boolean Whether the base path is a base path of the other path.

isLocal() публичный статический Метод

Returns whether the given path is on the local filesystem.
С версии: 1.0 Added method.
С версии: 2.0 Method now fails if $path is not a string.
public static isLocal ( string $path ) : boolean
$path string A path string.
Результат boolean Returns true if the path is local, false for a URL.

isRelative() публичный статический Метод

Returns whether a path is relative.
С версии: 1.0 Added method.
С версии: 2.0 Method now fails if $path is not a string.
public static isRelative ( string $path ) : boolean
$path string A path string.
Результат boolean Returns true if the path is relative or empty, false if it is absolute.

join() публичный статический Метод

The result is a canonical path.
С версии: 2.0 Added method.
public static join ( string[] | string $paths ) : string
$paths string[] | string Path parts as parameters or array.
Результат string The joint path.

makeAbsolute() публичный статический Метод

Usually, the relative path is appended to the given base path. Dot segments ("." and "..") are removed/collapsed and all slashes turned into forward slashes. php echo Path::makeAbsolute("../style.css", "/webmozart/puli/css"); => /webmozart/puli/style.css If an absolute path is passed, that path is returned unless its root directory is different than the one of the base path. In that case, an exception is thrown. php Path::makeAbsolute("/style.css", "/webmozart/puli/css"); => /style.css Path::makeAbsolute("C:/style.css", "C:/webmozart/puli/css"); => C:/style.css Path::makeAbsolute("C:/style.css", "/webmozart/puli/css"); InvalidArgumentException If the base path is not an absolute path, an exception is thrown. The result is a canonical path.
С версии: 1.0 Added method.
С версии: 2.0 Method now fails if $path or $basePath is not a string.
С версии: 2.2.2 Method does not fail anymore of $path and $basePath are absolute, but on different partitions.
public static makeAbsolute ( string $path, string $basePath ) : string
$path string A path to make absolute.
$basePath string An absolute base path.
Результат string An absolute path in canonical form.

makeRelative() публичный статический Метод

The relative path is created relative to the given base path: php echo Path::makeRelative("/webmozart/style.css", "/webmozart/puli"); => ../style.css If a relative path is passed and the base path is absolute, the relative path is returned unchanged: php Path::makeRelative("style.css", "/webmozart/puli/css"); => style.css If both paths are relative, the relative path is created with the assumption that both paths are relative to the same directory: php Path::makeRelative("style.css", "webmozart/puli/css"); => ../../../style.css If both paths are absolute, their root directory must be the same, otherwise an exception is thrown: php Path::makeRelative("C:/webmozart/style.css", "/webmozart/puli"); InvalidArgumentException If the passed path is absolute, but the base path is not, an exception is thrown as well: php Path::makeRelative("/webmozart/style.css", "webmozart/puli"); InvalidArgumentException If the base path is not an absolute path, an exception is thrown. The result is a canonical path.
С версии: 1.0 Added method.
С версии: 2.0 Method now fails if $path or $basePath is not a string.
public static makeRelative ( string $path, string $basePath ) : string
$path string A path to make relative.
$basePath string A base path.
Результат string A relative path in canonical form.

normalize() публичный статический Метод

During normalization, all slashes are replaced by forward slashes ("/"). Contrary to {@link canonicalize()}, this method does not remove invalid or dot path segments. Consequently, it is much more efficient and should be used whenever the given path is known to be a valid, absolute system path. This method is able to deal with both UNIX and Windows paths.
С версии: 2.2 Added method.
public static normalize ( string $path ) : string
$path string A path string.
Результат string The normalized path.