sebastian/file-filter
Filter and whitelist/blacklist files and directories in PHP projects. Extracted from phpunit/phpunit, this lightweight library helps build file lists while honoring include/exclude rules—useful for test runners, coverage tools, and CLI utilities.
sebastian/file-filter package is a small, focused utility for filtering files (e.g., by extension, path, or regex). It is not a full-fledged file system library (e.g., no async I/O, no metadata manipulation) but excels at precisely filtering files—ideal for:
vendor/, .git/, or specific file types).composer require sebastian/file-filter (or --dev for CI/test-only use).use SebastianBergmann\FileFilter\Filter;
$filter = new Filter();
$files = $filter->filter(['file1.php', 'file2.txt'], ['*.php']); // Returns ['file1.php']
Storage::files(), File::exists()).| Risk Area | Assessment | Mitigation Strategy |
|---|---|---|
| Performance | Minimal overhead (pure PHP, no syscalls in filtering logic). | Benchmark in target environment if processing millions of files. |
| Compatibility | PHP 8.1+ (as of 2026). | Check Laravel version compatibility (Laravel 10+ uses PHP 8.1+). |
| Thread Safety | Stateless; safe for concurrent use (e.g., in queues or parallel jobs). | No action required. |
| Future Maintenance | Low activity (last release 2026-04-22). | Monitor for breaking changes; fork if needed (BSD-3-Clause allows modification). |
| Edge Cases | Handles Unicode paths, symlinks (configurable via Filter constructor). |
Test with Laravel’s filesystem (e.g., Storage::disk('local')->files()). |
phpunit.xml exclusions) or runtime file operations (e.g., asset processing)?File::all() or augment it?File facade or Illuminate\Support\Facades\Storage suffice?Laravel Compatibility:
Storage facade for path resolution:
use Illuminate\Support\Facades\Storage;
use SebastianBergmann\FileFilter\Filter;
$filter = new Filter();
$files = Storage::disk('public')->files();
$filtered = $filter->filter($files, ['*.jpg', '!*.min.jpg']);
php artisan filter:assets).filesystem.updated.Ecosystem Synergy:
| Step | Action | Laravel-Specific Notes |
|---|---|---|
| 1. Assessment | Audit current file-filtering logic (e.g., custom regex, glob(), or manual loops). |
Identify pain points (e.g., complex exclusions, performance bottlenecks). |
| 2. Proof of Concept | Replace a single filtering use case (e.g., test files or asset processing). | Example: Replace array_filter(glob('public/*.{jpg,png}'), fn($f) => !str_contains($f, 'thumb')). |
| 3. Dependency Scope | Add to composer.json as dev (tests) or require (runtime). |
Use --dev if only for testing; otherwise, ensure CI includes the package. |
| 4. API Wrapping | (Optional) Create a Laravel service class to standardize usage: | |
| ```php | ||
| namespace App\Services; | ||
| class FileFilterService { | ||
| public function filter(array $files, array $patterns): array { | ||
| return (new Filter())->filter($files, $patterns); | ||
| } | ||
| } | ||
| ``` | ||
| 5. Testing | Verify edge cases (Unicode paths, symlinks, empty inputs). | Use Laravel’s Storage facade to test different disks (local, S3, etc.). |
| 6. Documentation | Add usage examples to Laravel’s internal docs or a README.md in the app. |
Note: Package has no Laravel-specific docs; custom examples will be needed. |
SplFileInfo objects).Storage facade, ensure paths are absolute or disk-relative (e.g., 'public/image.jpg').Filter constructor if needed:
$filter = new Filter(['allow-symlinks' => false]);
glob() to FileFilter in phpunit.xml.HandleAssetUpload job to filter processed files.$filter = new Filter(['verbose' => true]); // If supported (check docs).
Storage facade for path resolution issues.dd() or Log::debug()How can I help you explore Laravel packages today?