Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -459,3 +459,4 @@ compiler/cpp/tests/bin/
compiler/cpp/tests/*.a
compiler/cpp/tests/build/
compiler/cpp/build/
/lib/php/coverage.xml
13 changes: 11 additions & 2 deletions lib/php/lib/Serializer/TBinarySerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
*/
class TBinarySerializer
{
private static ?bool $hasAcceleratedProtocol = null;

// NOTE(rmarin): Because thrift_protocol_write_binary
// adds a begin message prefix, you cannot specify
// a transport in which to serialize an object. It has to
Expand All @@ -45,7 +47,7 @@ public static function serialize($object)
{
$transport = new TMemoryBuffer();
$protocol = new TBinaryProtocolAccelerated($transport);
if (function_exists('thrift_protocol_write_binary')) {
if (self::hasAcceleratedProtocol()) {
thrift_protocol_write_binary(
$protocol,
$object->getName(),
Expand All @@ -68,7 +70,7 @@ public static function deserialize($string_object, $class_name, $buffer_size = 8
{
$transport = new TMemoryBuffer();
$protocol = new TBinaryProtocolAccelerated($transport);
if (function_exists('thrift_protocol_read_binary')) {
if (self::hasAcceleratedProtocol()) {
// NOTE (t.heintz) TBinaryProtocolAccelerated internally wraps our TMemoryBuffer in a
// TBufferedTransport, so we have to retrieve it again or risk losing data when writing
// less than 512 bytes to the transport (see the comment there as well).
Expand All @@ -87,4 +89,11 @@ public static function deserialize($string_object, $class_name, $buffer_size = 8
return $object;
}
}

private static function hasAcceleratedProtocol(): bool
{
return self::$hasAcceleratedProtocol ??=
function_exists('thrift_protocol_write_binary')
&& function_exists('thrift_protocol_read_binary');
}
}
10 changes: 9 additions & 1 deletion lib/php/lib/Transport/TSocket.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ class TSocket extends TTransport
*/
public const DEFAULT_DEBUG_HANDLER = 'error_log';

private static ?bool $hasSocketsExtension = null;

/**
* Handle to PHP socket
*
Expand Down Expand Up @@ -191,7 +193,7 @@ public function open(): void
throw new TException($error);
}

if (function_exists('socket_import_stream') && function_exists('socket_set_option')) {
if (self::hasSocketsExtension()) {
// warnings silenced due to bug https://bugs.php.net/bug.php?id=70939
$socket = socket_import_stream($this->handle);
if ($socket !== false) {
Expand All @@ -200,6 +202,12 @@ public function open(): void
}
}

private static function hasSocketsExtension(): bool
{
return self::$hasSocketsExtension ??=
function_exists('socket_import_stream') && function_exists('socket_set_option');
}

public function close(): void
{
@fclose($this->handle);
Expand Down
16 changes: 8 additions & 8 deletions lib/php/lib/Transport/TSocketPool.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,7 @@ class TSocketPool extends TSocket
*/
private bool $alwaysTryLast = true;

/**
* Use apcu cache
*/
private bool $useApcuCache;
private static ?bool $hasApcuCache = null;

/**
* Socket pool constructor
Expand Down Expand Up @@ -100,8 +97,6 @@ public function __construct(
foreach ($hosts as $key => $host) {
$this->addServer($host, $ports[$key]);
}

$this->useApcuCache = function_exists('apcu_fetch');
}

/**
Expand Down Expand Up @@ -276,11 +271,16 @@ public function open(): void
*/
private function apcuFetch($key, &$success = null)
{
return $this->useApcuCache ? apcu_fetch($key, $success) : false;
return self::hasApcuCache() ? apcu_fetch($key, $success) : false;
}

private function apcuStore($key, $var, $ttl = 0)
{
return $this->useApcuCache ? apcu_store($key, $var, $ttl) : false;
return self::hasApcuCache() ? apcu_store($key, $var, $ttl) : false;
}

private static function hasApcuCache(): bool
{
return self::$hasApcuCache ??= function_exists('apcu_fetch');
}
}
2 changes: 2 additions & 0 deletions lib/php/phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ parameters:
- lib
bootstrapFiles:
- test/bootstrap.php
scanFiles:
- src/ext/thrift_protocol/php_thrift_protocol.stub.php
treatPhpDocTypesAsCertain: false
excludePaths:
- test/Resources/packages/*
Expand Down
13 changes: 13 additions & 0 deletions lib/php/test/Unit/Lib/Serializer/TBinarySerializerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,21 @@
use PHPUnit\Framework\TestCase;
use PHPUnit\Framework\Attributes\DataProvider;
use Test\Thrift\Unit\Lib\Fixture\TestSerializerStruct;
use Test\Thrift\Unit\Lib\ReflectionHelper;
use Thrift\Serializer\TBinarySerializer;

class TBinarySerializerTest extends TestCase
{
use PHPMock;
use ReflectionHelper;

protected function setUp(): void
{
self::defineFunctionMock('Thrift\Serializer', 'function_exists');

$this->getAccessibleProperty(TBinarySerializer::class, 'hasAcceleratedProtocol')
->setValue(null, null);
}

#[DataProvider('serializeDeserializeDataProvider')]
public function testSerializeAndDeserialize($stringVal, $intVal)
Expand Down Expand Up @@ -136,6 +146,9 @@ public function testDeserializeWithAcceleratedExtension()

$serialized = TBinarySerializer::serialize($object);

$this->getAccessibleProperty(TBinarySerializer::class, 'hasAcceleratedProtocol')
->setValue(null, null);

$funcExists = $this->getFunctionMock('Thrift\Serializer', 'function_exists');
$funcExists->expects($this->atLeastOnce())
->willReturn(true);
Expand Down
6 changes: 6 additions & 0 deletions lib/php/test/Unit/Lib/Transport/TSocketPoolTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
use PHPUnit\Framework\Attributes\DataProvider;
use Test\Thrift\Unit\Lib\ReflectionHelper;
use Thrift\Exception\TException;
use Thrift\Transport\TSocket;
use Thrift\Transport\TSocketPool;

class TSocketPoolTest extends TestCase
Expand All @@ -41,6 +42,11 @@ protected function setUp(): void
{
#need to be defined before the TSocketPool class definition
self::defineFunctionMock('Thrift\Transport', 'function_exists');

$this->getAccessibleProperty(TSocketPool::class, 'hasApcuCache')
->setValue(null, null);
$this->getAccessibleProperty(TSocket::class, 'hasSocketsExtension')
->setValue(null, null);
}

#[DataProvider('constructDataProvider')]
Expand Down
6 changes: 6 additions & 0 deletions lib/php/test/Unit/Lib/Transport/TSocketTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ class TSocketTest extends TestCase
use PHPMock;
use ReflectionHelper;

protected function setUp(): void
{
$this->getAccessibleProperty(TSocket::class, 'hasSocketsExtension')
->setValue(null, null);
}

#[DataProvider('openExceptionDataProvider')]
public function testOpenException(
$host,
Expand Down
Loading