Skip to content

Commit

Permalink
- remove file setup.php replaced by SetUp class
Browse files Browse the repository at this point in the history
- check if driver setUp once
- internally keep string instead of Closure
- BC break: method setUp need class-string instead of object
  • Loading branch information
h4kuna committed Jul 27, 2024
1 parent 8ba23c3 commit a32fd62
Show file tree
Hide file tree
Showing 11 changed files with 92 additions and 51 deletions.
14 changes: 12 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ You are using php serialize and you want to use igbinary. You can enable igbinat

> Support compatibility, if you have serialized data by php serialize, then you can decode by IgBinary::decode() and vice versa.
## Enable custom driver
Register you driver after autoload. By default, you can skip this step.
```php
require __DIR__ . '/vendor/autoload.php';
\h4kuna\Serialize\Serialize::setUp(Php::class);
```

## Enable igbinary

1. Install igbinary extension
Expand All @@ -25,7 +32,10 @@ Works!

## Disable igbinary

1. set constant as first things `define('SERIALIZATION_FORCE_DISABLE', true);` before vendor/autoload.php
1. set Php driver after vendor/autoload.php
```php
require __DIR__ . '/vendor/autoload.php';
\h4kuna\Serialize\Serialize::setUp(Php::class);
```
2. wait if your all data will be decoded
3. uninstall igbinary extension
4. remove constant `SERIALIZATION_FORCE_DISABLE`
8 changes: 4 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,7 @@
"autoload": {
"psr-4": {
"h4kuna\\Serialize\\": "src"
},
"files": [
"setup.php"
]
}
},
"autoload-dev": {
"psr-4": {
Expand All @@ -38,5 +35,8 @@
"scripts": {
"phpstan": "vendor/bin/phpstan analyse --level max src tests",
"tests": "vendor/bin/tester -s -j 4 -C --colors 1 tests/src"
},
"require": {
"nette/utils": ">= 3.0"
}
}
9 changes: 0 additions & 9 deletions setup.php

This file was deleted.

11 changes: 5 additions & 6 deletions src/Base64.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,20 @@
namespace h4kuna\Serialize;

use h4kuna\Serialize\Exception\InvalidStateException;
use Nette\StaticClass;

final class Base64
{
/**
* @param mixed $value
*/
use StaticClass;

/** @param mixed $value */
public static function encode($value): string
{
return base64_encode(Serialize::encode($value));
}


/**
* @return mixed
*/
/** @return mixed */
public static function decode(string $value, bool $strict = true)
{
$base = base64_decode($value, $strict);
Expand Down
8 changes: 2 additions & 6 deletions src/Driver.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,11 @@

interface Driver
{
/**
* @param mixed $value
*/
/** @param mixed $value */
static function encode($value): string;


/**
* @return mixed
*/
/** @return mixed */
static function decode(string $value);

}
7 changes: 4 additions & 3 deletions src/Driver/IgBinary.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@

use h4kuna\Serialize\Driver;
use h4kuna\Serialize\Exception\InvalidStateException;
use Nette\StaticClass;

final class IgBinary implements Driver
{
use StaticClass;

private const NULL = "\x00";

/**
* @var array<string>
*/
/** @var array<string> */
private static array $haystack = ["\x00\x00\x00\x01", "\x00\x00\x00\x02"];


Expand Down
7 changes: 4 additions & 3 deletions src/Driver/Php.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@

use h4kuna\Serialize\Driver;
use h4kuna\Serialize\Exception\InvalidStateException;
use Nette\StaticClass;

final class Php implements Driver
{
/**
* @var array{allowed_classes?: bool, max_depth?: int}
*/
use StaticClass;

/** @var array{allowed_classes?: bool, max_depth?: int} */
public static array $options = [];


Expand Down
36 changes: 36 additions & 0 deletions src/Driver/SetUp.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php declare(strict_types=1);

namespace h4kuna\Serialize\Driver;

use h4kuna\Serialize\Driver;
use h4kuna\Serialize\Serialize;
use Nette\StaticClass;

/**
* @internal
*/
final class SetUp implements Driver
{
use StaticClass;

public static function encode($value): string
{
self::boot();
return Serialize::encode($value);
}

public static function decode(string $value)
{
self::boot();
return Serialize::decode($value);
}

private static function boot(): void
{
if (extension_loaded('igbinary')) {
Serialize::setUp(IgBinary::class);
} else {
Serialize::setUp(Php::class);
}
}
}
4 changes: 3 additions & 1 deletion src/Exception/InvalidStateException.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

namespace h4kuna\Serialize\Exception;

final class InvalidStateException extends \RuntimeException
use RuntimeException;

final class InvalidStateException extends RuntimeException
{

}
32 changes: 17 additions & 15 deletions src/Serialize.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,40 @@

namespace h4kuna\Serialize;

use Closure;
use h4kuna\Serialize\Driver\SetUp;
use h4kuna\Serialize\Exception\InvalidStateException;
use Nette\StaticClass;

final class Serialize implements Driver
{
use StaticClass;

/**
* @var Closure
*/
private static $encode;
/** @var class-string<Driver> */
private static string $driver = SetUp::class;

/**
* @var Closure
* @param class-string<Driver> $driver
*/
private static $decode;


public static function setUp(Driver $driver): void
public static function setUp(string $driver): void
{
self::$encode = static fn ($value) => $driver::encode($value);
self::$decode = static fn (string $value) => $driver::decode($value);
if (self::$driver !== SetUp::class) {
if (self::$driver === $driver) {
return;
}
throw new InvalidStateException(sprintf('Driver was already set up "%s" and you want "%s".', self::$driver, $driver));
}
self::$driver = $driver;
}


public static function encode($value): string
{
return (self::$encode)($value);
return (self::$driver)::encode($value);
}


public static function decode(string $value)
{
return (self::$decode)($value);
return (self::$driver)::decode($value);
}

}
7 changes: 5 additions & 2 deletions tests/src/DisableAutoInstallTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@

namespace h4kuna\Serialize\Tests;

use h4kuna\Serialize\Driver\IgBinary;
use h4kuna\Serialize\Driver\Php;
use h4kuna\Serialize\Exception\InvalidStateException;
use h4kuna\Serialize\Serialize;
use Tester\Assert;

define('SERIALIZATION_FORCE_DISABLE', true);

require_once __DIR__ . '/../bootstrap.php';
Serialize::setUp(Php::class);

$data = Serialize::encode('foo');
Assert::same('foo', Php::decode($data));
Serialize::setUp(Php::class); // does not throw exception
Assert::exception(fn () => Serialize::setUp(IgBinary::class), InvalidStateException::class, 'Driver was already set up "h4kuna\Serialize\Driver\Php" and you want "h4kuna\Serialize\Driver\IgBinary".');

0 comments on commit a32fd62

Please sign in to comment.