framework
framework copied to clipboard
[9.x] Allow custom `PathNormalizer` for filesystem
Currently all filesystems use the default WhitespacePathNormalizer provided by thephpleague/flysystem.
While this is mostly fine, it throws an exception for "funky" whitespaces. Currently there is no general solution to alter this behavior to e.g. just remove this funky whitespaces.
This PR allows a custom PathNormalizer class to be defined in filesystems config.
The documentation for the new configuration option in the laravel/laravel repo could look something like:
/*
|--------------------------------------------------------------------------
| Path Normalization
|--------------------------------------------------------------------------
|
| Here you may specify a custom path normalizer. It has to implement
| `\Illuminate\Contracts\Filesystem\PathNormalizer`.
|
*/
'path_normalizer' => null,
Here an example for a custom PathNormalizer that alters this behavior:
<?php
namespace App\Filesystem;
use Illuminate\Contracts\Filesystem\PathNormalizer as PathNormalizerContract;
use League\Flysystem\WhitespacePathNormalizer;
class PathNormalizer extends WhitespacePathNormalizer implements PathNormalizerContract
{
public function normalizePath(string $path): string
{
// Remove funky whitespaces.
$path = preg_replace('#\p{C}+#u', '', $path);
return parent::normalizePath($path);
}
}
This exact behavior was also recently changed in Spaties popular spatie/laravel-medialibrary package:
https://github.com/spatie/laravel-medialibrary/pull/3105
Fixes #44244.