Skip to content

Commit 01c2c33

Browse files
committed
improve os/arch detection
1 parent c4b498e commit 01c2c33

1 file changed

Lines changed: 96 additions & 57 deletions

File tree

php/src/Client.php

Lines changed: 96 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -23,75 +23,114 @@ class Client {
2323
private $events = array();
2424

2525
private function loadLibrary() {
26-
$platform = PHP_OS_FAMILY;
27-
$arch = php_uname('m');
26+
$osFamily = PHP_OS_FAMILY; // "Windows", "Linux" or "Darwin"
27+
$machine = php_uname('m'); // e.g. "AMD64", "x86_64", "i386", "aarch64"
2828
$libDir = __DIR__ . '/../lib';
29-
$libPath = null;
30-
print_r("libDir: $libDir\n");
31-
32-
print_r("Platform: $platform, Arch: $arch\n");
33-
34-
switch (strtolower($platform)) {
35-
case 'windows':
36-
switch ($arch) {
37-
case 'x86_64':
38-
$libPath = $libDir . '/openiap-windows-x64.dll';
39-
break;
40-
case 'x86':
41-
$libPath = $libDir . '/openiap-windows-i686.dll';
42-
break;
43-
case 'aarch64':
44-
$libPath = $libDir . '/openiap-windows-arm64.dll';
45-
break;
46-
default:
47-
throw new Exception("Unsupported architecture on Windows: $arch");
48-
}
49-
break;
50-
case 'darwin':
51-
switch ($arch) {
52-
case 'x86_64':
53-
$libPath = $libDir . '/libopeniap-macos-x64.dylib';
54-
break;
55-
case 'arm64':
56-
$libPath = $libDir . '/libopeniap-macos-arm64.dylib';
57-
break;
58-
default:
59-
throw new Exception("Unsupported architecture on Darwin: $arch");
60-
}
29+
30+
// Map OS → (tag, dynamic extension, prefix)
31+
$osMap = [
32+
'Windows' => ['windows', '.dll', ''], // no prefix on Windows
33+
'Linux' => ['linux', '.so', 'lib'], // libraries named lib*
34+
'Darwin' => ['macos', '.dylib','lib'],
35+
];
36+
if (!isset($osMap[$osFamily])) {
37+
throw new Exception("Unsupported OS: {$osFamily}");
38+
}
39+
list($tagOs, $ext, $prefix) = $osMap[$osFamily];
40+
41+
// Normalize common machine strings
42+
switch (strtolower($machine)) {
43+
case 'amd64':
44+
case 'x86_64':
45+
$tagArch = 'x64';
6146
break;
62-
case 'linux':
63-
switch ($arch) {
64-
case 'x86_64':
65-
$libPath = $libDir . '/libopeniap-linux-x64.so';
66-
break;
67-
case 'aarch64':
68-
$libPath = $libDir . '/libopeniap-linux-arm64.so';
69-
break;
70-
default:
71-
throw new Exception("Unsupported architecture on Linux: $arch");
72-
}
47+
48+
case 'x86':
49+
case 'i386':
50+
$tagArch = 'i686';
7351
break;
74-
case 'freebsd':
75-
switch ($arch) {
76-
case 'x86_64':
77-
$libPath = $libDir . '/libopeniap-freebsd-x64.so';
78-
break;
79-
default:
80-
throw new Exception("Unsupported architecture on FreeBSD: $arch");
81-
}
52+
53+
case 'arm64':
54+
case 'aarch64':
55+
$tagArch = 'arm64';
8256
break;
57+
8358
default:
84-
throw new Exception("Unsupported platform: $platform");
59+
throw new Exception("Unsupported architecture on {$osFamily}: {$machine}");
8560
}
61+
62+
// Build the expected dynamic‐lib filename:
63+
$filename = sprintf(
64+
'%sopeniap-%s-%s%s',
65+
$prefix, // '' on Windows, 'lib' on Linux/macOS
66+
$tagOs, // windows, linux, macos
67+
$tagArch, // x64, i686, arm64
68+
$ext // .dll, .so, .dylib
69+
);
70+
$libPath = $libDir . DIRECTORY_SEPARATOR . $filename;
71+
72+
73+
// switch (strtolower($platform)) {
74+
// case 'windows':
75+
// switch ($arch) {
76+
// case 'x86_64':
77+
// $libPath = $libDir . '/openiap-windows-x64.dll';
78+
// break;
79+
// case 'x86':
80+
// $libPath = $libDir . '/openiap-windows-i686.dll';
81+
// break;
82+
// case 'aarch64':
83+
// $libPath = $libDir . '/openiap-windows-arm64.dll';
84+
// break;
85+
// default:
86+
// throw new Exception("Unsupported architecture on Windows: $arch");
87+
// }
88+
// break;
89+
// case 'darwin':
90+
// switch ($arch) {
91+
// case 'x86_64':
92+
// $libPath = $libDir . '/libopeniap-macos-x64.dylib';
93+
// break;
94+
// case 'arm64':
95+
// $libPath = $libDir . '/libopeniap-macos-arm64.dylib';
96+
// break;
97+
// default:
98+
// throw new Exception("Unsupported architecture on Darwin: $arch");
99+
// }
100+
// break;
101+
// case 'linux':
102+
// switch ($arch) {
103+
// case 'x86_64':
104+
// $libPath = $libDir . '/libopeniap-linux-x64.so';
105+
// break;
106+
// case 'aarch64':
107+
// $libPath = $libDir . '/libopeniap-linux-arm64.so';
108+
// break;
109+
// default:
110+
// throw new Exception("Unsupported architecture on Linux: $arch");
111+
// }
112+
// break;
113+
// case 'freebsd':
114+
// switch ($arch) {
115+
// case 'x86_64':
116+
// $libPath = $libDir . '/libopeniap-freebsd-x64.so';
117+
// break;
118+
// default:
119+
// throw new Exception("Unsupported architecture on FreeBSD: $arch");
120+
// }
121+
// break;
122+
// default:
123+
// throw new Exception("Unsupported platform: $platform");
124+
// }
86125

87126
// If library not found in standard location, check debug location
88127
if (!file_exists($libPath)) {
89128
$libDir = __DIR__ . '/../../target/debug/';
90-
switch (strtolower($platform)) {
91-
case 'windows':
129+
switch (strtolower($osFamily)) {
130+
case 'Windows':
92131
$libPath = $libDir . 'openiap_clib.dll';
93132
break;
94-
case 'darwin':
133+
case 'Darwin':
95134
$libPath = $libDir . 'libopeniap_clib.dylib';
96135
break;
97136
default:

0 commit comments

Comments
 (0)