Skip to content

Commit

Permalink
feat(src): Add ExceptionNotifyServiceProvider.php
Browse files Browse the repository at this point in the history
- Add ExceptionNotifyServiceProvider class
- Implement boot, provides, and registerMacros methods
  • Loading branch information
guanguans committed Aug 9, 2023
1 parent c54304f commit 15185b4
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions src/ExceptionNotifyServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
use Guanguans\LaravelExceptionNotify\Macros\StringableMacro;
use Guanguans\LaravelExceptionNotify\Macros\StrMacro;
use Illuminate\Container\Container;
use Illuminate\Contracts\Container\BindingResolutionException;
use Illuminate\Contracts\Debug\ExceptionHandler;
use Illuminate\Foundation\Application as LaravelApplication;
use Illuminate\Http\Request;
use Illuminate\Support\Collection;
Expand All @@ -29,6 +31,10 @@ class ExceptionNotifyServiceProvider extends ServiceProvider
{
protected bool $defer = false;

/**
* @throws \ReflectionException
* @throws BindingResolutionException
*/
public function register(): void
{
$this->setupConfig();
Expand All @@ -39,7 +45,15 @@ public function register(): void

public function boot(): void
{
$this->setupConfig();
$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);
});
}

return $handler;
});
}

public function provides(): array
Expand All @@ -53,13 +67,13 @@ public function provides(): array
}

/**
* @throws \Illuminate\Contracts\Container\BindingResolutionException
* @throws BindingResolutionException
* @throws \ReflectionException
*/
public function registerMacros(): void
{
Request::mixin($this->app->make(RequestMacro::class));
Collection::mixin($this->app->make(CollectionMacro::class));
Request::mixin($this->app->make(RequestMacro::class));
Str::mixin($this->app->make(StrMacro::class));
Stringable::mixin($this->app->make(StringableMacro::class));
}
Expand Down

0 comments on commit 15185b4

Please sign in to comment.