Skip to content

Commit 37f8161

Browse files
committed
Optimize collectors
1 parent a4305dd commit 37f8161

20 files changed

+441
-146
lines changed

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
"prefer-stable": true,
5353
"require": {
5454
"php": ">=7.2",
55+
"ext-json": "*",
5556
"guanguans/notify": "^1.13",
5657
"illuminate/bus": "^5.5 || ^6.0 || ^7.0 || ^8.0 || ^9.0",
5758
"illuminate/contracts": "^5.5 || ^6.0 || ^7.0 || ^8.0 || ^9.0",

config/exception-notify.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,17 @@
8080
\Guanguans\LaravelExceptionNotify\Collectors\LaravelCollector::class,
8181
\Guanguans\LaravelExceptionNotify\Collectors\AnnexCollector::class,
8282
\Guanguans\LaravelExceptionNotify\Collectors\PhpInfoCollector::class,
83-
\Guanguans\LaravelExceptionNotify\Collectors\ExceptionCollector::class,
84-
\Guanguans\LaravelExceptionNotify\Collectors\RequestCollector::class,
83+
\Guanguans\LaravelExceptionNotify\Collectors\ExceptionBasicCollector::class,
84+
\Guanguans\LaravelExceptionNotify\Collectors\ExceptionTraceCollector::class,
85+
\Guanguans\LaravelExceptionNotify\Collectors\RequestBasicCollector::class,
86+
// \Guanguans\LaravelExceptionNotify\Collectors\RequestMiddlewareCollector::class,
87+
// \Guanguans\LaravelExceptionNotify\Collectors\RequestHeaderCollector::class,
88+
\Guanguans\LaravelExceptionNotify\Collectors\RequestQueryCollector::class,
89+
\Guanguans\LaravelExceptionNotify\Collectors\RequestPostCollector::class,
90+
\Guanguans\LaravelExceptionNotify\Collectors\RequestFileCollector::class,
91+
// \Guanguans\LaravelExceptionNotify\Collectors\RequestServerCollector::class,
92+
// \Guanguans\LaravelExceptionNotify\Collectors\RequestCookieCollector::class,
93+
// \Guanguans\LaravelExceptionNotify\Collectors\RequestSessionCollector::class,
8594
],
8695
// The transformer is used to transformer collectors to reports.
8796
'transformer' => \Guanguans\LaravelExceptionNotify\CollectorTransformer::class,
@@ -98,7 +107,7 @@
98107
'reporting' => [
99108
// \Guanguans\LaravelExceptionNotify\Listeners\Reporting\LogReportListener::class,
100109
// \Guanguans\LaravelExceptionNotify\Listeners\Reporting\DumpReportListener::class,
101-
// \Guanguans\LaravelExceptionNotify\Listeners\Reporting\DdReportListener::class,
110+
\Guanguans\LaravelExceptionNotify\Listeners\Reporting\DdReportListener::class,
102111
],
103112

104113
/*

src/Collectors/AnnexCollector.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
class AnnexCollector extends Collector
1414
{
15-
public function collect(): array
15+
public function collect()
1616
{
1717
return [
1818
'Time' => date('Y-m-d H:i:s'),

src/Collectors/Collector.php

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,26 +10,45 @@
1010

1111
namespace Guanguans\LaravelExceptionNotify\Collectors;
1212

13-
use ArrayAccess;
1413
use Guanguans\LaravelExceptionNotify\Contracts\Collector as CollectorContract;
15-
use Guanguans\LaravelExceptionNotify\Support\Traits\OptionsProperty;
14+
use Illuminate\Support\Collection;
1615
use Illuminate\Support\Str;
1716

18-
abstract class Collector implements CollectorContract, ArrayAccess
17+
abstract class Collector implements CollectorContract
1918
{
20-
use OptionsProperty;
19+
/**
20+
* @var callable|null
21+
*/
22+
protected $pipe;
23+
24+
public function __construct(callable $pipe = null)
25+
{
26+
$this->pipe = $pipe;
27+
}
2128

2229
public function getName(): string
2330
{
2431
return Str::ucfirst(Str::beforeLast(class_basename($this), 'Collector'));
2532
}
2633

