Primo Committ
This commit is contained in:
14
vendor/facade/flare-client-php/src/Truncation/AbstractTruncationStrategy.php
vendored
Normal file
14
vendor/facade/flare-client-php/src/Truncation/AbstractTruncationStrategy.php
vendored
Normal file
@@ -0,0 +1,14 @@
|
||||
<?php
|
||||
|
||||
namespace Facade\FlareClient\Truncation;
|
||||
|
||||
abstract class AbstractTruncationStrategy implements TruncationStrategy
|
||||
{
|
||||
/** @var ReportTrimmer */
|
||||
protected $reportTrimmer;
|
||||
|
||||
public function __construct(ReportTrimmer $reportTrimmer)
|
||||
{
|
||||
$this->reportTrimmer = $reportTrimmer;
|
||||
}
|
||||
}
|
||||
41
vendor/facade/flare-client-php/src/Truncation/ReportTrimmer.php
vendored
Normal file
41
vendor/facade/flare-client-php/src/Truncation/ReportTrimmer.php
vendored
Normal file
@@ -0,0 +1,41 @@
|
||||
<?php
|
||||
|
||||
namespace Facade\FlareClient\Truncation;
|
||||
|
||||
class ReportTrimmer
|
||||
{
|
||||
protected static $maxPayloadSize = 524288;
|
||||
|
||||
protected $strategies = [
|
||||
TrimStringsStrategy::class,
|
||||
TrimContextItemsStrategy::class,
|
||||
];
|
||||
|
||||
public function trim(array $payload): array
|
||||
{
|
||||
foreach ($this->strategies as $strategy) {
|
||||
if (! $this->needsToBeTrimmed($payload)) {
|
||||
break;
|
||||
}
|
||||
|
||||
$payload = (new $strategy($this))->execute($payload);
|
||||
}
|
||||
|
||||
return $payload;
|
||||
}
|
||||
|
||||
public function needsToBeTrimmed(array $payload): bool
|
||||
{
|
||||
return strlen(json_encode($payload)) > self::getMaxPayloadSize();
|
||||
}
|
||||
|
||||
public static function getMaxPayloadSize(): int
|
||||
{
|
||||
return self::$maxPayloadSize;
|
||||
}
|
||||
|
||||
public static function setMaxPayloadSize(int $maxPayloadSize): void
|
||||
{
|
||||
self::$maxPayloadSize = $maxPayloadSize;
|
||||
}
|
||||
}
|
||||
44
vendor/facade/flare-client-php/src/Truncation/TrimContextItemsStrategy.php
vendored
Normal file
44
vendor/facade/flare-client-php/src/Truncation/TrimContextItemsStrategy.php
vendored
Normal file
@@ -0,0 +1,44 @@
|
||||
<?php
|
||||
|
||||
namespace Facade\FlareClient\Truncation;
|
||||
|
||||
class TrimContextItemsStrategy extends AbstractTruncationStrategy
|
||||
{
|
||||
public static function thresholds()
|
||||
{
|
||||
return [100, 50, 25, 10];
|
||||
}
|
||||
|
||||
public function execute(array $payload): array
|
||||
{
|
||||
foreach (static::thresholds() as $threshold) {
|
||||
if (! $this->reportTrimmer->needsToBeTrimmed($payload)) {
|
||||
break;
|
||||
}
|
||||
|
||||
$payload['context'] = $this->iterateContextItems($payload['context'], $threshold);
|
||||
}
|
||||
|
||||
return $payload;
|
||||
}
|
||||
|
||||
protected function iterateContextItems(array $contextItems, int $threshold): array
|
||||
{
|
||||
array_walk($contextItems, [$this, 'trimContextItems'], $threshold);
|
||||
|
||||
return $contextItems;
|
||||
}
|
||||
|
||||
protected function trimContextItems(&$value, $key, int $threshold)
|
||||
{
|
||||
if (is_array($value)) {
|
||||
if (count($value) > $threshold) {
|
||||
$value = array_slice($value, $threshold * -1, $threshold);
|
||||
}
|
||||
|
||||
array_walk($value, [$this, 'trimContextItems'], $threshold);
|
||||
}
|
||||
|
||||
return $value;
|
||||
}
|
||||
}
|
||||
35
vendor/facade/flare-client-php/src/Truncation/TrimStringsStrategy.php
vendored
Normal file
35
vendor/facade/flare-client-php/src/Truncation/TrimStringsStrategy.php
vendored
Normal file
@@ -0,0 +1,35 @@
|
||||
<?php
|
||||
|
||||
namespace Facade\FlareClient\Truncation;
|
||||
|
||||
class TrimStringsStrategy extends AbstractTruncationStrategy
|
||||
{
|
||||
public static function thresholds()
|
||||
{
|
||||
return [1024, 512, 256];
|
||||
}
|
||||
|
||||
public function execute(array $payload): array
|
||||
{
|
||||
foreach (static::thresholds() as $threshold) {
|
||||
if (! $this->reportTrimmer->needsToBeTrimmed($payload)) {
|
||||
break;
|
||||
}
|
||||
|
||||
$payload = $this->trimPayloadString($payload, $threshold);
|
||||
}
|
||||
|
||||
return $payload;
|
||||
}
|
||||
|
||||
protected function trimPayloadString(array $payload, int $threshold): array
|
||||
{
|
||||
array_walk_recursive($payload, function (&$value) use ($threshold) {
|
||||
if (is_string($value) && strlen($value) > $threshold) {
|
||||
$value = substr($value, 0, $threshold);
|
||||
}
|
||||
});
|
||||
|
||||
return $payload;
|
||||
}
|
||||
}
|
||||
8
vendor/facade/flare-client-php/src/Truncation/TruncationStrategy.php
vendored
Normal file
8
vendor/facade/flare-client-php/src/Truncation/TruncationStrategy.php
vendored
Normal file
@@ -0,0 +1,8 @@
|
||||
<?php
|
||||
|
||||
namespace Facade\FlareClient\Truncation;
|
||||
|
||||
interface TruncationStrategy
|
||||
{
|
||||
public function execute(array $payload): array;
|
||||
}
|
||||
Reference in New Issue
Block a user