From d0e0a80ada8ec905a6fdb24f0174d5a4d29ef3d2 Mon Sep 17 00:00:00 2001 From: Chris Kankiewicz Date: Wed, 8 Jan 2020 10:41:57 -0700 Subject: [PATCH] Extracted breadcrumb logic to a view function --- app/Controllers/DirectoryController.php | 25 +--------------- app/Providers/TwigProvider.php | 1 + app/ViewFunctions/Breadcrumbs.php | 33 ++++++++++++++++++++++ app/resources/views/components/header.twig | 2 +- app/resources/views/layouts/app.twig | 2 +- tests/ViewFunctions/BreadcrumbsTest.php | 27 ++++++++++++++++++ 6 files changed, 64 insertions(+), 26 deletions(-) create mode 100644 app/ViewFunctions/Breadcrumbs.php create mode 100644 tests/ViewFunctions/BreadcrumbsTest.php diff --git a/app/Controllers/DirectoryController.php b/app/Controllers/DirectoryController.php index 2e99fd2..eeae838 100644 --- a/app/Controllers/DirectoryController.php +++ b/app/Controllers/DirectoryController.php @@ -65,8 +65,7 @@ class DirectoryController 'files' => $search ? $files->name( sprintf('/(?:.*)%s(?:.*)/i', preg_quote($search, '/')) ) : $files->depth(0), - 'title' => $this->relativePath($path), - 'breadcrumbs' => $this->breadcrumbs($path), + 'relative_path' => $this->relativePath($path), 'is_root' => $this->isRoot($path), 'readme' => $this->readme($path), 'search' => $search, @@ -87,28 +86,6 @@ class DirectoryController )->filter()->implode('/'); } - /** - * Build an array of breadcrumbs for a given path. - * - * @param string $path - * - * @return array - */ - protected function breadcrumbs(string $path): array - { - $breadcrumbs = Collection::make(explode('/', $path))->diff( - explode('/', $this->container->get('base_path')) - )->filter(); - - return $breadcrumbs->filter(function (string $crumb) { - return $crumb !== '.'; - })->reduce(function (array $carry, string $crumb) { - $carry[$crumb] = end($carry) . "/{$crumb}"; - - return $carry; - }, []); - } - /** * Determine if a provided path is the root path. * diff --git a/app/Providers/TwigProvider.php b/app/Providers/TwigProvider.php index 05c0a4e..51537ed 100644 --- a/app/Providers/TwigProvider.php +++ b/app/Providers/TwigProvider.php @@ -15,6 +15,7 @@ class TwigProvider /** @const Constant description */ protected const VIEW_FUNCTIONS = [ ViewFunctions\Asset::class, + ViewFunctions\Breadcrumbs::class, ViewFunctions\Config::class, ViewFunctions\Icon::class, ViewFunctions\Markdown::class, diff --git a/app/ViewFunctions/Breadcrumbs.php b/app/ViewFunctions/Breadcrumbs.php new file mode 100644 index 0000000..415053d --- /dev/null +++ b/app/ViewFunctions/Breadcrumbs.php @@ -0,0 +1,33 @@ +diff( + explode('/', $this->container->get('base_path')) + )->filter(); + + return $breadcrumbs->filter(function (string $crumb) { + return $crumb !== '.'; + })->reduce(function (array $carry, string $crumb) { + $carry[$crumb] = end($carry) . "/{$crumb}"; + + return $carry; + }, []); + } +} diff --git a/app/resources/views/components/header.twig b/app/resources/views/components/header.twig index c6481bb..18f391d 100644 --- a/app/resources/views/components/header.twig +++ b/app/resources/views/components/header.twig @@ -3,7 +3,7 @@
Home - {% for name, path in breadcrumbs %} + {% for name, path in breadcrumbs(relative_path) %} / {{ name }} {% endfor %}
diff --git a/app/resources/views/layouts/app.twig b/app/resources/views/layouts/app.twig index f720c44..747bfc0 100644 --- a/app/resources/views/layouts/app.twig +++ b/app/resources/views/layouts/app.twig @@ -7,7 +7,7 @@ -{{ title | default('Home') }} • Directory Lister +{{ relative_path | default('Home') }} • Directory Lister
{% block content %}{% endblock %} diff --git a/tests/ViewFunctions/BreadcrumbsTest.php b/tests/ViewFunctions/BreadcrumbsTest.php new file mode 100644 index 0000000..5fb9359 --- /dev/null +++ b/tests/ViewFunctions/BreadcrumbsTest.php @@ -0,0 +1,27 @@ +container, $this->config); + + $this->assertEquals([ + 'foo' => '/foo', + 'bar' => '/foo/bar', + 'baz' => '/foo/bar/baz', + ], $breadcrumbs('foo/bar/baz')); + } + + public function test_it_can_parse_breadcrumbs_for_dot_path(): void + { + $breadcrumbs = new Breadcrumbs($this->container, $this->config); + + $this->assertEquals([], $breadcrumbs('.')); + } +}