From 0d9b9c7242d5ef95f02cfa97f3c7eb6e1761889e Mon Sep 17 00:00:00 2001 From: Zoltan Fabian Date: Mon, 18 Mar 2024 13:37:59 +0200 Subject: [PATCH] Revert "Attempt #3 to exclude a path." This reverts commit b3abefb6f0c942ea25ca0b954870f0051e729779. --- src/Commands/LangscannerCommand.php | 5 +---- src/RequiredTranslations.php | 35 +++++++++-------------------- 2 files changed, 11 insertions(+), 29 deletions(-) diff --git a/src/Commands/LangscannerCommand.php b/src/Commands/LangscannerCommand.php index 957eeee..69cc0c8 100644 --- a/src/Commands/LangscannerCommand.php +++ b/src/Commands/LangscannerCommand.php @@ -12,10 +12,7 @@ class LangscannerCommand extends Command { - protected $signature = 'langscanner - {language?} - {--path= : The path to scan for translation keys (ex: --path=app/Modules/Module1 )} - {--exclude-path=* : Directories to exclude from the scan (ex: --exclude-path=app/Modules/)}'; + protected $signature = 'langscanner {language?} {--path= : The path to scan for translation keys (ex: --path=app/Modules/Module1 )} {--exclude-path= : Directory to exclude from the scan (ex: --exclude-path=app/Modules/) }'; protected $description = "Updates translation files with missing translation keys."; public function handle(Filesystem $filesystem): void diff --git a/src/RequiredTranslations.php b/src/RequiredTranslations.php index 8592056..047ba61 100644 --- a/src/RequiredTranslations.php +++ b/src/RequiredTranslations.php @@ -58,40 +58,25 @@ private function files(): array { $allFiles = $this->disk->allFiles($this->paths); + $excludedPathsAbsolute = array_map(function ($path) { + return realpath($path) ?: $path; + }, $this->excludedPaths); + return Collection::make($allFiles) - ->reject(function ($file) { - $filePath = $this->normalizePath($file->getPathName()); + ->filter(function ($file) use ($excludedPathsAbsolute) { + $filePath = realpath($file->getPathName()); - foreach ($this->excludedPaths as $excludedPath) { - $normalizedExcludedPath = $this->normalizePath($excludedPath); - if (strpos($filePath, $normalizedExcludedPath) === 0) { - return true; + foreach ($excludedPathsAbsolute as $excludedPath) { + if ($this->startsWith($filePath, $excludedPath)) { + return false; } } - return false; + return true; }) ->toArray(); } - /* - * Converts Directory Separators - * Different operating systems use different directory separators in file paths. Windows uses backslashes (\), - * while UNIX-like systems, including Linux and macOS, use forward slashes (/). The str_replace('\\', '/', $path) - * part of the function converts all backslashes to forward slashes. This ensures that paths are handled in a uniform way, - * regardless of the operating system on which your PHP code is running. - * - * Removes Trailing Slashes - * Paths can sometimes end with a trailing slash (or backslash, depending on the system), especially when referring to directories. - * However, when comparing two paths, a trailing slash might lead to inconsistencies where two paths that essentially refer to the same directory are considered different. - * For example, some/path/ and some/path would be considered different strings even though they refer to the same directory. - * The rtrim($path, '/') part removes any trailing forward slashes from the path, ensuring that paths are compared without considering these potentially extraneous characters. - */ - private function normalizePath($path): string - { - return rtrim(str_replace('\\', '/', $path), '/'); - } - private function startsWith($haystack, $needle): bool { return strncmp($haystack, $needle, strlen($needle)) === 0;