From a7df4c04ccf80f8f02171a2c45d7c98d18fb8b82 Mon Sep 17 00:00:00 2001 From: spiralbot Date: Fri, 15 Dec 2023 04:21:35 +0000 Subject: [PATCH] Merge pull request #1030 from spiral/bugfix/normalize-path --- src/Files.php | 8 +++++++- tests/ConversionTest.php | 39 ++++++++++++++++++++++++--------------- 2 files changed, 31 insertions(+), 16 deletions(-) diff --git a/src/Files.php b/src/Files.php index dd6a443..9ecc8b9 100644 --- a/src/Files.php +++ b/src/Files.php @@ -313,10 +313,16 @@ public function tempFilename(string $extension = '', string $location = null): s public function normalizePath(string $path, bool $asDirectory = false): string { + $isUnc = \str_starts_with($path, '\\\\') || \str_starts_with($path, '//'); + if ($isUnc) { + $leadingSlashes = \substr($path, 0, 2); + $path = \substr($path, 2); + } + $path = \str_replace(['//', '\\'], '/', $path); //Potentially open links and ../ type directories? - return \rtrim($path, '/') . ($asDirectory ? '/' : ''); + return ($isUnc ? $leadingSlashes : '') . \rtrim($path, '/') . ($asDirectory ? '/' : ''); } /** diff --git a/tests/ConversionTest.php b/tests/ConversionTest.php index 1296adf..c576ff2 100644 --- a/tests/ConversionTest.php +++ b/tests/ConversionTest.php @@ -6,51 +6,60 @@ use Spiral\Files\Files; -class ConversionTest extends TestCase +final class ConversionTest extends TestCase { - public function testNormalizeFilePath(): void + private Files $files; + + protected function setUp(): void { - $files = new Files(); + $this->files = new Files(); + } - $this->assertSame('/abc/file.name', $files->normalizePath('/abc\\file.name')); - $this->assertSame('/abc/file.name', $files->normalizePath('\\abc//file.name')); + public function testNormalizeFilePath(): void + { + $this->assertSame('/abc/file.name', $this->files->normalizePath('/abc\\file.name')); + $this->assertSame('/abc/file.name', $this->files->normalizePath('\\abc//file.name')); } public function testNormalizeDirectoryPath(): void { - $files = new Files(); + $this->assertSame('/abc/dir/', $this->files->normalizePath('\\abc/dir', true)); + $this->assertSame('/abc/dir/', $this->files->normalizePath('\\abc//dir', true)); + } - $this->assertSame('/abc/dir/', $files->normalizePath('\\abc/dir', true)); - $this->assertSame('/abc/dir/', $files->normalizePath('\\abc//dir', true)); + public function testNormalizeUniversalNamingConventionPath(): void + { + $this->assertSame('//host/path/resource', $this->files->normalizePath('//host/path/resource')); + $this->assertSame('//host/path/resource', $this->files->normalizePath('//host/path//resource')); + $this->assertSame('\\\\host/path/resource', $this->files->normalizePath('\\\\host/path/resource')); + $this->assertSame('\\\\host/path/resource', $this->files->normalizePath('\\\\host/path//resource')); } public function testRelativePath(): void { - $files = new Files(); - $this->assertSame( 'some-filename.txt', - $files->relativePath('/abc/some-filename.txt', '/abc') + $this->files->relativePath('/abc/some-filename.txt', '/abc') ); $this->assertSame( '../some-filename.txt', - $files->relativePath('/abc/../some-filename.txt', '/abc') + $this->files->relativePath('/abc/../some-filename.txt', '/abc') ); $this->assertSame( '../../some-filename.txt', - $files->relativePath('/abc/../../some-filename.txt', '/abc') + $this->files->relativePath('/abc/../../some-filename.txt', '/abc') ); $this->assertSame( './some-filename.txt', - $files->relativePath('/abc/some-filename.txt', '/abc/..') + $this->files->relativePath('/abc/some-filename.txt', '/abc/..') ); $this->assertSame( '../some-filename.txt', - $files->relativePath('/abc/some-filename.txt', '/abc/../..') + $this->files->relativePath('/abc/some-filename.txt', '/abc/../..') ); } }