Path
in package
Path class for handling file system paths This class provides utility methods for working with file paths and ensures compatibility across different operating systems.
Table of Contents
Constants
- CLEANUP_SIZE : mixed = 800
- Maximum allowed path length in characters Used to prevent performance issues or system limitations
- CLEANUP_THRESHOLD : int = 1000
- Threshold for path cleanup operations
Properties
- $buffer : array<string|int, mixed>
- Buffer for caching path operations
- $bufferSize : int
- Current size of the buffer
Methods
- canonicalize() : string
- Canonicalizes a path by removing '.' and '..' components, resolving symbolic links, and handling the root directory properly. It also caches results to improve performance.
- normalize() : string
- normalize function
- findCanonicalParts() : array<string|int, mixed>
- This function finds the canonical parts of a given path relative to a given root.
- getHomeDirectory() : string
- This function returns the canonicalized home directory path.
- split() : array<string|int, mixed>
- This function splits a given path into its root and relative parts.
Constants
CLEANUP_SIZE
Maximum allowed path length in characters Used to prevent performance issues or system limitations
private
mixed
CLEANUP_SIZE
= 800
CLEANUP_THRESHOLD
Threshold for path cleanup operations
private
int
CLEANUP_THRESHOLD
= 1000
Properties
$buffer
Buffer for caching path operations
private
static array<string|int, mixed>
$buffer
= []
$bufferSize
Current size of the buffer
private
static int
$bufferSize
= 0
Methods
canonicalize()
Canonicalizes a path by removing '.' and '..' components, resolving symbolic links, and handling the root directory properly. It also caches results to improve performance.
public
static canonicalize(string $path) : string
Parameters
- $path : string
-
The path to canonicalize
Return values
string —The canonicalized path
normalize()
normalize function
public
static normalize(string $path) : string
Parameters
- $path : string
Return values
stringfindCanonicalParts()
This function finds the canonical parts of a given path relative to a given root.
private
static findCanonicalParts(string $root, string $path) : array<string|int, mixed>
It takes a root path and a path and removes any '..' segments from the path, effectively limiting the path to the part that is within the root. It does this by iterating over the parts of the path and adding them to an array, except for the '.' and '//' segments which are ignored. If a '..' segment is found, the last part of the path is removed.
Parameters
- $root : string
-
The root path.
- $path : string
-
The path to be canonicalized.
Return values
array<string|int, mixed> —An array containing the canonical parts of the path.
getHomeDirectory()
This function returns the canonicalized home directory path.
private
static getHomeDirectory() : string
It first checks if the 'HOME' environment variable is set, and if so, returns the normalized home directory path. If not, it checks if the 'HOMEDRIVE' and 'HOMEPATH' environment variables are set, and if so, returns the normalized home directory path. If neither 'HOME' nor 'HOMEDRIVE' and 'HOMEPATH' are set, it throws a RuntimeException with a message indicating that the home directory cannot be determined.
Tags
Return values
string —The canonicalized home directory path.
split()
This function splits a given path into its root and relative parts.
private
static split(string $path) : array<string|int, mixed>
If the path starts with a forward slash ('/'), it is considered an absolute path and the root is set to '/'. The root is then removed from the path. If the path does not start with a forward slash, it is considered a relative path and the root is set to an empty string.
Parameters
- $path : string
-
The path to be split.
Return values
array<string|int, mixed> —An array containing the root and the relative part of the path.