diff --git a/app/src/Factories/FinderFactory.php b/app/src/Factories/FinderFactory.php index d7e6e68..dc8c572 100644 --- a/app/src/Factories/FinderFactory.php +++ b/app/src/Factories/FinderFactory.php @@ -3,6 +3,7 @@ namespace App\Factories; use App\Exceptions\InvalidConfiguration; +use App\HiddenFiles; use Closure; use DI\Container; use PHLAK\Splat\Glob; @@ -15,14 +16,21 @@ class FinderFactory /** @var Container The application container */ protected $container; + /** @var Collection Collection of hidden files */ + protected $hiddenFiles; + + /** @var Glob Hidden files pattern cache */ + protected $pattern; + /** * Create a new FinderFactory object. * * @param \DI\Container $container */ - public function __construct(Container $container) + public function __construct(Container $container, HiddenFiles $hiddenFiles) { $this->container = $container; + $this->hiddenFiles = $hiddenFiles; } /** @@ -35,7 +43,7 @@ class FinderFactory $finder = Finder::create()->followLinks(); $finder->ignoreVCS($this->container->get('hide_vcs_files')); - if ($this->hiddenFiles()->isNotEmpty()) { + if ($this->hiddenFiles->isNotEmpty()) { $finder->filter(function (SplFileInfo $file): bool { return ! $this->isHidden($file); }); @@ -59,24 +67,6 @@ class FinderFactory return $finder; } - /** - * Get a collection of hidden file paths. - * - * @return \Tightenco\Collect\Support\Collection - */ - protected function hiddenFiles(): Collection - { - return Collection::make( - $this->container->get('hidden_files') - )->when(is_readable($this->container->get('hidden_files_list')), function (Collection $collection) { - return $collection->merge( - file($this->container->get('hidden_files_list'), FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES) - ); - })->when($this->container->get('hide_app_files'), function (Collection $collection) { - return $collection->merge($this->container->get('app_files')); - })->unique(); - } - /** * Determine if a file should be hidden. * @@ -86,10 +76,12 @@ class FinderFactory */ protected function isHidden(SplFileInfo $file): bool { - return Glob::pattern( - Glob::escape( + if (! isset($this->pattern)) { + $this->pattern = Glob::pattern(sprintf('%s{%s}', Glob::escape( $this->container->get('base_path') . DIRECTORY_SEPARATOR - ) . sprintf('{%s}', $this->hiddenFiles()->implode(',')) - )->matchStart($file->getRealPath()); + ), $this->hiddenFiles->implode(','))); + } + + return $this->pattern->matchStart($file->getRealPath()); } } diff --git a/app/src/HiddenFiles.php b/app/src/HiddenFiles.php new file mode 100644 index 0000000..2c27b98 --- /dev/null +++ b/app/src/HiddenFiles.php @@ -0,0 +1,29 @@ +get('hidden_files'); + + if (is_readable($container->get('hidden_files_list'))) { + array_merge($items, file($container->get('hidden_files_list'), FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES)); + } + + if ($container->get('hide_app_files')) { + array_merge($container->get('app_files')); + } + + parent::__construct(array_unique($items)); + } +} diff --git a/tests/Factories/FinderFactoryTest.php b/tests/Factories/FinderFactoryTest.php index 8c3c622..90e1671 100644 --- a/tests/Factories/FinderFactoryTest.php +++ b/tests/Factories/FinderFactoryTest.php @@ -4,6 +4,7 @@ namespace Tests\Factories; use App\Exceptions\InvalidConfiguration; use App\Factories\FinderFactory; +use App\HiddenFiles; use Symfony\Component\Finder\Finder; use Symfony\Component\Finder\SplFileInfo; use Tests\TestCase; @@ -12,7 +13,7 @@ class FinderFactoryTest extends TestCase { public function test_it_can_compose_the_finder_component(): void { - $finder = (new FinderFactory($this->container, $this->cache))(); + $finder = (new FinderFactory($this->container, $this->container->get(HiddenFiles::class)))(); $this->assertInstanceOf(Finder::class, $finder); @@ -35,7 +36,7 @@ class FinderFactoryTest extends TestCase } )); - $finder = (new FinderFactory($this->container, $this->cache))(); + $finder = (new FinderFactory($this->container, $this->container->get(HiddenFiles::class)))(); $finder->in($this->filePath('subdir'))->depth(0); $this->assertEquals([ @@ -51,7 +52,7 @@ class FinderFactoryTest extends TestCase { $this->container->set('reverse_sort', true); - $finder = (new FinderFactory($this->container, $this->cache))(); + $finder = (new FinderFactory($this->container, $this->container->get(HiddenFiles::class)))(); $finder->in($this->filePath('subdir'))->depth(0); $this->assertEquals([ @@ -69,7 +70,7 @@ class FinderFactoryTest extends TestCase 'subdir/alpha.scss', 'subdir/charlie.bash', '**/*.yaml' ]); - (new FinderFactory($this->container, $this->cache))(); + (new FinderFactory($this->container, $this->container->get(HiddenFiles::class)))(); $finder = $this->container->get(Finder::class); $finder->in($this->filePath('subdir'))->depth(0); @@ -87,7 +88,7 @@ class FinderFactoryTest extends TestCase $this->expectException(InvalidConfiguration::class); - (new FinderFactory($this->container, $this->cache))(); + (new FinderFactory($this->container, $this->container->get(HiddenFiles::class)))(); } protected function getFilesArray(Finder $finder): array