Download the PHP package ohffs/php-pathlib without Composer
On this page you can find all versions of the php package ohffs/php-pathlib. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download ohffs/php-pathlib
More information about ohffs/php-pathlib
Files in ohffs/php-pathlib
Package php-pathlib
Short Description Very basic port of pythons pathlib to PHP
License MIT
Informations about the package php-pathlib
PHP PathLib
A tiny, Python pathlib‑flavoured Path for PHP that makes path handling and small file ops pleasant and testable.
Installation
TL;DR — why this over raw PHP?
Before (string wrangling + globals):
After (fluent, intention‑revealing):
You get consistent separators, easy joins, and readable code you can test safely.
Heads-up: PHP itself doesn’t expand
~. Either callexpanduser()before filesystem operations, or enable auto-expansion in your app (see Behaviour details).
Quick start
Fake filesystem for tests
Sandbox all file ops under a disposable temp directory. Absolute logical paths (like /etc/hosts) are mapped under the fake base, never touching your real FS.
Pest example
Notes
Path::fake()returns aScopedFakewithbase(): stringand cleans up on__destruct().__toString()always shows the logical path; the temp base is applied only when touching the real filesystem.
Common recipes
Ensure a directory exists and write a file
List items in a folder
Change extension safely (dotfiles supported)
Normalize a messy path without requiring it to exist
Behaviour details
-
~(tilde) expansion: PHP itself doesn’t expand~. Either callexpanduser()before filesystem operations, or enable auto-expansion once in your bootstrap:Path::setAutoExpandTilde(true);. -
Separators: accepts
/and\\in inputs; outputs use your OS separator for real FS calls. Logical string helpers normalise sensibly. -
Dotfiles:
.env/.gitignoreare treated as no extension;suffix()returns'',stem()returns the whole name. -
glob(): non‑recursive; uses PHP’sglob()under the hood; returnsPath[]with logical paths (no temp prefixes). -
resolve(): if the actual path exists, usesrealpath(); otherwise performs a pure string normalisation (./..). - Safety: in fake mode, even logical absolutes are sandboxed under the temp base.
API reference (essentials)
Construction
Path::of(string $path): Path
Query
exists(): boolisFile(): boolisDir(): boolisAbsolute(): boolname(): stringstem(): stringsuffix(): string(no leading dot)parent(): Pathparts(): string[]
Transform
joinpath(string ...$parts): PathwithSuffix(string $suffix): PathwithName(string $name): Pathexpanduser(): Pathresolve(): Path
Filesystem ops
readText(): stringwriteText(string $content): voidmkdir(bool $parents = false, int $mode = 0777): voiditerdir(): Path[]glob(?string $pattern = null): Path[]
Testing helpers
Path::fake(): ScopedFake(returns guard withbase(); auto‑cleanup on destruct)Path::unfake(): void
Requirements
- PHP 8.0+ (typed properties & arrow functions)
- No runtime dependencies
License
MIT — see LICENSE.