Skip to content

Commit

Permalink
refactor(pipes): Add LimitLengthPipe to CollectorManager
Browse files Browse the repository at this point in the history
- Added LimitLengthPipe to CollectorManager for processing
- Updated toPipes method to handle LimitLengthPipe
- Sorted pipes in toPipes method based on rules
  • Loading branch information
guanguans committed May 12, 2024
1 parent 9d14174 commit 80c43b4
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 1 deletion.
32 changes: 31 additions & 1 deletion src/CollectorManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Guanguans\LaravelExceptionNotify\Contracts\Collector;
use Guanguans\LaravelExceptionNotify\Contracts\ExceptionAware;
use Guanguans\LaravelExceptionNotify\Pipes\FixPrettyJsonPipe;
use Guanguans\LaravelExceptionNotify\Pipes\LimitLengthPipe;
use Illuminate\Pipeline\Pipeline;
use Illuminate\Support\Collection;
use Illuminate\Support\Fluent;
Expand Down Expand Up @@ -46,9 +47,38 @@ private function mapToReport(string $channel, Collection $collectors): string
{
return (string) (new Pipeline(app()))
->send($collectors)
->through(FixPrettyJsonPipe::class, ...config("exception-notify.channels.$channel.pipes", []))
->through($this->toPipes($channel))
->then(static fn (Collection $collectors): Stringable => Str::of(json_pretty_encode(
$collectors->jsonSerialize()
)));
}

private function toPipes(string $channel): array
{
$index = collect($pipes = config("exception-notify.channels.$channel.pipes", []))->search(
static fn (string $pipe) => Str::contains($pipe, LimitLengthPipe::class)
);

if (false === $index) {
return $pipes;
}

return collect($pipes)
->push(FixPrettyJsonPipe::class)
->sort(static function (string $a, string $b): int {
if (FixPrettyJsonPipe::class === $a && !Str::contains($b, LimitLengthPipe::class)) {
return 1;
}

$rules = [
FixPrettyJsonPipe::class,
LimitLengthPipe::class,
];

return collect($rules)->search(static fn (string $rule) => Str::contains($a, $rule))
<=> collect($rules)->search(static fn (string $rule) => Str::contains($b, $rule));
})
// ->dump()
->all();
}
}
40 changes: 40 additions & 0 deletions tests/CollectorManagerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

/** @noinspection AnonymousFunctionStaticInspection */
/** @noinspection StaticClosureCanBeUsedInspection */

declare(strict_types=1);

/**
* Copyright (c) 2021-2024 guanguans<[email protected]>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*
* @see https://github.com/guanguans/laravel-exception-notify
*/

namespace Guanguans\LaravelExceptionNotify\Tests;

use Guanguans\LaravelExceptionNotify\CollectorManager;
use Guanguans\LaravelExceptionNotify\Exceptions\RuntimeException;
use Guanguans\LaravelExceptionNotify\Pipes\LimitLengthPipe;
use Guanguans\LaravelExceptionNotify\Pipes\SprintfHtmlPipe;
use Illuminate\Support\Str;

it('can map to reports', function (): void {
collect(config('exception-notify.channels'))->each(fn (array $config, string $name) => config()->set(
"exception-notify.channels.$name.pipes",
collect($config['pipes'] ?? [])
->reject(fn (string $pipe) => Str::contains($pipe, SprintfHtmlPipe::class))
->push(hydrate_pipe(LimitLengthPipe::class, 512))
->all()
));

$reports = app(CollectorManager::class)->mapToReports(
array_keys(config('exception-notify.channels')),
new RuntimeException
);

expect(array_map(fn ($report): string => trim($report, " \n\r\t\v\0`"), $reports))->each->toBeJson();
})->group(__DIR__, __FILE__);

0 comments on commit 80c43b4

Please sign in to comment.