matthiasmullie/path-converter
Convert relative paths between source and target locations. Given a file currently relative to one path (e.g., an import), it returns the equivalent relative path from another path—useful for moving/minifying assets while keeping URLs correct.
storage_path()) are insufficient for dynamic relative path conversions. Ideal for:
url() references in CSS/JS during Laravel Mix builds).dev, staging, and prod).Str::replace() or Path facade, this package offers a dedicated, tested solution for path normalization (e.g., resolving ../ or . edge cases).$from, $to) and a single method call (convert())../, ../, /), reducing manual string manipulation.| Risk Area | Assessment |
|---|---|
| Path Format Assumptions | Relies on POSIX-style paths (forward slashes). Windows compatibility requires pre-processing (e.g., str_replace('\\', '/', $path)). |
| No Active Maintenance | Last release in 2020; no open issues or PRs. Low risk for simple conversions but higher risk for critical path logic in long-term projects. |
| Laravel-Specific Gaps | Requires manual conversion between Laravel’s absolute paths (e.g., storage_path()) and relative paths. Example: $converter->convert(basename($absolutePath)). |
| Alternatives Exist | Laravel’s Str::replace() or Path facade could replicate functionality with more effort. Example: $path = str_replace($from, $to, $relativePath); but lacks built-in normalization. |
| Performance | Negligible overhead for most use cases, but not optimized for high-frequency calls (e.g., per-request path conversions). |
Use Case Justification
Str::replace() or Path facade?
../ or .) or arbitrary base directory conversions?Windows Compatibility
/ vs \) and normalize inputs:
$converter = new Converter(str_replace('\\', '/', $from), str_replace('\\', '/', $to));
Maintenance Strategy
Performance Requirements
str_replace() or realpath() for optimization.Alternatives Evaluation
Illuminate\Support\Str::replace() or Illuminate\Filesystem\Filesystem::makePath() suffice?
$path = str_replace($from, $to, $relativePath); (but lacks normalization).mix-manifest.json).uploads/ → processed/).str_replace() or realpath():
$converter = new Converter('/old/base', '/new/base');
$result = $converter->convert('../../file.txt'); // Returns '../file.txt'
// Before (manual)
$newPath = str_replace('old/base', 'new/base', $path);
// After (using package)
$converter = new Converter('old/base', 'new/base');
$newPath = $converter->convert($path);
$converter = new Converter(
str_replace('\\', '/', $from),
str_replace('\\', '/', $to)
);
| Component | Compatibility Notes |
|---|---|
| PHP Version | Works with PHP 7.2+ (Laravel 7+). No PHP 8+ compatibility guarantees (but likely works due to simple string operations). |
| Laravel | No dependencies; integrates via Composer. |
| Path Formats | Assumes forward slashes. Windows requires pre-processing (e.g., str_replace('\\', '/', $path)). |
| Edge Cases | Handles ./, ../, and absolute paths, but test with deeply nested paths (e.g., ../../../../file.txt). |
| Laravel Helpers | Requires manual conversion between Laravel’s absolute paths (e.g., storage_path('file.txt')) and relative paths (e.g., 'file.txt'). Example: $converter->convert(basename($absolutePath)). |
composer require matthiasmullie/path-converter
$this->app->bind(Converter::class, function ($app) {
return new Converter(
$app['config']['paths.source'],
$app['config']['paths.target']
);
});
Converter into classes handling path logic (e.g., AssetCompiler, FileUploader, or CmsContentService).public function __construct(private Converter $converter) {}
public function updateAssetPaths(string $content): string {
return $this->converter->convert($content);
}
file.txt → ../file.txt).../../file.txt)../, /, or mixed separators).storage_path()) and relative paths.How can I help you explore Laravel packages today?