Last active
August 12, 2024 08:41
-
-
Save ewilan-riviere/ee6a4481bfd50bf86f7eae9d7baa662d to your computer and use it in GitHub Desktop.
Parse files with PHP
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /** | |
| * Tested with 1290 files: 21.43 sec | |
| */ | |
| private function parseFilesWithLaravelFile(string $path): array | |
| { | |
| $files = \Illuminate\Support\Facades\File::allFiles($path); | |
| return array_map(fn (SplFileInfo $file) => $file->getPathname(), $files); | |
| } | |
| /** | |
| * Tested with 1290 files: 1.97 sec | |
| */ | |
| private function parseFilesWithGlob(string $path, array &$results = []): array | |
| { | |
| $files = glob($path.'/*'); | |
| foreach ($files as $file) { | |
| if (is_dir($file)) { | |
| $this->parseFilesWithGlob($file, $results); | |
| } else { | |
| $results[] = $file; | |
| } | |
| } | |
| return $results; | |
| } | |
| /** | |
| * Tested with 1290 files: 4.34 sec | |
| */ | |
| private function parseFilesWithRecursiveIterator(string $path): array | |
| { | |
| $rii = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($path)); | |
| $files = []; | |
| /** @var SplFileInfo $file */ | |
| foreach ($rii as $file) { | |
| if ($file->isDir()) { | |
| continue; | |
| } | |
| $files[] = $file->getPathname(); | |
| } | |
| return $files; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment