1
0
mirror of https://github.com/flextype/flextype.git synced 2025-08-07 05:36:54 +02:00

feat(serializers): use Symfony VarExporter for secure and fast var exporting.

Affected:
- PhpArray
- PhpCode

New setting added:
- wrap (boolean) - To set php return wrapper for result value.
This commit is contained in:
Awilum
2021-09-27 17:44:18 +03:00
parent 31c859859e
commit e867fc113d
4 changed files with 25 additions and 4 deletions

View File

@@ -59,7 +59,8 @@
"siriusphp/upload": "^3.0.1",
"filp/whoops": "^2.14.3",
"symfony/console": "^5.3.7"
"symfony/console": "^5.3.7",
"symfony/var-exporter": "^5.3"
},
"suggest": {
"ext-zend-opcache": "Recommended for better performance",
@@ -84,4 +85,4 @@
"victorjonsson/markdowndocs": "^1.3.8",
"kint-php/kint": "^3.3"
}
}
}

View File

@@ -9,6 +9,8 @@ declare(strict_types=1);
namespace Flextype\Serializers;
use Symfony\Component\VarExporter\VarExporter;
use RuntimeException;
use function cache;
@@ -27,8 +29,14 @@ class PhpArray
*/
public function encode($input): string
{
$wrap = registry()->get('flextype.settings.serializers.phparray.encode.wrap');
try {
$data = "<?php\n return " . var_export($input, true) . ";\n";
if ($wrap) {
$data = "<?php\n return " . VarExporter::export($input) . ";\n";
} else {
$data = VarExporter::export($input);
}
} catch (Exception $e) {
throw new RuntimeException('Encoding PhpArray failed');
}

View File

@@ -9,6 +9,8 @@ declare(strict_types=1);
namespace Flextype\Serializers;
use Symfony\Component\VarExporter\VarExporter;
use RuntimeException;
use function cache;
@@ -31,8 +33,14 @@ class PhpCode
*/
public function encode($input): string
{
$wrap = registry()->get('flextype.settings.serializers.phpcode.encode.wrap');
try {
$data = var_export($input, true);
if ($wrap) {
$data = "<?php\n return " . VarExporter::export($input) . ";\n";
} else {
$data = VarExporter::export($input);
}
} catch (Exception $e) {
throw new RuntimeException('Encoding PhpCode failed');
}

View File

@@ -456,9 +456,13 @@ serializers:
phparray:
decode:
cache: true
encode:
wrap: true
phpcode:
decode:
cache: true
encode:
wrap: false
# Parsers
#