PHP Class 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.
Since: 1.0
Author: Bernhard Schussek ([email protected])
Author: Thomas Schulz ([email protected])
Show file Open project: webmozart/path-util Class Usage Examples

Public Methods

Method Description
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.

Private Methods

Method Description
__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).

Method Details

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.

changeExtension() public static method

Changes the extension of a path string.
Since: 1.1 Added method.
Since: 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).
return string The path string with new file extension.

getDirectory() public static method

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.
Since: 1.0 Added method.
Since: 2.0 Method now fails if $path is not a string.
public static getDirectory ( string $path ) : string
$path string A path string.
return 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() public static method

Returns the extension from a file path.
Since: 1.1 Added method.
Since: 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).
return string The extension of the file path (without leading dot).

getFilename() public static method

Returns the file name from a file path.
Since: 1.1 Added method.
Since: 2.0 Method now fails if $path is not a string.
public static getFilename ( string $path ) : string
$path string The path string.
return string The file name.

getFilenameWithoutExtension() public static method

Returns the file name without the extension from a file path.
Since: 1.1 Added method.
Since: 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).
return string The file name without extension.

getHomeDirectory() public static method

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.
Since: 2.1 Added method.
public static getHomeDirectory ( ) : string
return string The canonical home directory

getLongestCommonBasePath() public static method

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
Since: 1.0 Added method.
Since: 2.0 Method now fails if $paths are not strings.
public static getLongestCommonBasePath ( array $paths ) : string | null
$paths array A list of paths.
return string | null The longest common base path in canonical form or `null` if the paths are on different Windows partitions.

getRoot() public static method

The result is a canonical path.
Since: 1.0 Added method.
Since: 2.0 Method now fails if $path is not a string.
public static getRoot ( string $path ) : string
$path string A path string.
return string The canonical root directory. Returns an empty string if the given path is relative or empty.

hasExtension() public static method

Returns whether the path has an extension.
Since: 1.1 Added method.
Since: 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).
return boolean Returns `true` if the path has an (or the specified) extension and `false` otherwise.

isAbsolute() public static method

Returns whether a path is absolute.
Since: 1.0 Added method.
Since: 2.0 Method now fails if $path is not a string.
public static isAbsolute ( string $path ) : boolean
$path string A path string.
return boolean Returns true if the path is absolute, false if it is relative or empty.

isBasePath() public static method

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
Since: 1.0 Added method.
Since: 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.
return boolean Whether the base path is a base path of the other path.

isLocal() public static method

Returns whether the given path is on the local filesystem.
Since: 1.0 Added method.
Since: 2.0 Method now fails if $path is not a string.
public static isLocal ( string $path ) : boolean
$path string A path string.
return boolean Returns true if the path is local, false for a URL.

isRelative() public static method

Returns whether a path is relative.
Since: 1.0 Added method.
Since: 2.0 Method now fails if $path is not a string.
public static isRelative ( string $path ) : boolean
$path string A path string.
return boolean Returns true if the path is relative or empty, false if it is absolute.

join() public static method

The result is a canonical path.
Since: 2.0 Added method.
public static join ( string[] | string $paths ) : string
$paths string[] | string Path parts as parameters or array.
return string The joint path.

makeAbsolute() public static method

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.
Since: 1.0 Added method.
Since: 2.0 Method now fails if $path or $basePath is not a string.
Since: 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.
return string An absolute path in canonical form.

makeRelative() public static method

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.
Since: 1.0 Added method.
Since: 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.
return string A relative path in canonical form.

normalize() public static method

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.
Since: 2.2 Added method.
public static normalize ( string $path ) : string
$path string A path string.
return string The normalized path.