diff --git a/README.md b/README.md index a3fc1e0..4d32ebd 100644 --- a/README.md +++ b/README.md @@ -9,3 +9,23 @@ In your project are available two new methods. - h4kuna\Serialize\Serialize::decode() If you enable igbinary extension, then automatic use Driver\IgBinary. Or you can define own implmentation by Driver interface. + + +## Compatibility + +You are using php serialize and you want to use igbinary. You can enable igbinaty on fly and old serialized data will be decoded by old php serialize. + +> Support compatibility, if you have serialized data by php serialize, then you can decode by IgBinary::decode() and vice versa. + +## Enable igbinary + +1. Install igbinary extension + +Works! + +## Disable igbinary + +1. set constant as first things `define('SERIALIZATION_FORCE_DISABLE', true);` before vendor/autoload.php +2. wait if your all data will be decoded +3. uninstall igbinary extension +4. remove constant `SERIALIZATION_FORCE_DISABLE` diff --git a/src/Driver/IgBinary.php b/src/Driver/IgBinary.php index 36f78d1..13262a5 100644 --- a/src/Driver/IgBinary.php +++ b/src/Driver/IgBinary.php @@ -37,9 +37,10 @@ public static function decode(string $value) $data = @igbinary_unserialize($value); if ($data === null) { $error = error_get_last(); + assert($error !== null); error_clear_last(); - if ($error === null || self::serializationCheckIgbinary($value)) { - return null; + if (self::serializationCheckIgbinary($value) === false) { + return Php::decode($value); // fallback to native serialize } throw new InvalidStateException($error['message']); diff --git a/src/Driver/Php.php b/src/Driver/Php.php index c75b28d..fbf5ce4 100644 --- a/src/Driver/Php.php +++ b/src/Driver/Php.php @@ -28,6 +28,9 @@ public static function decode(string $value) return false; } error_clear_last(); + if (IgBinary::serializationCheckIgbinary($value) === true) { + return IgBinary::decode($value); + } throw new InvalidStateException($error['message']); } diff --git a/tests/src/AutoInstallTest.php b/tests/src/AutoInstallTest.php index 945707e..708ec9e 100644 --- a/tests/src/AutoInstallTest.php +++ b/tests/src/AutoInstallTest.php @@ -3,16 +3,10 @@ namespace h4kuna\Serialize\Tests; use h4kuna\Serialize\Driver\IgBinary; -use h4kuna\Serialize\Driver\Php; use h4kuna\Serialize\Serialize; use Tester\Assert; require_once __DIR__ . '/../bootstrap.php'; -if (extension_loaded('igbinary')) { - $data = Serialize::encode('foo'); - Assert::same('foo', IgBinary::decode($data)); -} else { - $data = Serialize::encode('foo'); - Assert::same('foo', Php::decode($data)); -} +$data = Serialize::encode('foo'); +Assert::same('foo', IgBinary::decode($data)); diff --git a/tests/src/DisableAutoInstallTest.php b/tests/src/DisableAutoInstallTest.php new file mode 100644 index 0000000..cd8510d --- /dev/null +++ b/tests/src/DisableAutoInstallTest.php @@ -0,0 +1,14 @@ +