mirror of
https://github.com/flextype/flextype.git
synced 2025-08-07 05:36:54 +02:00
feat(serializers): remove phpcode serializer for now
This commit is contained in:
@@ -1,113 +0,0 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* Flextype (https://flextype.org)
|
||||
* Founded by Sergey Romanenko and maintained by Flextype Community.
|
||||
*/
|
||||
|
||||
namespace Flextype\Serializers;
|
||||
|
||||
use Symfony\Component\VarExporter\VarExporter;
|
||||
|
||||
use RuntimeException;
|
||||
|
||||
use function cache;
|
||||
use function error_get_last;
|
||||
use function error_reporting;
|
||||
use function registry;
|
||||
use function strings;
|
||||
use function var_export;
|
||||
|
||||
use const E_ALL;
|
||||
|
||||
class PhpCode
|
||||
{
|
||||
/**
|
||||
* Returns the PhpCode representation of a value.
|
||||
*
|
||||
* @param mixed $input The PHP value.
|
||||
*
|
||||
* @return string A PhpCode string representing the original PHP value.
|
||||
*/
|
||||
public function encode($input): string
|
||||
{
|
||||
$wrap = registry()->get('flextype.settings.serializers.phpcode.encode.wrap');
|
||||
|
||||
try {
|
||||
if ($wrap) {
|
||||
$data = "<?php\n return " . VarExporter::export($input) . ";\n";
|
||||
} else {
|
||||
$data = VarExporter::export($input);
|
||||
}
|
||||
} catch (Exception $e) {
|
||||
throw new RuntimeException('Encoding PhpCode failed');
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Takes a PhpCode encoded string and converts it into a PHP variable.
|
||||
*
|
||||
* @param string $input A string containing PhpCode.
|
||||
*
|
||||
* @return mixed The PhpCode converted to a PHP value.
|
||||
*/
|
||||
public function decode(string $input)
|
||||
{
|
||||
$cache = registry()->get('flextype.settings.serializers.phpcode.decode.cache');
|
||||
|
||||
$decode = static function (string $input) {
|
||||
$currentErrorLevel = error_reporting();
|
||||
error_reporting(E_ALL);
|
||||
$return = null;
|
||||
$eval = @eval('$return=' . $input . ';');
|
||||
$error = error_get_last();
|
||||
error_reporting($currentErrorLevel);
|
||||
|
||||
if ($eval === false || $error) {
|
||||
$msg = 'Decoding PhpCode failed';
|
||||
|
||||
if ($eval === false) {
|
||||
$lastError = error_get_last();
|
||||
$msg .= ': ' . $lastError['message'];
|
||||
}
|
||||
|
||||
throw new RuntimeException($msg, 0);
|
||||
}
|
||||
|
||||
return $return;
|
||||
};
|
||||
|
||||
if ($cache === true && registry()->get('flextype.settings.cache.enabled') === true) {
|
||||
$key = $this->getCacheID($input);
|
||||
|
||||
if ($dataFromCache = cache()->get($key)) {
|
||||
return $dataFromCache;
|
||||
}
|
||||
|
||||
$data = $decode($input);
|
||||
cache()->set($key, $data);
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
return $decode($input);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get Cache ID for phpcode.
|
||||
*
|
||||
* @param string $input Input.
|
||||
*
|
||||
* @return string Cache ID.
|
||||
*
|
||||
* @access public
|
||||
*/
|
||||
public function getCacheID(string $input): string
|
||||
{
|
||||
return strings('phpcode' . $input)->hash()->toString();
|
||||
}
|
||||
}
|
@@ -54,12 +54,4 @@ class Serializers
|
||||
{
|
||||
return new PhpArray();
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a PhpCode instance.
|
||||
*/
|
||||
public function phpcode(): PhpCode
|
||||
{
|
||||
return new PhpCode();
|
||||
}
|
||||
}
|
||||
|
@@ -459,11 +459,6 @@ serializers:
|
||||
cache: true
|
||||
encode:
|
||||
wrap: true
|
||||
phpcode:
|
||||
decode:
|
||||
cache: true
|
||||
encode:
|
||||
wrap: false
|
||||
|
||||
# Parsers
|
||||
#
|
||||
|
@@ -1,34 +0,0 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
test('encode', function () {
|
||||
$this->assertEquals(31, strings(serializers()->phpcode()->encode(['flextype' => registry()->get("flextype.manifest.version")]))->length());
|
||||
|
||||
registry()->set('flextype.settings.serializers.phpcode.encode.wrap', true);
|
||||
$this->assertEquals(47, strings(serializers()->phpcode()->encode(['flextype' => registry()->get("flextype.manifest.version")]))->length());
|
||||
});
|
||||
|
||||
test('test encode() throws exception RuntimeException', function (): void {
|
||||
serializers()->phpcode()->encode(new Foo());
|
||||
})->throws(RuntimeException::class);
|
||||
|
||||
test('decode', function () {
|
||||
$this->assertEquals('Flextype', serializers()->phpcode()->decode('registry()->get("flextype.manifest.name")'));
|
||||
});
|
||||
|
||||
test('get cache ID', function () {
|
||||
$string = strings(serializers()->phpcode()->encode(['flextype' => registry()->get("flextype.manifest.version")]))->toString();
|
||||
$cache_id = serializers()->phparray()
|
||||
->getCacheID($string);
|
||||
$this->assertEquals(32, strlen($cache_id));
|
||||
$this->assertNotEquals($string, $cache_id);
|
||||
});
|
||||
|
||||
class Foo
|
||||
{
|
||||
public function __serialize()
|
||||
{
|
||||
throw new RuntimeException('Encoding PhpCode failed');
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user