mirror of
https://github.com/DirectoryLister/DirectoryLister.git
synced 2025-08-22 21:54:15 +02:00
Extracted hidden files to a dedicated class and internally cache (memoize) the hidden file regex pattern
This commit is contained in:
@@ -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
29
app/src/HiddenFiles.php
Normal 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));
|
||||
}
|
||||
}
|
@@ -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
|
||||
|
Reference in New Issue
Block a user