From 504d7b0a3c6587ae3cef56f5029089d3521c48e5 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sat, 14 Dec 2024 10:49:56 +0100 Subject: [PATCH] Cleanup and refactor the code --- .../src/Foundation/Kernel/Filesystem.php | 23 ++++--------------- 1 file changed, 4 insertions(+), 19 deletions(-) diff --git a/packages/framework/src/Foundation/Kernel/Filesystem.php b/packages/framework/src/Foundation/Kernel/Filesystem.php index 0877de59b3b..e403781dea2 100644 --- a/packages/framework/src/Foundation/Kernel/Filesystem.php +++ b/packages/framework/src/Foundation/Kernel/Filesystem.php @@ -188,37 +188,22 @@ public function smartGlob(string $pattern, int $flags = 0): Collection /** @return \Illuminate\Support\Collection */ public function findFiles(string $directory, string|false $matchExtension = false, bool $recursive = false): Collection { - // Resolve the full directory path based on the project root - $directory = $this->path($directory); + $finder = Finder::create()->files()->in($this->path($directory)); - // Create a Symfony Finder instance - $finder = new Finder(); - - // Configure Finder to look in the directory - $finder->files()->in($directory); - - // Configure Finder for recursive or non-recursive search - if (! $recursive) { + if ($recursive === false) { $finder->depth('== 0'); } - // Optionally match file extensions (case-insensitively) if ($matchExtension !== false) { - // Normalize input by removing a leading dot if present - $normalizedExtension = ltrim($matchExtension, '.'); - - // Use a case-insensitive regex to match file extensions - $finder->name('/\.' . preg_quote($normalizedExtension, '/') . '$/i'); + $finder->name('/\.' . preg_quote(ltrim($matchExtension, '.'), '/') . '$/i'); } - // Collect relative paths $files = collect(); foreach ($finder as $file) { - $files->push(str_replace($directory.DIRECTORY_SEPARATOR, '', $file->getRealPath())); + $files->push(str_replace($this->path($directory) .DIRECTORY_SEPARATOR, '', $file->getRealPath())); } - // Sort files for consistent output return $files->sort()->values(); } }