Skip to content

Commit

Permalink
Cleanup and refactor the code
Browse files Browse the repository at this point in the history
  • Loading branch information
caendesilva committed Dec 14, 2024
1 parent be1d051 commit 504d7b0
Showing 1 changed file with 4 additions and 19 deletions.
23 changes: 4 additions & 19 deletions packages/framework/src/Foundation/Kernel/Filesystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -188,37 +188,22 @@ public function smartGlob(string $pattern, int $flags = 0): Collection
/** @return \Illuminate\Support\Collection<int, string> */
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();
}
}

0 comments on commit 504d7b0

Please sign in to comment.