@@ -204,6 +204,29 @@ private static function libraryName(string $name, int $abi): string
204
204
// most *nix
205
205
return "$ name.so. $ abi " ;
206
206
}
207
+
208
+ return null ;
209
+ }
210
+
211
+ private static function libraryLoad (
212
+ array $ libraryPaths ,
213
+ string $ libraryName ,
214
+ string $ interface
215
+ ): \FFI {
216
+ Utils::debugLog ("trying to open " , ["libraryName " => $ libraryName ]);
217
+ foreach ($ libraryPaths as $ path ) {
218
+ Utils::debugLog ("trying path " , ["path " => $ path ]);
219
+ try {
220
+ $ library = \FFI ::cdef ($ interface , $ path . $ libraryName );
221
+ Utils::debugLog ("success " , []);
222
+ return $ library ;
223
+ } catch (\FFI \Exception $ e ) {
224
+ Utils::debugLog ("init " , [
225
+ "msg " => "library load failed " ,
226
+ "exception " => $ e ->getMessage ()
227
+ ]);
228
+ }
229
+ }
207
230
}
208
231
209
232
private static function init (): void
@@ -251,30 +274,15 @@ private static function init(): void
251
274
$ libraryPaths [] = "/opt/homebrew/lib/ " ; // Homebrew on Apple Silicon
252
275
}
253
276
254
- // attempt to open libraries using the system library search method
255
- // (no prefix) and a couple of fallback paths, if any
256
- $ vips = null ;
257
- foreach ($ libraryPaths as $ path ) {
258
- Utils::debugLog ("init " , ["path " => $ path ]);
259
-
260
- try {
261
- $ vips = \FFI ::cdef (<<<EOS
262
- int vips_init (const char *argv0);
263
- const char *vips_error_buffer (void);
264
- int vips_version(int flag);
265
- EOS , $ path . $ vips_libname );
266
- break ;
267
- } catch (\FFI \Exception $ e ) {
268
- Utils::debugLog ("init " , [
269
- "msg " => "library load failed " ,
270
- "exception " => $ e ->getMessage ()
271
- ]);
272
- }
273
- }
277
+ $ vips = self ::libraryLoad ($ libraryPaths , $ vips_libname , <<<EOS
278
+ int vips_init (const char *argv0);
279
+ const char *vips_error_buffer (void);
280
+ int vips_version(int flag);
281
+ EOS );
274
282
275
283
if ($ vips === null ) {
284
+ // drop the "" (system path) member
276
285
array_shift ($ libraryPaths );
277
-
278
286
$ msg = "Unable to open library ' $ vips_libname' " ;
279
287
if (!empty ($ libraryPaths )) {
280
288
$ msg .= " in any of [' " . implode ("', ' " , $ libraryPaths ) . "'] " ;
@@ -579,7 +587,7 @@ private static function init(): void
579
587
unsigned int offset;
580
588
} VipsArgumentClass;
581
589
582
- int vips_object_get_argument (VipsObject* object, const char *name,
590
+ int vips_object_get_argument (VipsObject* object, const char *name,
583
591
GParamSpec** pspec,
584
592
VipsArgumentClass** argument_class,
585
593
VipsArgumentInstance** argument_instance);
@@ -758,9 +766,21 @@ private static function init(): void
758
766
}
759
767
760
768
Utils::debugLog ("init " , ["binding ... " ]);
761
- self ::$ glib = \FFI ::cdef ($ glib_decls , $ glib_libname );
762
- self ::$ gobject = \FFI ::cdef ($ gobject_decls , $ gobject_libname );
763
- self ::$ vips = \FFI ::cdef ($ vips_decls , $ vips_libname );
769
+ self ::$ glib = self ::libraryLoad (
770
+ $ libraryPaths ,
771
+ $ glib_libname ,
772
+ $ glib_decls
773
+ );
774
+ self ::$ gobject = self ::libraryLoad (
775
+ $ libraryPaths ,
776
+ $ gobject_libname ,
777
+ $ gobject_decls
778
+ );
779
+ self ::$ vips = self ::libraryLoad (
780
+ $ libraryPaths ,
781
+ $ vips_libname ,
782
+ $ vips_decls
783
+ );
764
784
765
785
# Useful for debugging
766
786
# self::$vips->vips_leak_set(1);
0 commit comments