Fixed file and parent dir links not working properly

This commit is contained in:
Chris Kankiewicz
2020-01-26 01:21:08 -07:00
parent 9ad8c54d49
commit 1730c276ed
4 changed files with 29 additions and 18 deletions

View File

@@ -18,8 +18,10 @@ class ParentDir extends ViewFunction
*/
public function __invoke(string $path)
{
return Collection::make(
$parentDir = dirname($_SERVER['SCRIPT_NAME']) . '/' . Collection::make(
explode('/', $path)
)->filter()->slice(0, -1)->implode('/');
return '/' . ltrim($parentDir, '/');
}
}

View File

@@ -1,5 +1,5 @@
<a
href="/{{ parentDir ? parent_dir(path) : file.getPathname }}"
href="{{ parentDir ? parent_dir(path) : url(file.getPathname) }}"
class="flex flex-col items-center rounded-lg font-mono group hover:bg-gray-200 hover:shadow"
>
<div class="flex justify-between items-center p-4 w-full">

View File

@@ -0,0 +1,25 @@
<?php
namespace Tests\ViewFunctions;
use App\ViewFunctions\ParentDir;
use Tests\TestCase;
class ParentDirTest extends TestCase
{
public function test_it_can_get_the_parent_directory_from_a_path(): void
{
$parentDir = new ParentDir($this->container, $this->config);
$this->assertEquals('/foo/bar', $parentDir('foo/bar/baz'));
}
public function test_it_can_get_the_parent_directory_from_a_path_in_a_subdirectory(): void
{
$_SERVER['SCRIPT_NAME'] = '/some/dir/index.php';
$parentDir = new ParentDir($this->container, $this->config);
$this->assertEquals('/some/dir/foo/bar', $parentDir('foo/bar/baz'));
}
}

View File

@@ -1,16 +0,0 @@
<?php
namespace Tests\ViewFunctions;
use App\ViewFunctions\ParentDir;
use Tests\TestCase;
class ParentDirTest extends TestCase
{
public function test_it_can_get_the_parent_directory_from_a_path(): void
{
$parentDir = new ParentDir($this->container, $this->config);
$this->assertEquals('foo/bar', $parentDir('foo/bar/baz'));
}
}