Skip to content

Commit

Permalink
Revert "Attempt druc#3 to exclude a path."
Browse files Browse the repository at this point in the history
This reverts commit b3abefb.
  • Loading branch information
fszotyi committed Mar 18, 2024
1 parent b3abefb commit 0d9b9c7
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 29 deletions.
5 changes: 1 addition & 4 deletions src/Commands/LangscannerCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
35 changes: 10 additions & 25 deletions src/RequiredTranslations.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 0d9b9c7

Please sign in to comment.