Download the PHP package mb4it/filesystem without Composer
On this page you can find all versions of the php package mb4it/filesystem. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download mb4it/filesystem
More information about mb4it/filesystem
Files in mb4it/filesystem
Package filesystem
Short Description Lightweight filesystem abstraction for PHP with predictable, exception-based API.
License MIT
Informations about the package filesystem
MB Filesystem
Lightweight wrapper around the local PHP filesystem with a convenient, predictable, exception‑based API.
Requirements
- PHP 8.1+
Installation
Basic usage
Working with JSON
Content and updateContent (plain files)
For non-JSON files you can use content() with an optional default and updateContent() for read-modify-write. updateContent() accepts either a path or a File object. For batch updates by path, prefer calling updateContent($path, $updater) with a string path so you avoid creating a File node per path (faster). The third parameter $atomic (default true) uses a temp file + rename so the file is never partially written on failure; pass false for higher throughput when crash-safety is not critical (e.g. logs, cache).
get() — get a node by path
get(string $path) returns a node representing the path — it does not return file content. The return type is File|Directory|Link:
- File — when the path is a regular file (or a symlink is not followed; use
file()for a file,link()for a symlink). - Directory — when the path is a directory.
- Link — when the path is a symbolic link (symlink).
Use content($path) or $file->content() to read file content as a string. Example:
File, Directory, and Link (nodes)
file(), directory(), and link() return rich nodes with metadata and operations. Use them to read, update, or delete without passing the path again. You can also obtain a node with get($path) (see above).
Node types and main methods
| Node | Main methods / metadata |
|---|---|
| File | path, size, lastModified, extension, basename, filename, dirname, readable, writable, mode — content(), lines(), line($n), update(), write(), delete(), move(), copy(), chmod(), touch() |
| Directory | path, lastModified, readable, writable, mode — files(), directories(), create(), delete($recursive), chmod(), touch() |
| Link | path, target, isBroken, targetType — target(), isBroken(), resolve(): File|Directory|null, delete() |
File
For large files use lines() instead of content() to avoid loading the whole file; use line($lineNumber) to read a single line by 1-based index.
Directory
Link (symlinks)
Create a symlink with createSymlink($target, $linkPath). Target can be relative (to the link’s directory) or absolute; link path must not exist. Use isLink($path) to check, link($path) to get a Link node. resolve() returns the target as File or Directory, or null if the link is broken.
Move, rename, and real path
move($from, $to) and rename($from, $to) do the same thing: move or rename a file, directory, or symlink. Both use PHP’s rename(); for directories, source and destination must be on the same filesystem.
realPath($path) returns the canonical absolute path (symlinks and ../. resolved). Throws if the path does not exist.
Directories and recursive operations
Masks and filtering
Metadata and write modes
Path helpers and base directory
Finding PHP classes by extends/implements/traits
The package provides a ClassFinder utility that can find classes by base parent, implemented interface, or used trait using static token-based parsing (no autoload, no ReflectionClass).
Search: grep and find
Four methods provide content search (grep-like) and path search (find-like). Path-only variants return string[] (lighter); node variants return File[] or File|Directory|Link[] for use with metadata and methods.
When to use which: Use grepPaths / findPaths when you only need paths or have very large result sets; use grep / find when you need node metadata or methods (content(), size, etc.). All are implemented in pure PHP (no shell).
Content search (grepPaths / grep)
Path search (findPaths / find)
Error handling
The package uses three custom exceptions:
MB\Filesystem\Exceptions\FileNotFoundException— resource not found.MB\Filesystem\Exceptions\IOException— general I/O error (read/write failure, invalid JSON, etc.).MB\Filesystem\Exceptions\PermissionException— insufficient permissions to perform the operation.
It is recommended to handle them explicitly in your code:
Development
Running tests
Install dev dependencies and run the full test suite:
Or run PHPUnit directly (with optional config):
On Windows use vendor\bin\phpunit. Some tests (e.g. symlink creation) may be skipped if the environment does not support them (e.g. creating symlinks without Administrator or Developer Mode on Windows).
Running the benchmark
The benchmark measures performance of common operations (put/get, recursive listing, content search, file updates, etc.) and prints timing and memory statistics.
Or directly:
You can tune the run via environment variables; see benchmark/README.md for options (e.g. BENCH_FILES, BENCH_TREE_DEPTH, BENCH_LARGE_MB).