lastdragon-ru/path
Laravel/PHP utilities for working with filesystem-like paths: build, normalize, join, and resolve path segments with consistent behavior across platforms. Lightweight helper functions/classes aimed at safer path manipulation in applications and libraries.
Installation
composer require lastdragon-ru/path
Add the service provider to config/app.php (if not auto-discovered):
'providers' => [
// ...
LastDragon\Path\PathServiceProvider::class,
],
First Usage
use LastDragon\Path\Path;
$path = Path::make('/var/www/project');
echo $path->getPath(); // "/var/www/project"
Key Initial Methods
Path::make($path) – Create a new path instance.$path->getPath() – Retrieve the raw path string.$path->exists() – Check if the path exists.Path Manipulation
$path = Path::make('/var/www/project')->append('src')->append('Controller');
echo $path->getPath(); // "/var/www/project/src/Controller"
File/Directory Operations
$path = Path::make('/var/www/storage');
$path->createDirectory(); // mkdir()
$path->delete(); // rmdir() (empty dirs only)
Relative Paths
$base = Path::make('/var/www');
$relative = $base->relativeTo('/var/www/project');
echo $relative->getPath(); // "project"
File Operations
$file = Path::make('/var/www/app.log');
$file->exists() ? 'Exists' : 'Missing';
$file->delete(); // unlink()
Storage Paths
$storage = Path::make(storage_path());
$file = $storage->append('app.log');
Config Files
$config = Path::make(config_path('app.php'));
$config->exists() ? 'Config exists' : 'Missing';
Artisan Commands
use LastDragon\Path\Path;
public function handle()
{
$cacheDir = Path::make(cache_path())->append('temp');
$cacheDir->createDirectory();
}
Cross-Platform Paths
/ as separator by default. For Windows compatibility, ensure paths are normalized:
$path = Path::make('C:\Users\file.txt')->normalize();
Directory Deletion
delete() only removes empty directories. Use deleteDirectory() (if available) for recursive deletion.Case Sensitivity
Path::make('/var/www/Project')->exists(); // May fail if actual path is '/var/www/project'
Trailing Slashes
append() ignores trailing slashes. Use ensureTrailingSlash() if needed:
$path->ensureTrailingSlash()->append('file.txt');
Inspect Paths
$path->getPath(); // Raw string
$path->getRealPath(); // Resolved symlinks (if supported)
Check Permissions
$path->isWritable() ? 'Writable' : 'Read-only';
Custom Path Classes
Extend LastDragon\Path\Path to add domain-specific methods:
class ProjectPath extends Path
{
public function getComposerJson()
{
return $this->append('composer.json')->getPath();
}
}
Override Default Behavior Bind a custom path resolver in the service provider:
$this->app->bind('path', function () {
return new CustomPathResolver();
});
Path Normalization
Use normalize() to handle edge cases (e.g., ./, ../, or duplicate slashes).
How can I help you explore Laravel packages today?