mirror of
https://github.com/DirectoryLister/DirectoryLister.git
synced 2025-08-29 08:40:42 +02:00
Updated breadcrumbs view function to handle running from a sub-directory
This commit is contained in:
@@ -24,10 +24,10 @@ class Breadcrumbs extends ViewFunction
|
||||
|
||||
return $breadcrumbs->filter(function (string $crumb) {
|
||||
return $crumb !== '.';
|
||||
})->reduce(function (array $carry, string $crumb) {
|
||||
$carry[$crumb] = end($carry) . "/{$crumb}";
|
||||
|
||||
return $carry;
|
||||
}, []);
|
||||
})->reduce(function (Collection $carry, string $crumb) {
|
||||
return $carry->put($crumb, $carry->last() . '/' . $crumb);
|
||||
}, new Collection)->map(function (string $path) {
|
||||
return '/' . ltrim(dirname($_SERVER['SCRIPT_NAME']) . $path, '/');
|
||||
});
|
||||
}
|
||||
}
|
||||
|
@@ -4,6 +4,7 @@ namespace Tests\ViewFunctions;
|
||||
|
||||
use App\ViewFunctions\Breadcrumbs;
|
||||
use Tests\TestCase;
|
||||
use Tightenco\Collect\Support\Collection;
|
||||
|
||||
class BreadcrumbsTest extends TestCase
|
||||
{
|
||||
@@ -11,17 +12,30 @@ class BreadcrumbsTest extends TestCase
|
||||
{
|
||||
$breadcrumbs = new Breadcrumbs($this->container, $this->config);
|
||||
|
||||
$this->assertEquals([
|
||||
$this->assertEquals(Collection::make([
|
||||
'foo' => '/foo',
|
||||
'bar' => '/foo/bar',
|
||||
'baz' => '/foo/bar/baz',
|
||||
], $breadcrumbs('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('.'));
|
||||
$this->assertEquals(new Collection, $breadcrumbs('.'));
|
||||
}
|
||||
|
||||
public function test_it_can_parse_breadcrumbs_from_a_subdirectory(): void
|
||||
{
|
||||
$_SERVER['SCRIPT_NAME'] = '/some/dir/index.php';
|
||||
|
||||
$breadcrumbs = new Breadcrumbs($this->container, $this->config);
|
||||
|
||||
$this->assertEquals(Collection::make([
|
||||
'foo' => '/some/dir/foo',
|
||||
'bar' => '/some/dir/foo/bar',
|
||||
'baz' => '/some/dir/foo/bar/baz',
|
||||
]), $breadcrumbs('foo/bar/baz'));
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user