Simplified path handling in DirectoryController

This commit is contained in:
Chris Kankiewicz
2020-01-26 01:26:56 -07:00
parent ad67f5b985
commit a1cb213131
2 changed files with 1 additions and 40 deletions

View File

@@ -10,7 +10,6 @@ use Slim\Views\Twig;
use Symfony\Component\Finder\Exception\DirectoryNotFoundException;
use Symfony\Component\Finder\Finder;
use Symfony\Component\Finder\SplFileInfo;
use Tightenco\Collect\Support\Collection;
class DirectoryController
{
@@ -64,29 +63,13 @@ class DirectoryController
'files' => $search ? $files->name(
sprintf('/(?:.*)%s(?:.*)/i', preg_quote($search, '/'))
) : $files->depth(0),
'path' => $this->relativePath($path),
'path' => $path,
'is_root' => $this->isRoot($path),
'readme' => $this->readme($path),
'search' => $search,
]);
}
/**
* Return the relative path given a full path.
*
* @param string $path
*
* @return string
*/
protected function relativePath(string $path): string
{
$realPath = realpath($this->container->get('base_path') . '/' . $path);
return Collection::make(explode('/', $realPath))->diff(
explode('/', $this->container->get('base_path'))
)->filter()->implode('/');
}
/**
* Determine if a provided path is the root path.
*

View File

@@ -4,7 +4,6 @@ namespace Tests\Support;
use App\Support\Helpers;
use PHPUnit\Framework\TestCase;
use RuntimeException;
class HelpersTest extends TestCase
{
@@ -48,25 +47,4 @@ class HelpersTest extends TestCase
$this->assertEquals('Test charlie; please ignore', $env);
}
public function test_it_can_get_a_relative_path_from_one_path_to_another(): void
{
$relativePath = Helpers::realativePath('foo/bar', 'foo/bar/baz/qux');
$this->assertEquals('baz/qux', $relativePath);
}
public function test_it_cat_get_a_relative_path_to_itself(): void
{
$relativePath = Helpers::realativePath('foo/bar/baz', 'foo/bar/baz');
$this->assertEquals('', $relativePath);
}
public function test_it_can_get_a_relative_path_between_two_unrelated_paths(): void
{
$path = $this->expectException(RuntimeException::class);
Helpers::realativePath('foo/bar', 'baz/qux');
}
}