mirror of
https://github.com/DirectoryLister/DirectoryLister.git
synced 2025-09-02 18:33:04 +02:00
Extracted breadcrumb logic to a view function
This commit is contained in:
@@ -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.
|
||||
*
|
||||
|
@@ -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,
|
||||
|
33
app/ViewFunctions/Breadcrumbs.php
Normal file
33
app/ViewFunctions/Breadcrumbs.php
Normal file
@@ -0,0 +1,33 @@
|
||||
<?php
|
||||
|
||||
namespace App\ViewFunctions;
|
||||
|
||||
use Tightenco\Collect\Support\Collection;
|
||||
|
||||
class Breadcrumbs extends ViewFunction
|
||||
{
|
||||
/** @var string The function name */
|
||||
protected $name = 'breadcrumbs';
|
||||
|
||||
/**
|
||||
* Build an array of breadcrumbs for a given path.
|
||||
*
|
||||
* @param string $path
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function __invoke(string $path)
|
||||
{
|
||||
$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;
|
||||
}, []);
|
||||
}
|
||||
}
|
@@ -3,7 +3,7 @@
|
||||
<div class="text-white text-sm tracking-tight mb-2 md:my-2">
|
||||
<a href="/" class="hover:underline">Home</a>
|
||||
|
||||
{% for name, path in breadcrumbs %}
|
||||
{% for name, path in breadcrumbs(relative_path) %}
|
||||
/ <a href="{{ path }}" class="hover:underline">{{ name }}</a>
|
||||
{% endfor %}
|
||||
</div>
|
||||
|
@@ -7,7 +7,7 @@
|
||||
|
||||
<link rel="stylesheet" href="{{ asset('app.css') }}">
|
||||
|
||||
<title>{{ title | default('Home') }} • Directory Lister</title>
|
||||
<title>{{ relative_path | default('Home') }} • Directory Lister</title>
|
||||
|
||||
<div id="app" class="flex flex-col min-h-screen font-mono {{ config('dark_mode') ? 'dark-mode' : 'light-mode' }}">
|
||||
{% block content %}{% endblock %}
|
||||
|
27
tests/ViewFunctions/BreadcrumbsTest.php
Normal file
27
tests/ViewFunctions/BreadcrumbsTest.php
Normal file
@@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\ViewFunctions;
|
||||
|
||||
use App\ViewFunctions\Breadcrumbs;
|
||||
use Tests\TestCase;
|
||||
|
||||
class BreadcrumbsTest extends TestCase
|
||||
{
|
||||
public function test_it_can_parse_breadcrumbs_from_the_path(): void
|
||||
{
|
||||
$breadcrumbs = new Breadcrumbs($this->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('.'));
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user