From f86e1321b023076ea03534787a0c8e92e75f64ab Mon Sep 17 00:00:00 2001 From: Mior Muhammad Zaki Date: Thu, 12 Dec 2024 10:32:45 +0800 Subject: [PATCH] wip Signed-off-by: Mior Muhammad Zaki --- src/functions.php | 11 +++-- .../TransformRealpathToRelativeTest.php | 42 +++++++++++++++++++ tests/Helpers/TransformRelativePathTest.php | 5 +-- 3 files changed, 48 insertions(+), 10 deletions(-) create mode 100644 tests/Helpers/TransformRealpathToRelativeTest.php diff --git a/src/functions.php b/src/functions.php index 4d7a11700..e966a59aa 100644 --- a/src/functions.php +++ b/src/functions.php @@ -231,21 +231,20 @@ function refresh_router_lookups(Router $router): void function transform_realpath_to_relative(string $path, ?string $workingPath = null, string $prefix = ''): string { if (! \is_null($workingPath)) { - return str_replace(rtrim($workingPath, DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR, $prefix, $path); + return str_replace(rtrim($workingPath, DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR, "{$prefix}/", $path); } $laravelPath = base_path(); - $packagePath = package_path(); $workbenchPath = workbench_path(); + $packagePath = package_path(); - $locate = static fn ($path) => match (true) { + return match (true) { str_starts_with($path, $laravelPath) => str_replace("{$laravelPath}/", '@laravel/', $path), str_starts_with($path, $workbenchPath) => str_replace("{$workbenchPath}/", '@workbench/', $path), str_starts_with($path, $packagePath) => str_replace("{$packagePath}/", './', $path), - default => $prefix.$path, + ! empty($prefix) => implode('/', [$prefix, ltrim($path, '/')]), + default => $path, }; - - return $locate($path); } /** diff --git a/tests/Helpers/TransformRealpathToRelativeTest.php b/tests/Helpers/TransformRealpathToRelativeTest.php new file mode 100644 index 000000000..893726588 --- /dev/null +++ b/tests/Helpers/TransformRealpathToRelativeTest.php @@ -0,0 +1,42 @@ +assertSame('Testbench.php', transform_realpath_to_relative('Testbench.php')); + + $this->assertSame( + './src/TestCase.php', + transform_realpath_to_relative(package_path('src', 'TestCase.php')) + ); + + $this->assertSame( + '@laravel/composer.json', + transform_realpath_to_relative(default_skeleton_path('composer.json')) + ); + + $this->assertSame( + '@workbench/app/Providers/WorkbenchServiceProvider.php', + transform_realpath_to_relative(package_path('workbench', 'app', 'Providers', 'WorkbenchServiceProvider.php')) + ); + } + + /** @test */ + public function it_can_use_transform_realpath_to_relative_using_custom_working_path() + { + $this->assertSame( + '@tests/Helpers/TransformRealpathToRelativeTest.php', + transform_realpath_to_relative(__FILE__, package_path('tests'), '@tests') + ); + } +} diff --git a/tests/Helpers/TransformRelativePathTest.php b/tests/Helpers/TransformRelativePathTest.php index d014d0563..afae4508e 100644 --- a/tests/Helpers/TransformRelativePathTest.php +++ b/tests/Helpers/TransformRelativePathTest.php @@ -11,9 +11,6 @@ class TransformRelativePathTest extends TestCase /** @test */ public function it_can_use_transform_relative_path() { - $this->assertSame( - realpath(__DIR__.DIRECTORY_SEPARATOR.'TransformRelativePathTest.php'), - transform_relative_path('./TransformRelativePathTest.php', realpath(__DIR__)) - ); + $this->assertSame(__FILE__, transform_relative_path('./TransformRelativePathTest.php', __DIR__)); } }