Skip to content
This repository was archived by the owner on Mar 30, 2024. It is now read-only.

Commit 204aba0

Browse files
committed
Socket fix for PHP 8 (fixes #31)
1 parent 824f214 commit 204aba0

File tree

3 files changed

+21
-5
lines changed

3 files changed

+21
-5
lines changed

core/AutocompleteSocket.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ private function openSocket(){
5353
}
5454

5555
public function __destruct(){
56-
if( is_resource($this->socket)){
56+
if( Utilities::isSocketType($this->socket)){
5757
socket_close($this->socket);
5858
}
5959
if( file_exists($this->socketpath)){
@@ -68,7 +68,7 @@ private function closeOtherSocket(){
6868

6969
socket_write($s, $msg, strlen($msg));
7070
}
71-
if(is_resource($s)){
71+
if(Utilities::isSocketType($s)){
7272
socket_close($s);
7373
}
7474

@@ -81,7 +81,7 @@ private function closeOtherSocket(){
8181
private function connectionHandler( $connection ){
8282
$buffer = "";
8383
do{
84-
if( !is_resource($connection) ){
84+
if( !Utilities::isSocketType($connection) ){
8585
return;
8686
}
8787
$buffer = @socket_read($connection, 256, PHP_NORMAL_READ);

core/Utilities.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
*/
55
class Utilities {
66

7-
const VERSION = 'v1.0.13';
7+
const VERSION = 'v1.0.14';
88

99
const DEFAULT_LINE_LENGTH = 125;
1010

@@ -140,6 +140,20 @@ public static function isOnline(string $url) : bool {
140140
return false;
141141
}
142142
}
143+
144+
/**
145+
* Checks if given variable is a socket.
146+
* @param mixed $s the var to check
147+
* @return bool is socket?
148+
*/
149+
public static function isSocketType($s) : bool {
150+
if( class_exists('Socket') && version_compare(PHP_VERSION, '8.0.0', '>=') ){
151+
return $s instanceof Socket;
152+
}
153+
else{
154+
return is_resource($s);
155+
}
156+
}
143157
}
144158

145159
?>

core/platform/socketTester.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
require_once(__DIR__ . '/../Utilities.php');
4+
35
function getSocketPath(){
46
return '/private/tmp/TaskTimeTerminateAutocomplete.sock';
57
//return getenv('USERPROFILE') . '/AppData/Local/Temp/TaskTimeTerminateAutocomplete.sock';
@@ -18,7 +20,7 @@ function getCompletion($prefix){
1820
}
1921
}
2022
}
21-
if(is_resource($s)){
23+
if(Utilities::isSocketType($s)){
2224
socket_close($s);
2325
}
2426
}

0 commit comments

Comments
 (0)