diff --git a/app/Bootstrap/FilesComposer.php b/app/Bootstrap/FilesComposer.php new file mode 100644 index 0000000..4c2628d --- /dev/null +++ b/app/Bootstrap/FilesComposer.php @@ -0,0 +1,49 @@ +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(); + } +} diff --git a/app/Controllers/DirectoryController.php b/app/Controllers/DirectoryController.php index 325b3b2..9b9b37a 100644 --- a/app/Controllers/DirectoryController.php +++ b/app/Controllers/DirectoryController.php @@ -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), ]); } diff --git a/index.php b/index.php index 713aee7..2b80145 100644 --- a/index.php +++ b/index.php @@ -1,5 +1,6 @@ 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 */