-
-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathExceptionNotifyManager.php
99 lines (81 loc) · 3.05 KB
/
ExceptionNotifyManager.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
<?php
/** @noinspection PhpUnused */
declare(strict_types=1);
/**
* Copyright (c) 2021-2025 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;
use Guanguans\LaravelExceptionNotify\Channels\Channel;
use Guanguans\LaravelExceptionNotify\Contracts\ChannelContract;
use Guanguans\LaravelExceptionNotify\Exceptions\InvalidArgumentException;
use Guanguans\LaravelExceptionNotify\Support\Traits\AggregationTrait;
use Illuminate\Config\Repository;
use Illuminate\Support\Manager;
use function Guanguans\LaravelExceptionNotify\Support\rescue;
/**
* @property \Illuminate\Foundation\Application $container
*
* @method \Guanguans\LaravelExceptionNotify\Channels\Channel driver(?string $driver = null)
*
* @mixin \Guanguans\LaravelExceptionNotify\Channels\Channel
*/
class ExceptionNotifyManager extends Manager implements ChannelContract
{
use AggregationTrait {
AggregationTrait::__call as macroCall;
}
public function __call(mixed $method, mixed $parameters): mixed
{
return static::hasMacro($method)
? $this->macroCall($method, $parameters)
: parent::__call($method, $parameters);
}
public function channel(?string $channel = null): Channel
{
return $this->driver($channel);
}
public function report(\Throwable $throwable): void
{
rescue(function () use ($throwable): void {
$this->driver()->report($throwable);
});
}
public function reportContent(string $content): mixed
{
return rescue(fn (): mixed => $this->driver()->reportContent($content));
}
public function getDefaultDriver(): string
{
return config('exception-notify.default');
}
/**
* @noinspection MethodShouldBeFinalInspection
* @noinspection MissingParentCallInspection
* @noinspection PhpMissingParentCallCommonInspection
*/
protected function createDriver(mixed $driver): Channel
{
$channelContract = $this->createOriginalDriver($driver);
return $channelContract instanceof Channel ? $channelContract : new Channel($channelContract);
}
private function createOriginalDriver(string $driver): ChannelContract
{
if (isset($this->customCreators[$driver])) {
return $this->callCustomCreator($driver);
}
$configRepository = tap(
new Repository($this->config->get("exception-notify.channels.$driver", [])),
static fn (Repository $configRepository): mixed => $configRepository->set('__channel', $driver)
);
$studlyName = str($configRepository->get('driver', $driver))->studly();
if (class_exists($class = "\\Guanguans\\LaravelExceptionNotify\\Channels\\{$studlyName}Channel")) {
return new $class($configRepository);
}
throw new InvalidArgumentException("Driver [$driver] not supported.");
}
}