Skip to content

Commit deaf4e1

Browse files
committed
Incorporate review comment
1 parent 3ae9a3b commit deaf4e1

File tree

1 file changed

+17
-11
lines changed

1 file changed

+17
-11
lines changed

src/FFI.php

+17-11
Original file line numberDiff line numberDiff line change
@@ -181,8 +181,9 @@ public static function atLeast(int $x, int $y, int $z = 0): bool
181181
/**
182182
* Adds a directory to the search path for shared libraries.
183183
*
184-
* This method has no effect if FFI handles are already initialized or
185-
* if the specified path is already included.
184+
* This method has no effect if FFI handles are already initialized,
185+
* if the specified path is non-existent, or if the path is already
186+
* included.
186187
*
187188
* @param string $path The path of the library.
188189
* @return bool `true` if the path was added; otherwise, `false`.
@@ -194,12 +195,20 @@ public static function addLibraryPath(string $path): bool
194195
return false;
195196
}
196197

197-
if (!in_array($path, self::$libraryPaths)) {
198-
self::$libraryPaths[] = $path;
199-
return true;
198+
$path = realpath($path);
199+
if ($path === false) {
200+
return false;
201+
}
202+
203+
$path .= DIRECTORY_SEPARATOR;
204+
205+
if (in_array($path, self::$libraryPaths)) {
206+
return false;
200207
}
201208

202-
return false;
209+
self::$libraryPaths[] = $path;
210+
211+
return true;
203212
}
204213

205214
/**
@@ -248,9 +257,6 @@ private static function libraryLoad(
248257
foreach (self::$libraryPaths as $path) {
249258
Utils::debugLog("trying path", ["path" => $path]);
250259
try {
251-
if ($path !== '') {
252-
$path .= '/';
253-
}
254260
$library = \FFI::cdef($interface, $path . $libraryName);
255261
Utils::debugLog("success", []);
256262
return $library;
@@ -289,9 +295,9 @@ private static function init(): void
289295

290296
if (PHP_OS_FAMILY === "OSX" || PHP_OS_FAMILY === "Darwin") {
291297
// Homebrew on Apple Silicon
292-
self::$libraryPaths[] = "/opt/homebrew/lib";
298+
self::addLibraryPath("/opt/homebrew/lib");
293299
// See https://github.com/Homebrew/brew/issues/13481#issuecomment-1207203483
294-
self::$libraryPaths[] = "/usr/local/lib";
300+
self::addLibraryPath("/usr/local/lib");
295301
}
296302

297303
$vips = self::libraryLoad($vips_libname, <<<'CPP'

0 commit comments

Comments
 (0)