Skip to content

Commit b503974

Browse files
committed
Fix compatibility with php 8.0 (resource -> object in curl)
1 parent 2c27245 commit b503974

File tree

2 files changed

+28
-3
lines changed

2 files changed

+28
-3
lines changed

src/Http/Channel/HttpChannelManager.php

+4-3
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Dogma\Http\HttpRequest;
1616
use Dogma\NonCloneableMixin;
1717
use Dogma\NonSerializableMixin;
18+
use Dogma\Obj;
1819
use Dogma\StrictBehaviorMixin;
1920
use Dogma\Time\Provider\CurrentTimeProvider;
2021
use const CURLM_CALL_MULTI_PERFORM;
@@ -50,7 +51,7 @@ class HttpChannelManager
5051
/** @var HttpChannel[] */
5152
private $channels = [];
5253

53-
/** @var mixed[] ($resourceId => array($channelId, $jobName, $request)) */
54+
/** @var mixed[] (int $resourceId => array($channelId, $jobName, $request)) */
5455
private $resources = [];
5556

5657
/** @var HttpHeaderParser|null */
@@ -154,7 +155,7 @@ public function exec(): int
154155
private function readResults(): void
155156
{
156157
while ($info = curl_multi_info_read($this->handler)) {
157-
$resourceId = (string) $info['handle'];
158+
$resourceId = Obj::objectId($info['handle']);
158159
[$channelId, $name, $request] = $this->resources[$resourceId];
159160
$channel = &$this->channels[$channelId];
160161

@@ -218,7 +219,7 @@ private function selectChannel()
218219
*/
219220
public function jobStarted($resource, HttpChannel $channel, $name, HttpRequest $request): void
220221
{
221-
$this->resources[(string) $resource] = [spl_object_hash($channel), $name, $request];
222+
$this->resources[Obj::objectId($resource)] = [spl_object_hash($channel), $name, $request];
222223
}
223224

224225
public function getHeaderParser(): HttpHeaderParser

src/common/Obj.php

+24
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111

1212
namespace Dogma;
1313

14+
use function function_exists;
15+
use function hexdec;
16+
use function is_object;
1417
use function md5;
1518
use function spl_object_hash;
1619
use function substr;
@@ -27,4 +30,25 @@ public static function dumpHash($object): string
2730
return substr(md5(spl_object_hash($object)), 0, 4);
2831
}
2932

33+
/**
34+
* @param object|resource $object
35+
* @return int
36+
*/
37+
public static function objectId($object): int
38+
{
39+
if (is_object($object)) {
40+
// PHP >= 7.2
41+
if (function_exists('spl_object_id')) {
42+
return spl_object_id($object);
43+
} else {
44+
$hash = spl_object_hash($object);
45+
$hash = substr($hash, 8, 8) . substr($hash, 24, 8);
46+
47+
return (int) hexdec($hash);
48+
}
49+
} else {
50+
return (int) $object;
51+
}
52+
}
53+
3054
}

0 commit comments

Comments
 (0)