Skip to content

Commit

Permalink
feat(ReportUsingCreator): add class
Browse files Browse the repository at this point in the history
- Closes #48
- Create ReportUsingCreator class
  • Loading branch information
guanguans committed Aug 19, 2023
1 parent 0a87c42 commit 9431a62
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 12 deletions.
22 changes: 14 additions & 8 deletions config/exception-notify.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Guanguans\LaravelExceptionNotify\Pipes\ReplaceStrPipe;
use Guanguans\LaravelExceptionNotify\Pipes\ToHtmlPipe;
use Guanguans\LaravelExceptionNotify\Pipes\ToMarkdownPipe;
use Guanguans\LaravelExceptionNotify\ReportUsingCreator;

return [
/**
Expand All @@ -41,14 +42,6 @@
// Illuminate\Http\Exceptions\HttpResponseException::class,
],

/**
* The options of report exception job.
*/
'job' => [
'connection' => env('EXCEPTION_NOTIFY_JOB_CONNECTION', config('queue.default', 'sync')),
'queue' => env('EXCEPTION_NOTIFY_JOB_QUEUE'),
],

/**
* The rate limit of same exception.
*/
Expand All @@ -58,11 +51,24 @@
'decay_seconds' => (int) env('EXCEPTION_NOTIFY_RATE_LIMIT_DECAY_SECONDS', 300),
],

/**
* The options of report exception job.
*/
'job' => [
'connection' => env('EXCEPTION_NOTIFY_JOB_CONNECTION', config('queue.default', 'sync')),
'queue' => env('EXCEPTION_NOTIFY_JOB_QUEUE'),
],

/**
* The title of exception notification report.
*/
'title' => env('EXCEPTION_NOTIFY_TITLE', sprintf('The %s application exception report', config('app.name'))),

/**
* The creator of report using.
*/
'report_using_creator' => env('EXCEPTION_NOTIFY_REPORT_USING_CREATOR', ReportUsingCreator::class),

/**
* The list of collector.
*/
Expand Down
18 changes: 14 additions & 4 deletions src/ExceptionNotifyServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,10 +146,20 @@ protected function registerTestCommand(): self
protected function extendExceptionHandler(): self
{
$this->app->extend(ExceptionHandler::class, function (ExceptionHandler $handler): ExceptionHandler {
if (method_exists($handler, 'reportable')) {
$handler->reportable(function (\Throwable $e) use ($handler): void {
$this->app->make(ExceptionNotifyManager::class)->reportIf($handler->shouldReport($e), $e);
});
if (
($reportUsingCreator = config('exception-notify.report_using_creator'))
&& method_exists($handler, 'reportable')
) {
if (\is_string($reportUsingCreator) && class_exists($reportUsingCreator)) {
$reportUsingCreator = $this->app->make($reportUsingCreator);
}

$reportUsing = $reportUsingCreator($handler);
if ($reportUsing instanceof \Closure) {
$reportUsing = $reportUsing->bindTo($handler, $handler);
}

$handler->reportable($reportUsing);
}

return $handler;
Expand Down
32 changes: 32 additions & 0 deletions src/ReportUsingCreator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

declare(strict_types=1);

/**
* This file is part of the guanguans/laravel-exception-notify.
*
* (c) guanguans <ityaozm@gmail.com>
*
* This source file is subject to the MIT license that is bundled.
*/

namespace Guanguans\LaravelExceptionNotify;

/**
* @property \Illuminate\Contracts\Container\Container $container
*
* @mixin \Illuminate\Foundation\Exceptions\Handler
*/
class ReportUsingCreator
{
/**
* @psalm-suppress UndefinedThisPropertyFetch
* @psalm-suppress InaccessibleProperty
*/
public function __invoke(): \Closure
{
return function (\Throwable $e): void {
$this->container->make(ExceptionNotifyManager::class)->reportIf($this->shouldReport($e), $e);
};
}
}

0 comments on commit 9431a62

Please sign in to comment.