Installation
Add to composer.json:
"require": {
"dontdrinkandroot/path": "^1.0"
}
Run composer update.
First Use Case Basic path creation and manipulation:
use DontDrinkAndRoot\Path\Path;
$path = Path::create('/var/www/html');
echo $path->getAbsolutePath(); // Resolves to absolute path
Where to Look First
DontDrinkAndRoot\Path\Path (immutable path handling).Path::create(), getAbsolutePath(), and join() methods.tests/ directory for edge cases (e.g., Windows paths, symlinks).Path Creation & Resolution
// Immutable path creation
$path = Path::create('app/Config');
$absolutePath = $path->getAbsolutePath(); // Resolves relative to current dir
// Join paths safely
$nestedPath = $path->join('services', 'cache.php');
File/Directory Operations
// Check existence
if ($path->exists()) {
$path->delete(); // Delete directory (recursively)
}
// Create directories
$path->mkdir(); // Creates parent dirs if needed
Path Normalization
$normalized = Path::create('./app/../Config')->normalize();
// Resolves to 'Config' (removes redundant segments)
Laravel Integration Use with Laravel’s filesystem:
$path = Path::create(storage_path('logs'));
Storage::put($path->getAbsolutePath(), 'log content');
Path objects as read-only. Use ->with() for modifications:
$newPath = $path->withBasename('newname.txt');
C:\Users\file.txt).isFile()/isDirectory() before operations.Windows Path Handling
/) or DIRECTORY_SEPARATOR for consistency:
$path = Path::create('C:/Users/file.txt'); // Works, but avoid mixed slashes.
$path->normalize()->getAbsolutePath();
Symlink Resolution
getAbsolutePath() follows symlinks by default. Use getRealPath() to resolve symlinks:
$realPath = $path->getRealPath(); // Resolves symlinks to target.
Permission Issues
mkdir()/delete() may fail silently. Check return values:
if (!$path->mkdir()) {
throw new \RuntimeException("Failed to create directory.");
}
Edge Cases
Path::create('/') behaves differently than Path::create('').Path::create('dir/') vs. Path::create('dir') may affect isDirectory().getAbsolutePath() for debugging to avoid relative path confusion.// Test with:
Path::create('//absolute/path'); // Double slash
Path::create('app/../Config'); // Parent dir
Path::create('file:///dev/null'); // URL-like paths
Custom Path Resolvers
Override getAbsolutePath() logic for custom environments:
class CustomPath extends Path {
public function getAbsolutePath() {
return '/custom/base/' . parent::getAbsolutePath();
}
}
Event Hooks
Extend with events (e.g., PathCreated, PathDeleted) using Laravel’s Events facade.
Integration with Laravel
Path to Laravel’s container:
$this->app->bind('path', function () {
return new Path();
});
Path facade for cleaner syntax:
use Illuminate\Support\Facades\Facade;
class PathFacade extends Facade {
protected static function getFacadeAccessor() { return 'path'; }
}
How can I help you explore Laravel packages today?