From 28418e2d17527333763c319c549aaf8a4b0735d1 Mon Sep 17 00:00:00 2001 From: Amir Rami Date: Tue, 8 Nov 2022 11:09:53 +0100 Subject: [PATCH] [Feature] Exclude subdirectories (#45) * [Feature] Exclude subdirectories * Fix test --- README.md | 8 ++++++++ config/localizator.php | 8 ++++++++ src/Services/FileFinder.php | 5 ++++- tests/LocalizatorTest.php | 30 ++++++++++++++++++++++++++++++ 4 files changed, 50 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 995ee81..01caec8 100644 --- a/README.md +++ b/README.md @@ -97,6 +97,14 @@ return [ * Directories which should be looked inside. */ 'dirs' => ['resources/views'], + + /** + * Subdirectories which will be excluded. + * The values must be relative to the included directory paths. + */ + 'exclude' => [ + // + ], /** * Patterns by which files should be queried. diff --git a/config/localizator.php b/config/localizator.php index 1a9ebbd..850b077 100644 --- a/config/localizator.php +++ b/config/localizator.php @@ -29,6 +29,14 @@ */ 'dirs' => ['resources/views'], + /** + * Subdirectories which will be excluded. + * The values must be relative to the included directory paths. + */ + 'exclude' => [ + // + ], + /** * Patterns by which files should be queried. * The values can be a regular expression, glob, or just a string. diff --git a/src/Services/FileFinder.php b/src/Services/FileFinder.php index 4c1ada8..82bcf06 100644 --- a/src/Services/FileFinder.php +++ b/src/Services/FileFinder.php @@ -33,7 +33,10 @@ public function getFiles(): Collection }, $this->config['search']['dirs']); return new Collection( - (new Finder)->in($directories)->name($this->config['search']['patterns'])->files() + (new Finder)->in($directories) + ->notPath($this->config['search']['exclude']) + ->name($this->config['search']['patterns']) + ->files() ); } } diff --git a/tests/LocalizatorTest.php b/tests/LocalizatorTest.php index 8c80be5..e59c32f 100644 --- a/tests/LocalizatorTest.php +++ b/tests/LocalizatorTest.php @@ -333,4 +333,34 @@ public function testRemoveMissingKeys(): void // Cleanup. self::flushDirectories('lang', 'views'); } + + public function testDirectoriesAreBeingExcluded(): void + { + mkdir(resource_path('views/sub1'), 0755); + mkdir(resource_path('views/sub2'), 0755); + + $this->createTestView("{{ __('Foo') }}"); + $this->createTestView("{{ __('Bar') }}", 'sub1/test'); + $this->createTestView("{{ __('Baz') }}", 'sub2/test'); + + config([ + 'localizator.sort' => false, + 'localizator.search.exclude' => 'sub1', + ]); + + $this->artisan('localize', ['lang' => 'en']); + + // Do created locale files exist? + self::assertJsonLangFilesExist('en'); + + // Do their contents match the expected results? + $contents = $this->getJsonLangContents('en'); + self::assertSame([ + 'Foo' => 'Foo', + 'Baz' => 'Baz', + ], $contents); + + // Cleanup. + self::flushDirectories('lang', 'views'); + } }