Skip to content

Commit

Permalink
Add CollectionMacro
Browse files Browse the repository at this point in the history
  • Loading branch information
guanguans committed Feb 27, 2022
1 parent 455708a commit c03dce8
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 20 deletions.
35 changes: 15 additions & 20 deletions src/ExceptionContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

namespace Guanguans\LaravelExceptionNotify;

use Illuminate\Support\Collection;
use Illuminate\Support\Str;
use Throwable;

Expand All @@ -25,27 +26,20 @@ class ExceptionContext
*/
public static function getContextAsString(Throwable $exception)
{
$context = static::get($exception);
$contextStr = collect(static::get($exception))
->tap(function (Collection $context) use ($exception, &$exceptionLine, &$markedExceptionLine, &$maxLineLen) {
$exceptionLine = $exception->getLine();
$markedExceptionLine = sprintf('➤ %s', $exceptionLine);
$maxLineLen = max(mb_strlen(array_key_last($context->toArray())), mb_strlen($markedExceptionLine));
})
->reduces(function ($carry, $code, $line) use ($maxLineLen, $markedExceptionLine, $exceptionLine) {
$line === $exceptionLine and $line = $markedExceptionLine;
$line = sprintf("%{$maxLineLen}s", $line);

$exceptionLine = $exception->getLine();
return "$carry $line $code".PHP_EOL;
}, '');

$markedExceptionLine = sprintf('➤ %s', $exceptionLine);

$maxLineLen = max(mb_strlen(array_key_last($context)), mb_strlen($markedExceptionLine));

$contextString = PHP_EOL.array_reduces(
$context,
function ($carry, $code, $line) use ($maxLineLen, $exceptionLine, $markedExceptionLine) {
$line === $exceptionLine and $line = $markedExceptionLine;

$line = str_pad($line, $maxLineLen, ' ', STR_PAD_LEFT);

return "$carry $line $code".PHP_EOL;
},
''
);

return "($contextString)";
return sprintf('(%s)', PHP_EOL.$contextStr);
}

/**
Expand Down Expand Up @@ -83,6 +77,7 @@ protected static function getFileContext(Throwable $exception)
->slice($exception->getLine() - 10, 20)
->mapWithKeys(function ($value, $key) {
return [$key + 1 => $value];
})->all();
})
->all();
}
}
3 changes: 3 additions & 0 deletions src/ExceptionNotifyServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@

namespace Guanguans\LaravelExceptionNotify;

use Guanguans\LaravelExceptionNotify\Macros\CollectionMacro;
use Illuminate\Foundation\Application as LaravelApplication;
use Illuminate\Support\Collection;
use Illuminate\Support\ServiceProvider;
use Laravel\Lumen\Application as LumenApplication;

Expand All @@ -24,6 +26,7 @@ class ExceptionNotifyServiceProvider extends ServiceProvider
public function boot()
{
$this->setupConfig();
Collection::mixin($this->app->make(CollectionMacro::class));
}

/**
Expand Down
22 changes: 22 additions & 0 deletions src/Macros/CollectionMacro.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

/**
* This file is part of the guanguans/laravel-exception-notify.
*
* (c) guanguans <[email protected]>
*
* This source file is subject to the MIT license that is bundled.
*/

namespace Guanguans\LaravelExceptionNotify\Macros;

class CollectionMacro
{
public function reduces(): callable
{
return function (callable $callback, $carry = null) {
/* @var \Illuminate\Support\Collection $this */
return array_reduces($this->items, $callback, $carry);
};
}
}

0 comments on commit c03dce8

Please sign in to comment.