mini\Util\Path final class

Documentation

Cross-platform path manipulation utility

Provides lexical path operations that work consistently across Unix-like systems and Windows. All paths are stored internally using forward slashes and converted to platform-native separators only when converted to strings.

Features

  • Cross-platform: Handles POSIX paths, Windows drive letters (C:/), and UNC paths (//server/share)
  • Lexical operations: Path manipulation without filesystem access (join, canonical)
  • Filesystem resolution: Optional realpath() for resolving symlinks and validating existence
  • Immutable: All operations return new Path instances
  • Type-safe: Implements PathInterface for consistent behavior

Usage Examples

// Create and manipulate paths
$path = new Path('/var/www/html');
$file = $path->join('index.php'); // /var/www/html/index.php

// Resolve relative paths
$config = Path::create('/var/www', '../config', 'app.php'); // /var/config/app.php

// Get parent directory
$parent = $path->parent(); // /var/www

// Check if path exists on filesystem
$real = $path->realpath(); // null if doesn't exist

// Platform-native string representation
echo $path; // "/var/www/html" on Unix, "\\var\\www\\html" on Windows

Path Semantics

Absolute paths start with:

  • POSIX root: / (e.g., /var/www)
  • Windows drive: C:/ (e.g., C:/Users)
  • UNC share: // (e.g., //server/share)

Relative paths are everything else:

  • Simple: foo/bar
  • Current directory: . or ./foo
  • Parent directory: .. or ../foo

Canonicalization resolves . and .. segments:

  • /foo/./bar/foo/bar
  • /foo/baz/../bar/foo/bar
  • foo/../../bar../bar (preserves unmatched .. in relative paths)

Inheritance

Implements: mini\Contracts\PathInterface Stringable

Properties (1)

private string $path

Internal canonical representation:

Methods (16)

Create a new Path instance

Build a path from a base and zero or more additional parts (lexical only)

Build a path and resolve it against the filesystem (realpath())

Convert path to string using platform-native separators

Join a target path to this path (lexical operation)

Get the parent directory of this path (lexical operation)

Get canonical (normalized) form of this path (lexical operation)

Resolve path against filesystem (resolves symlinks, validates existence)

Check if this path is absolute

Check if this path is relative

Normalize a raw path string to our internal representation:

Lexically canonicalize a normalized path:

Join two normalized path strings (base + relative target) lexically.

Decide if a normalized raw path is absolute.

Split a normalized path into [prefix, rest].

Build a normalized path string from prefix + segments.

Source

src/Util/Path.php:64-745