Skip to content

Commit 0fb33fc

Browse files
fix: Path joining for windows
1 parent f5d4b97 commit 0fb33fc

1 file changed

Lines changed: 27 additions & 9 deletions

File tree

src/LibraryLoader.php

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ class LibraryLoader
3636
*/
3737
public static function getInstance(string $library): FFI
3838
{
39-
if (! isset(self::$instances[$library])) {
39+
if (!isset(self::$instances[$library])) {
4040
self::$instances[$library] = self::createFFIInstance($library);
4141
}
4242

@@ -48,7 +48,7 @@ public static function getInstance(string $library): FFI
4848
*/
4949
private static function createFFIInstance(string $library): FFI
5050
{
51-
if (! isset(self::LIBRARY_CONFIGS[$library])) {
51+
if (!isset(self::LIBRARY_CONFIGS[$library])) {
5252
throw new RuntimeException("Unsupported library: {$library}");
5353
}
5454

@@ -62,7 +62,7 @@ private static function createFFIInstance(string $library): FFI
6262
$detector->getPlatformIdentifier()
6363
);
6464

65-
if (! file_exists($libPath)) {
65+
if (!file_exists($libPath)) {
6666
self::downloadLibraries();
6767
}
6868

@@ -83,12 +83,12 @@ private static function getPlatformDetector(): PlatformDetector
8383

8484
private static function getHeaderPath(string $headerFile): string
8585
{
86-
return dirname(__DIR__)."/include/{$headerFile}";
86+
return self::joinPaths(dirname(__DIR__), 'include', $headerFile);
8787
}
8888

8989
private static function getLibraryPath(string $prefix, string $extension, string $platform): string
9090
{
91-
return dirname(__DIR__)."/lib/{$platform}/{$prefix}.{$extension}";
91+
return self::joinPaths(dirname(__DIR__), 'lib', $platform, "$prefix.$extension");
9292
}
9393

9494
/**
@@ -98,7 +98,6 @@ private static function downloadLibraries(): void
9898
{
9999
$detector = self::getPlatformDetector();
100100
$platform = $detector->getPlatformIdentifier();
101-
$libDir = dirname(__DIR__).'/lib';
102101

103102
$url = sprintf(self::DOWNLOAD_URL, $platform);
104103

@@ -114,7 +113,7 @@ private static function downloadLibraries(): void
114113
'Accept: application/octet-stream',
115114
]);
116115

117-
if (! curl_exec($ch)) {
116+
if (!curl_exec($ch)) {
118117
fclose($fp);
119118
unlink($tempFile);
120119
throw new \RuntimeException(sprintf('Failed to download libraries from %s: %s', $url, curl_error($ch)));
@@ -123,8 +122,8 @@ private static function downloadLibraries(): void
123122
// Extract ZIP file
124123
$zip = new ZipArchive;
125124
if ($zip->open($tempFile) === true) {
126-
$platformLibDir = "{$libDir}/{$platform}";
127-
if (! is_dir($platformLibDir)) {
125+
$platformLibDir = self::joinPaths(dirname(__DIR__), 'lib', $platform);
126+
if (!is_dir($platformLibDir)) {
128127
mkdir($platformLibDir, 0755, true);
129128
}
130129
$zip->extractTo($platformLibDir);
@@ -135,4 +134,23 @@ private static function downloadLibraries(): void
135134
throw new RuntimeException('Failed to downloaded ZIP');
136135
}
137136
}
137+
138+
private static function joinPaths(string ...$args): string
139+
{
140+
$paths = [];
141+
142+
foreach ($args as $key => $path) {
143+
if ($path === '') {
144+
continue;
145+
} elseif ($key === 0) {
146+
$paths[$key] = rtrim($path, DIRECTORY_SEPARATOR);
147+
} elseif ($key === count($paths) - 1) {
148+
$paths[$key] = ltrim($path, DIRECTORY_SEPARATOR);
149+
} else {
150+
$paths[$key] = trim($path, DIRECTORY_SEPARATOR);
151+
}
152+
}
153+
154+
return preg_replace('#(?<!:)//+#', '/', implode(DIRECTORY_SEPARATOR, $paths));
155+
}
138156
}

0 commit comments

Comments
 (0)