diff --git a/phpunit/src/Platform/PlatformTest.php b/phpunit/src/Platform/PlatformTest.php index 35e0ec27..589ad117 100644 --- a/phpunit/src/Platform/PlatformTest.php +++ b/phpunit/src/Platform/PlatformTest.php @@ -110,6 +110,15 @@ class PlatformTest extends TestCase $this->assertSame('src/app.php', $linux->removeCommonPrefix('/project', '/project/src/app.php')); } + public function testPlatformPathPrefixRemovalUsesPathSegments(): void + { + $windows = new Windows(); + $linux = new Linux(); + + $this->assertSame('project2\src\app.php', $windows->removeCommonPrefix('C:\project', 'C:/project2/src/app.php')); + $this->assertSame('project2/src/app.php', $linux->removeCommonPrefix('/tmp/project', '/tmp/project2/src/app.php')); + } + /** * 测试 Windows 子系统选项 */ diff --git a/phpunit/src/PreprocessorTest.php b/phpunit/src/PreprocessorTest.php index 2f743a98..3e1f1bc2 100644 --- a/phpunit/src/PreprocessorTest.php +++ b/phpunit/src/PreprocessorTest.php @@ -135,6 +135,15 @@ class PreprocessorTest extends TestCase $this->assertStringStartsWith($this->compiler->getBuildDir(), $result); } + public function testGetCppFileDoesNotTrimSiblingDirectoryByCharacterPrefix(): void + { + $this->setProperty('buildDir', '/tmp/project/build'); + + $result = $this->compiler->getCppFile('/tmp/project2/src/app.php'); + + $this->assertSame('/tmp/project/build/project2/src/app.cc', $result); + } + public function testGetCppFileDotPhpReplaced(): void { $phpFile = '/tmp/test_file.php'; diff --git a/src/Php/Platform/PlatformBase.php b/src/Php/Platform/PlatformBase.php index 272ca47d..fc4e2da6 100644 --- a/src/Php/Platform/PlatformBase.php +++ b/src/Php/Platform/PlatformBase.php @@ -154,18 +154,22 @@ abstract class PlatformBase $long = str_replace('/', '\\', $long); } - $len = min(strlen($short), strlen($long)); + $short = rtrim($short, $separator); + $long = rtrim($long, $separator); + $shortParts = explode($separator, $short); + $longParts = explode($separator, $long); $prefixLen = 0; + $len = min(count($shortParts), count($longParts)); for ($i = 0; $i < $len; $i++) { - if ($short[$i] === $long[$i]) { + if ($shortParts[$i] === $longParts[$i]) { $prefixLen++; } else { break; } } - return ltrim(substr($long, $prefixLen), $separator); + return implode($separator, array_slice($longParts, $prefixLen)); } /**