PHP code example of ohffs / php-pathlib
1. Go to this page and download the library: Download ohffs/php-pathlib library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
ohffs / php-pathlib example snippets
$base = rtrim(getenv('HOME'), DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . 'myapp';
$cfg = $base . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR . 'settings.json';
@mkdir(dirname($cfg), 0777, true);
file_put_contents($cfg, json_encode(['debug' => true]));
use Ohffs\PhpPathlib\Path;
Path::setAutoExpandTilde(); // optional: call once at bootstrap to expand "~"
$cfg = Path::of('~/myapp')
->joinpath('config', 'settings.json');
$cfg->writeText(json_encode(['debug' => true]));
use Ohffs\PhpPathlib\Path;
Path::setAutoExpandTilde(); // optional: expand "~" automatically
// Construct & chain
$project = Path::of('~/projects/example');
$config = $project->joinpath('config', 'app.json');
$readme = $project->joinpath('README.md');
// Queries
$config->exists(); // bool
$config->isFile(); // bool
$project->isDir(); // bool
// Name parts
$readme->name(); // "README.md"
$readme->stem(); // "README"
$readme->suffix(); // "md" (no leading dot)
$project->parent(); // Path("~/projects")
$project->parts(); // absolute parts after expanduser()
// IO
$config->writeText('{"debug":true}');
$text = $config->readText();
// Transformations
$logDir = Path::of('/var/log')->resolve();
$logFile = $logDir->joinpath('app.log').""; // castable to string
$tmpMd = Path::of('report.txt')->withSuffix('md'); // report.md
// Globbing (non‑recursive)
foreach (Path::of('src')->glob('*.php') as $php) {
echo $php->name(), "\n";
}
use Ohffs\PhpPathlib\Path;
beforeEach(function () {
$this->fs = Path::fake(); // returns a guard; keep the ref alive
});
afterEach(function () {
unset($this->fs); // or Path::unfake()
});
it('writes without touching the real FS', function () {
$p = Path::of('/app/data.txt');
$p->writeText('ok');
expect($p->exists())->toBeTrue(); // logical
expect(is_file($this->fs->base().'/app/data.txt'))->toBeTrue(); // actual temp
});
$dir = Path::of('~/cache/images');
$dir->mkdir(true); // parents=true
$dir->joinpath('index.json')->writeText('[]');
$names = array_map(fn($p) => $p->name(), Path::of('logs')->iterdir());
Path::of('/a/archive.tar.gz')->withSuffix('zip'); // /a/archive.tar.zip
Path::of('/a/.env')->withSuffix('bak'); // /a/.env.bak
Path::of('/x/y/../z/./a')->resolve(); // /x/z/a
bash
composer