mirror of
https://github.com/DirectoryLister/DirectoryLister.git
synced 2025-09-02 02:12:37 +02:00
Extracted Finder composition to a dedicated FilesComposer class
This commit is contained in:
49
app/Bootstrap/FilesComposer.php
Normal file
49
app/Bootstrap/FilesComposer.php
Normal file
@@ -0,0 +1,49 @@
|
||||
<?php
|
||||
|
||||
namespace App\Bootstrap;
|
||||
|
||||
use PHLAK\Config\Config;
|
||||
use Symfony\Component\Finder\Finder;
|
||||
use Symfony\Component\Finder\SplFileInfo;
|
||||
use Tightenco\Collect\Support\Collection;
|
||||
|
||||
class FilesComposer
|
||||
{
|
||||
/** @var Config Application config */
|
||||
protected $config;
|
||||
|
||||
/** @var Finder Symfony finder component */
|
||||
protected $finder;
|
||||
|
||||
/** @var Collection Collection of hidden file paths */
|
||||
protected $hiddenFiles;
|
||||
|
||||
public function __construct(Config $config, Finder $finder)
|
||||
{
|
||||
$this->config = $config;
|
||||
$this->finder = $finder;
|
||||
|
||||
$this->hiddenFiles = Collection::make(
|
||||
$this->config->get('hidden_files', [])
|
||||
)->map(function (string $file) {
|
||||
return glob($file, GLOB_BRACE | GLOB_NOSORT);
|
||||
})->flatten()->map(function (string $file) {
|
||||
return realpath($file);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Setup the Finder component.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __invoke(): void
|
||||
{
|
||||
$this->finder->depth(0)->followLinks();
|
||||
$this->finder->ignoreVCS($this->config->get('ignore_vcs_files', false));
|
||||
$this->finder->filter(function (SplFileInfo $file) {
|
||||
return ! $this->hiddenFiles->contains($file->getRealPath());
|
||||
});
|
||||
$this->finder->sortByName(true)->sortByType();
|
||||
}
|
||||
}
|
@@ -3,11 +3,9 @@
|
||||
namespace App\Controllers;
|
||||
|
||||
use PHLAK\Config\Config;
|
||||
use Slim\Psr7\Request;
|
||||
use Slim\Psr7\Response;
|
||||
use Slim\Views\Twig;
|
||||
use Symfony\Component\Finder\Finder;
|
||||
use Symfony\Component\Finder\SplFileInfo;
|
||||
use Tightenco\Collect\Support\Collection;
|
||||
|
||||
class DirectoryController
|
||||
@@ -18,9 +16,6 @@ class DirectoryController
|
||||
/** @var Twig Twig templating component */
|
||||
protected $view;
|
||||
|
||||
/** @var Collection Collection of hidden file paths */
|
||||
protected $hiddenFiles;
|
||||
|
||||
/**
|
||||
* Create a new DirectoryController object.
|
||||
*
|
||||
@@ -31,37 +26,22 @@ class DirectoryController
|
||||
{
|
||||
$this->config = $config;
|
||||
$this->view = $view;
|
||||
|
||||
$this->hiddenFiles = Collection::make(
|
||||
$this->config->get('hidden_files', [])
|
||||
)->map(function (string $file) {
|
||||
return glob($file, GLOB_BRACE | GLOB_NOSORT);
|
||||
})->flatten()->map(function (string $file) {
|
||||
return realpath($file);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Invoke the DirectoryController.
|
||||
*
|
||||
* @param \Slim\Psr7\Request $request
|
||||
* @param \Slim\Psr7\Response $response
|
||||
* @param string $path
|
||||
* @param \Symfony\Component\Finder\Finder $finder
|
||||
* @param \Slim\Psr7\Response $response
|
||||
* @param string $path
|
||||
*
|
||||
* @return \Psr\Http\Message\ResponseInterface
|
||||
*/
|
||||
public function __invoke(Request $request, Response $response, string $path = '.')
|
||||
public function __invoke(Finder $files, Response $response, string $path = '.')
|
||||
{
|
||||
$files = Finder::create()->in($path)->depth(0)->followLinks();
|
||||
$files->ignoreVCS($this->config->get('ignore_vcs_files', false));
|
||||
$files->filter(function (SplFileInfo $file) {
|
||||
return ! $this->hiddenFiles->contains($file->getRealPath());
|
||||
});
|
||||
$files->sortByName(true)->sortByType();
|
||||
|
||||
return $this->view->render($response, 'index.twig', [
|
||||
'breadcrumbs' => $this->breadcrumbs($path),
|
||||
'files' => $files,
|
||||
'files' => $files->in($path),
|
||||
]);
|
||||
}
|
||||
|
||||
|
@@ -1,5 +1,6 @@
|
||||
<?php
|
||||
|
||||
use App\Bootstrap\FilesComposer;
|
||||
use App\Bootstrap\ViewComposer;
|
||||
use App\Controllers;
|
||||
use DI\Bridge\Slim\Bridge;
|
||||
@@ -7,6 +8,7 @@ use DI\Container;
|
||||
use Dotenv\Dotenv;
|
||||
use PHLAK\Config\Config;
|
||||
use Slim\Views\Twig;
|
||||
use Symfony\Component\Finder\Finder;
|
||||
|
||||
require __DIR__ . '/vendor/autoload.php';
|
||||
|
||||
@@ -21,11 +23,13 @@ $container = new Container();
|
||||
|
||||
/** Register dependencies */
|
||||
$container->set(Config::class, new Config('app/config'));
|
||||
$container->set(Finder::class, new Finder());
|
||||
$container->set(Twig::class, function (Config $config) {
|
||||
return new Twig("app/themes/{$config->get('theme', 'default')}");
|
||||
});
|
||||
|
||||
/** Configure the view handler */
|
||||
/** Configure the application components */
|
||||
$container->call(FilesComposer::class);
|
||||
$container->call(ViewComposer::class);
|
||||
|
||||
/** Create the application */
|
||||
|
Reference in New Issue
Block a user