From 13fe211755b1a2429b665d1c6548eb35fff72d99 Mon Sep 17 00:00:00 2001 From: kubotak-is Date: Fri, 3 Dec 2021 00:27:25 +0900 Subject: [PATCH] fix: Recursively search directories --- src/Config.php | 4 ++-- src/File.php | 24 +++++++++++++++++++++++- 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/src/Config.php b/src/Config.php index fdf7f7c..8723f7a 100644 --- a/src/Config.php +++ b/src/Config.php @@ -27,8 +27,8 @@ public function getDirs(): array return $this->dirs; } - public function getExtensions(): string + public function getExtensions(): array { - return implode(",", $this->extensions); + return $this->extensions; } } diff --git a/src/File.php b/src/File.php index 9b3b85a..f9ac78d 100644 --- a/src/File.php +++ b/src/File.php @@ -7,6 +7,28 @@ class File { public static function getFiles(string $dir, Config $config): array { - return glob(getcwd(). '/' . $dir . '/**/*.{' . $config->getExtensions() . '}', GLOB_BRACE) ?? []; + $files = []; + self::rglob(getcwd(). '/' . $dir, $config->getExtensions(), $files); + return $files; + } + + public static function rglob(string $dir, array $exts, array &$results=[]) { + $ls = glob($dir); + + if (is_array($ls)) { + foreach ($ls as $item) { + if (is_dir($item)) { + self::rglob($item . '/*', $exts, $results); + } + if (is_file($item)) { + $ext = substr($item, strrpos($item, '.') + 1); + if (in_array($ext, $exts, true)) { + $results[] = $item; + } + } + } + } + + return $results; } }