jackiedo/path-helper
PHP helper for working with local filesystem paths. Normalize directory separators, restyle paths for Unix/Windows/current OS, build absolute/relative paths, check if paths are absolute/relative, and test ancestor/descendant relationships. Composer install.
Storage, Filesystem) or manual file operations (e.g., file_get_contents(), Storage::put())./var/www/uploads/ vs. C:\uploads\). Mitigates inconsistencies in DIRECTORY_SEPARATOR or manual path concatenation.FileService).Str facade or create a custom PathHelper trait for reusable logic.Str::of($path)->replace() or pathinfo(), but fills gaps for path relationships (e.g., isAncestor(), isDescendant()), which are absent in core PHP/Laravel.composer require jackiedo/path-helper. No Laravel service provider or config needed.Path::unixStyle($path) in a controller).PathHelper class injected into a FileUploader service).absolute_path()) risk conflicts with other packages. Recommendation: Use class methods exclusively (Path::absolute()) to avoid namespace pollution.| Risk Area | Assessment | Mitigation Strategy |
|---|---|---|
| Maintenance | Last release: 2022-03-07 (3+ years stale). No GitHub activity. | - Fork and maintain if critical. |
| - Monitor for PHP 8.x compatibility (e.g., typed properties, nullsafe operators). | ||
| Functionality Gaps | Lacks URL/path joining (e.g., dirname() + basename() combinations). |
- Supplement with Laravel’s Str or pathinfo() where needed. |
| Performance | Minimal overhead for basic path ops, but no benchmarks vs. native PHP. | - Profile in critical paths (e.g., bulk file processing). |
| Testing | No visible test suite or coverage. | - Add unit tests for edge cases (e.g., ../../../ traversal, symlinks). |
| Security | Path normalization could expose directory traversal if misused. | - Validate inputs (e.g., Str::of($path)->startsWith(public_path())). |
| Dependency Bloat | Adds ~1KB to vendor size. | - Negligible for most apps; justify with reduced custom logic. |
str_replace('\\', '/', $path))?spatie/path (more active).\\server\share\file)?/api/uploads/file) or only filesystem paths?Path::normalize() → PathHelper::normalize()).Storage::disk()->put() for normalized paths.FormRequest rules (e.g., Path::isAbsolute($request->file_path)).dd(Path::unixStyle($path))).realpath(): Only works for existing paths; this package handles non-existent paths.pathinfo(): Limited to parsing, not normalization/relationships.DIRECTORY_SEPARATOR: Manual and error-prone for cross-platform apps.Flysystem\Adapter.| Current State | Migration Step | Example |
|---|---|---|
| Manual Path Handling | Replace str_replace()/explode() with Path::normalize(). |
Before: $path = str_replace('\\', '/', $rawPath); |
After: $path = Path::normalize($rawPath); |
||
| Hardcoded Separators | Use Path::osStyle() or Path::unixStyle() for consistency. |
Before: $path = '/var' . DIRECTORY_SEPARATOR . 'log'; |
After: $path = Path::join('/var', 'log'); (if method exists) |
||
| Platform-Specific Logic | Centralize path ops in a service class using this package. | Create app/Services/PathService.php with PathHelper instance. |
| Testing Path Logic | Add tests for path normalization/validation. | assertTrue(Path::isAbsolute('/var/log')); |
Path::absolute() may not resolve symlinks (unlike realpath()).isAbsolute() may behave unexpectedly with /path/ vs. /path.\\server\share\file (common in Windows).PathHelper service class wrapping static methods.class PathService {
public function normalize(string $path): string {
return Path::normalize($path);
}
}
deprecated() helper.Path::absolute()) are self-documenting.../../ traversal, network pathsHow can I help you explore Laravel packages today?