symfony/finder
Symfony Finder component provides a fluent API to locate and iterate files and directories. Filter by name, extension, size, date, contents, or path; search multiple locations; and traverse recursively with sorting and ignore rules for flexible filesystem searches.
Illuminate\Filesystem\Filesystem (e.g., glob() methods) and Artisan commands (e.g., config:clear). Adopting it eliminates reinventing file-search wheel and aligns with Laravel’s architectural patterns.Finder::in($path)->name('*.php')->files()) mirrors Laravel’s Eloquent/Collection APIs, improving developer productivity and code readability. This reduces cognitive load for teams familiar with Laravel’s conventions.glob() or scandir(), Finder uses iterators (e.g., IteratorAggregate), enabling memory-efficient traversal of large directories (critical for Laravel’s /public or /storage paths). This is especially valuable for CLI tools or asset pipelines.symfony/finder:^8.0) and immediate use in any PHP file or Laravel service.Finder::make()) to standardize usage across the codebase. Example:
// app/Providers/AppServiceProvider.php
public function register()
{
$this->app->singleton('finder', function () {
return new \Symfony\Component\Finder\Finder();
});
}
php artisan asset:optimize). Example:
use Symfony\Component\Finder\Finder;
public function handle()
{
$finder = new Finder();
$files = $finder->in(storage_path('app/public'))->files();
foreach ($files as $file) {
// Process files (e.g., compress images)
}
}
Storage facades (e.g., Storage::disk('public')->path()) for cross-platform path resolution:
$finder = new Finder();
$files = $finder->in(Storage::disk('public')->path('assets'))->name('*.png')->files();
symfony/finder:7.4.x for PHP 8.1+ or 6.4.x for PHP 7.4+ if upgrading is not feasible.^8.0.5 or ^7.4.5 for stability.glob() for trivial cases (e.g., single-directory scans). Expect negligible overhead for complex queries but better memory efficiency for large directories.symfony/finder:7.4.x suffice?glob()/scandir() calls that could be incrementally replaced without breaking changes?Filesystem, Console, and Artisan components. Adopting it reduces technical debt and leverages existing patterns.HttpKernel, Process, Console), making Finder a natural fit with no additional dependencies.Storage, Filesystem, Cache (for path resolution).spatie/laravel-medialibrary, intervention/image, or laravel-excel.glob(), scandir(), DirectoryIterator) for replacement candidates.DirectoryIterator script) with Finder.// Before (custom logic)
$files = [];
$iterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($path));
foreach ($iterator as $file) {
if ($file->isFile() && $file->getExtension() === 'php') {
$files[] = $file->getPathname();
}
}
// After (Finder)
$finder = new \Symfony\Component\Finder\Finder();
$files = $finder->in($path)->name('*.php')->files();
glob() calls with Finder’s Finder::create()->in($path)->name($pattern)->files().php artisan asset:optimize).Storage facade for cross-platform paths.VfsStream).glob() for performance-critical paths.symfony/finder:^8.0 (PHP 8.4+).symfony/finder:^7.4 (PHP 8.1+) or ^6.4 (PHP 7.4+).Storage facade), and custom filesystems.$finder = new Finder();
$files = $finder->in(Storage::disk('s3')->path('assets'))->files();
/ vs. \).s3://bucket/path).SplFileInfo objects, compatible with Laravel’s Collection methodsHow can I help you explore Laravel packages today?