forked from symfony/symfony
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
symfony#39102: avoid storing huge (de)serializing objects/strings in …
…memory by replacing too large source-objects/results as replacement (LargeContent).
- Loading branch information
Showing
11 changed files
with
342 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Serializer\Debug; | ||
|
||
final class LargeContent | ||
{ | ||
public const LIMIT_BYTES = 1024 * 1024; | ||
public $message; | ||
|
||
public function __construct() | ||
{ | ||
$this->message = sprintf( | ||
"The content of the serialized/deserialized data exceeded %d kB.", | ||
self::LIMIT_BYTES / 1024 | ||
); | ||
} | ||
|
||
public function __toString(): string | ||
{ | ||
return $this->message; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
95 changes: 95 additions & 0 deletions
95
src/Symfony/Component/Serializer/Debug/SerializerActionFactory.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
<?php | ||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Serializer\Debug; | ||
|
||
use Symfony\Component\Serializer\Debug\Normalizer\Denormalization; | ||
use Symfony\Component\Serializer\Debug\Normalizer\Normalization; | ||
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface; | ||
use Symfony\Component\Serializer\Normalizer\NormalizerInterface; | ||
|
||
final class SerializerActionFactory implements SerializerActionFactoryInterface | ||
{ | ||
public function createDenormalization(DenormalizerInterface $denormalizer, $data, $result, string $type, string $format, array $context = []): Denormalization | ||
{ | ||
return new Denormalization( | ||
$denormalizer, | ||
$this->sanitize($data), | ||
$this->sanitize($result), | ||
$type, | ||
$format, | ||
$context | ||
); | ||
} | ||
|
||
public function createNormalization(NormalizerInterface $normalizer, $object, $result, string $format, array $context = []): Normalization | ||
{ | ||
return new Normalization( | ||
$normalizer, | ||
$this->sanitize($object), | ||
$this->sanitize($result), | ||
$format, | ||
$context | ||
); | ||
} | ||
|
||
public function createSerialization($data, string $result, string $format, array $context = []): Serialization | ||
{ | ||
return new Serialization( | ||
$this->sanitize($data), | ||
$this->sanitize($result), | ||
$format, | ||
$context | ||
); | ||
} | ||
|
||
public function createDeserialization(string $data, $result, string $type, string $format, array $context = [] ): Deserialization | ||
{ | ||
return new Deserialization( | ||
$this->sanitize($data), | ||
$this->sanitize($result), | ||
$type, | ||
$format, | ||
$context | ||
); | ||
} | ||
|
||
/** | ||
* @param mixed $data | ||
* @return mixed|string|LargeContent | ||
*/ | ||
private function sanitize($data) | ||
{ | ||
if ($this->getVarSize($data) > LargeContent::LIMIT_BYTES) { | ||
$data = $this->markAsHugeContent($data); | ||
} | ||
|
||
return $data; | ||
} | ||
|
||
/** | ||
* @param mixed $data | ||
* @return string|LargeContent | ||
*/ | ||
private function markAsHugeContent($data) | ||
{ | ||
$output = new LargeContent(); | ||
if (is_string($data)) { | ||
return (string) $output; | ||
} | ||
|
||
return $output; | ||
} | ||
|
||
private function getVarSize($data): int | ||
{ | ||
return \mb_strlen(serialize($data), 'UTF-8') ?? 0; | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
src/Symfony/Component/Serializer/Debug/SerializerActionFactoryInterface.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?php | ||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Serializer\Debug; | ||
|
||
use Symfony\Component\Serializer\Debug\Normalizer\Denormalization; | ||
use Symfony\Component\Serializer\Debug\Normalizer\Normalization; | ||
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface; | ||
use Symfony\Component\Serializer\Normalizer\NormalizerInterface; | ||
|
||
interface SerializerActionFactoryInterface | ||
{ | ||
/** | ||
* @param mixed $result | ||
*/ | ||
public function createDenormalization(DenormalizerInterface $denormalizer, string $data, $result, string $type, string $format, array $context = []): Denormalization; | ||
|
||
/** | ||
* @param mixed $object | ||
*/ | ||
public function createNormalization(NormalizerInterface $normalizer, $object, string $result, string $format, array $context = []): Normalization; | ||
|
||
/** | ||
* @param mixed $result | ||
*/ | ||
public function createDeserialization(string $data, $result, string $type, string $format, array $context = []): Deserialization; | ||
|
||
/** | ||
* @param mixed $data | ||
*/ | ||
public function createSerialization($data, string $result, string $format, array $context = []): Serialization; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.