Skip to content

Commit

Permalink
Add fallback from php serialize to igbinary and vice versa
Browse files Browse the repository at this point in the history
  • Loading branch information
h4kuna committed Aug 10, 2023
1 parent f031655 commit b844121
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 26 deletions.
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
5 changes: 3 additions & 2 deletions src/Driver/IgBinary.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
Expand Down
3 changes: 3 additions & 0 deletions src/Driver/Php.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
}
Expand Down
10 changes: 2 additions & 8 deletions tests/src/AutoInstallTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
14 changes: 14 additions & 0 deletions tests/src/DisableAutoInstallTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php declare(strict_types=1);

namespace h4kuna\Serialize\Tests;

use h4kuna\Serialize\Driver\Php;
use h4kuna\Serialize\Serialize;
use Tester\Assert;

define('SERIALIZATION_FORCE_DISABLE', true);

require_once __DIR__ . '/../bootstrap.php';

$data = Serialize::encode('foo');
Assert::same('foo', Php::decode($data));
17 changes: 8 additions & 9 deletions tests/src/Driver/IgBinaryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace h4kuna\Serialize\Tests\Driver;

use h4kuna\Serialize\Driver\IgBinary;
use h4kuna\Serialize\Driver;
use Tester\Assert;
use Tester\TestCase;

Expand All @@ -13,24 +13,23 @@
*/
class IgBinaryTest extends TestCase
{

/**
* @throws \h4kuna\Serialize\Exception\InvalidStateException
* @param mixed $value
* @dataProvider dataBasicTypes
*/
public function testFailed(): void
public function testEncodeDecode($value): void
{
$value = 'foo';
$data = serialize($value);
Assert::same($value, IgBinary::decode($data));
Assert::same($value, Driver\IgBinary::decode(Driver\IgBinary::encode($value)));
}


/**
* @param mixed $value
* @dataProvider dataBasicTypes
*/
public function testForwardCompatibility($value): void
public function testFallback($value): void
{
Assert::same($value, IgBinary::decode(IgBinary::encode($value)));
Assert::same($value, Driver\IgBinary::decode(Driver\Php::encode($value)));
}


Expand Down
11 changes: 4 additions & 7 deletions tests/src/Driver/PhpTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
use Tester\Assert;
use Tester\TestCase;

define('SERIALIZATION_FORCE_DISABLE', true);

require_once __DIR__ . '/../../bootstrap.php';

/**
Expand All @@ -17,13 +15,12 @@ class PhpTest extends TestCase
{

/**
* @throws \h4kuna\Serialize\Exception\InvalidStateException
* @param mixed $value
* @dataProvider dataBasicTypes
*/
public function testBackCompatibility(): void
public function testFallback($value): void
{
$value = 'foo';
$data = Driver\IgBinary::encode($value);
Assert::same($value, Driver\Php::decode($data));
Assert::same($value, Driver\Php::decode(Driver\IgBinary::encode($value)));
}


Expand Down

0 comments on commit b844121

Please sign in to comment.