From 7b9c575ad19c5d675649967afc45e71d83b2d18c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Milan=20Mat=C4=9Bj=C4=8Dek?= Date: Tue, 30 Jul 2024 09:25:02 +0200 Subject: [PATCH] feat(igbinary): check if extension is loaded, and disable auto select driver --- src/Driver/IgBinary.php | 6 ++++++ src/Driver/Php.php | 2 +- src/Driver/SetUp.php | 6 +----- ...DisableAutoInstallTest.php => CheckDefaulDriverTest.php} | 1 - tests/src/{AutoInstallTest.php => IgBinaryForceTest.php} | 1 + 5 files changed, 9 insertions(+), 7 deletions(-) rename tests/src/{DisableAutoInstallTest.php => CheckDefaulDriverTest.php} (95%) rename tests/src/{AutoInstallTest.php => IgBinaryForceTest.php} (88%) diff --git a/src/Driver/IgBinary.php b/src/Driver/IgBinary.php index 8a70385..a24e467 100644 --- a/src/Driver/IgBinary.php +++ b/src/Driver/IgBinary.php @@ -10,6 +10,8 @@ final class IgBinary implements Driver { use StaticClass; + private static ?bool $extension = null; + private const NULL = "\x00"; /** @var array */ @@ -52,6 +54,10 @@ public static function decode(string $value) return $data; } + public static function isIgbinaryLoaded(): bool + { + return self::$extension ??= extension_loaded('igbinary'); + } public static function serializationCheckIgbinary(string $value): bool { diff --git a/src/Driver/Php.php b/src/Driver/Php.php index 647a3eb..9f623ee 100644 --- a/src/Driver/Php.php +++ b/src/Driver/Php.php @@ -29,7 +29,7 @@ public static function decode(string $value) return false; } error_clear_last(); - if (IgBinary::serializationCheckIgbinary($value) === true) { + if (IgBinary::isIgbinaryLoaded() && IgBinary::serializationCheckIgbinary($value)) { return IgBinary::decode($value); } diff --git a/src/Driver/SetUp.php b/src/Driver/SetUp.php index 13627eb..b8cc02a 100644 --- a/src/Driver/SetUp.php +++ b/src/Driver/SetUp.php @@ -27,10 +27,6 @@ public static function decode(string $value) private static function boot(): void { - if (extension_loaded('igbinary')) { - Serialize::setUp(IgBinary::class); - } else { - Serialize::setUp(Php::class); - } + Serialize::setUp(Php::class); } } diff --git a/tests/src/DisableAutoInstallTest.php b/tests/src/CheckDefaulDriverTest.php similarity index 95% rename from tests/src/DisableAutoInstallTest.php rename to tests/src/CheckDefaulDriverTest.php index d147358..1a2a55d 100644 --- a/tests/src/DisableAutoInstallTest.php +++ b/tests/src/CheckDefaulDriverTest.php @@ -9,7 +9,6 @@ use Tester\Assert; require_once __DIR__ . '/../bootstrap.php'; -Serialize::setUp(Php::class); $data = Serialize::encode('foo'); Assert::same('foo', Php::decode($data)); diff --git a/tests/src/AutoInstallTest.php b/tests/src/IgBinaryForceTest.php similarity index 88% rename from tests/src/AutoInstallTest.php rename to tests/src/IgBinaryForceTest.php index 708ec9e..47b65f1 100644 --- a/tests/src/AutoInstallTest.php +++ b/tests/src/IgBinaryForceTest.php @@ -7,6 +7,7 @@ use Tester\Assert; require_once __DIR__ . '/../bootstrap.php'; +Serialize::setUp(IgBinary::class); $data = Serialize::encode('foo'); Assert::same('foo', IgBinary::decode($data));