Skip to content

Commit 819bcb4

Browse files
committed
FFI instantiation tweaks
1 parent 24b6193 commit 819bcb4

File tree

2 files changed

+11
-11
lines changed

2 files changed

+11
-11
lines changed

Diff for: Makefile

+4-1
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,16 @@ stan: image
1919
psalm: image
2020
${RUN_PHP_CMD} ./vendor/bin/psalm --show-info=true
2121

22+
test-ffi-scope: image
23+
${RUN_PHP_CMD} -d ffi.preload=./src/pcap.h -d memory_limit=-1 ./vendor/bin/phpunit --debug
24+
2225
buster:
2326
docker build -t ${REPOSITORY}:buster -f Dockerfile.buster .
2427

2528
buster-test: buster
2629
${RUN_CMD}:buster php -d memory_limit=-1 -d memory_limit=-1 ./vendor/bin/phpunit --debug
2730

28-
ci: stan psalm test buster-test
31+
ci: stan psalm test-ffi-scope test buster-test
2932

3033
clean:
3134
rm -rf `cat .gitignore`

Diff for: src/PcapFFI.php

+7-10
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
use FFI;
88
use FFI\CData;
9-
use FFI\Exception;
9+
use FFI\Exception as FFIException;
1010

1111
/**
1212
* FFI/libpcap class
@@ -15,13 +15,13 @@ class PcapFFI
1515
{
1616
public const LIBPCAP_NAME = 'libpcap.so.1';
1717

18-
static private $ffi = NULL;
18+
static private FFI $ffi;
1919

2020
private ?string $error = null;
2121

2222
public function __construct()
2323
{
24-
if (self::$ffi) {
24+
if (isset(self::$ffi)) {
2525
return;
2626
}
2727

@@ -31,21 +31,18 @@ public function __construct()
3131
$code = file_get_contents(__DIR__ . '/pcap.h');
3232

3333
if ($code === false) {
34-
throw new Exception('Cannot load pcap C definitions');
34+
throw new FFIException('Cannot load pcap C definitions');
3535
}
3636
self::$ffi = FFI::cdef($code, $lib);
3737
} else {
3838
try {
3939
// Try preload
40-
self::$ffi = \FFI::scope("_RTCKIT_PCAP_FFI_");
41-
} catch (\FFI\Exception $e) {
40+
self::$ffi = FFI::scope("_RTCKIT_PCAP_FFI_");
41+
} catch (FFIException $e) {
4242
// Try direct load
43-
self::$ffi = \FFI::load(__DIR__ . '/pcap.h');
43+
self::$ffi = FFI::load(__DIR__ . '/pcap.h');
4444
}
4545
}
46-
if (!self::$ffi) {
47-
throw new \RuntimeException("FFI parse fails");
48-
}
4946
}
5047

5148
public function lib_version(): string

0 commit comments

Comments
 (0)