Extracted hidden files to a dedicated class and internally cache (memoize) the hidden file regex pattern

This commit is contained in:
Chris Kankiewicz
2020-05-31 16:20:12 -07:00
parent d54be8e122
commit f9440f1593
3 changed files with 51 additions and 29 deletions

View File

@@ -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());
}
}

29
app/src/HiddenFiles.php Normal file
View File

@@ -0,0 +1,29 @@
<?php
namespace App;
use DI\Container;
use Tightenco\Collect\Support\Collection;
class HiddenFiles extends Collection
{
/**
* Createa a new HiddenFiles collection object.
*
* @param \DI\Container $container
*/
public function __construct(Container $container)
{
$items = $container->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));
}
}

View File

@@ -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