34+
protected function applyPipeCollect()
35+
{
36+
$pipedCollect = collect($this->collect())
37+
->when($this->pipe, function (Collection $collects) {
38+
return $collects->pipe($this->pipe);
39+
});
40+
41+
return collect($pipedCollect)
42+
->filter(function ($item) {
43+
return ! blank($item);
44+
})
45+
->all();
46+
}
47+
2748
public function __toString()
2849
{
2950
return varexport(
30-
array_filter($this->collect(), function ($value) {
31-
return ! blank($value);
32-
}),
51+
$this->applyPipeCollect(),
3352
true
3453
);
3554
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
/**
4+
* This file is part of the guanguans/laravel-exception-notify.
5+
*
6+
* (c) guanguans <ityaozm@gmail.com>
7+
*
8+
* This source file is subject to the MIT license that is bundled.
9+
*/
10+
11+
namespace Guanguans\LaravelExceptionNotify\Collectors;
12+
13+
use Guanguans\LaravelExceptionNotify\Concerns\ExceptionProperty;
14+
use Guanguans\LaravelExceptionNotify\Contracts\ExceptionProperty as ExceptionPropertyContract;
15+
use Guanguans\LaravelExceptionNotify\Support\ExceptionContext;
16+
17+
class ExceptionBasicCollector extends Collector implements ExceptionPropertyContract
18+
{
19+
use ExceptionProperty;
20+
21+
public function collect()
22+
{
23+
return [
24+
'Class' => get_class($this->exception),
25+
'Message' => $this->exception->getMessage(),
26+
'Code' => $this->exception->getCode(),
27+
'File' => $this->exception->getFile(),
28+
'Line' => $this->exception->getLine(),
29+
'Preview' => ExceptionContext::getFormattedContext($this->exception),
30+
];
31+
}
32+
}

src/Collectors/ExceptionCollector.php

Lines changed: 0 additions & 67 deletions
This file was deleted.
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
/**
4+
* This file is part of the guanguans/laravel-exception-notify.
5+
*
6+
* (c) guanguans <ityaozm@gmail.com>
7+
*
8+
* This source file is subject to the MIT license that is bundled.
9+
*/
10+
11+
namespace Guanguans\LaravelExceptionNotify\Collectors;
12+
13+
use Guanguans\LaravelExceptionNotify\Concerns\ExceptionProperty;
14+
use Guanguans\LaravelExceptionNotify\Contracts\ExceptionProperty as ExceptionPropertyContract;
15+
use Illuminate\Support\Collection;
16+
use Illuminate\Support\Str;
17+
18+
class ExceptionTraceCollector extends Collector implements ExceptionPropertyContract
19+
{
20+
use ExceptionProperty;
21+
22+
public function __construct(callable $pipe = null)
23+
{
24+
parent::__construct();
25+
$this->pipe = $pipe
26+
?: function (Collection $traces) {
27+
return $traces->filter(function ($trace) {
28+
return ! Str::contains($trace, 'vendor');
29+
});
30+
};
31+
}
32+
33+
public function collect()
34+
{
35+
return collect($this->exception->getTrace())
36+
->filter(function ($trace) {
37+
return isset($trace['file']) and isset($trace['line']);
38+
})
39+
->map(function ($trace) {
40+
return $trace['file']."({$trace['line']})";
41+
})
42+
->all();
43+
}
44+
}

src/Collectors/LaravelCollector.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,13 @@ class LaravelCollector extends Collector
1919
*/
2020
protected $app;
2121

22-
public function __construct(Container $app)
22+
public function __construct(Container $app, callable $pipe = null)
2323
{
24+
parent::__construct($pipe);
2425
$this->app = $app;
2526
}
2627

27-
public function collect(): array
28+
public function collect()
2829
{
2930
return [
3031
'Name' => $this->app['config']['app.name'],

src/Collectors/PhpInfoCollector.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
class PhpInfoCollector extends Collector
1414
{
15-
public function collect(): array
15+
public function collect()
1616
{
1717
return [
1818
'Version' => implode('.', [PHP_MAJOR_VERSION, PHP_MINOR_VERSION, PHP_RELEASE_VERSION]),
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
/**
4+
* This file is part of the guanguans/laravel-exception-notify.
5+
*
6+
* (c) guanguans <ityaozm@gmail.com>
7+
*
8+
* This source file is subject to the MIT license that is bundled.
9+
*/
10+
11+
namespace Guanguans\LaravelExceptionNotify\Collectors;
12+
13+
use Illuminate\Http\Request;
14+
15+
class RequestBasicCollector extends Collector
16+
{
17+
/**
18+
* @var \Illuminate\Http\Request
19+
*/
20+
protected $request;
21+
22+
public function __construct(Request $request, callable $pipe = null)
23+
{
24+
parent::__construct($pipe);
25+
$this->request = $request;
26+
}
27+
28+
public function collect()
29+
{
30+
return [
31+
'Url' => $this->request->url(),
32+
'Ip' => $this->request->ip(),
33+
'Method' => $this->request->method(),
34+
'Action' => optional($this->request->route())->getActionName(),
35+
'Duration' => value(function () {
36+
$startTime = defined('LARAVEL_START') ? LARAVEL_START : $this->request->server('REQUEST_TIME_FLOAT');
37+
38+
return floor((microtime(true) - $startTime) * 1000).'ms';
39+
}),
40+
];
41+
}
42+
}

src/Collectors/RequestCollector.php

Lines changed: 0 additions & 64 deletions
This file was deleted.

0 commit comments

Comments
 (0)