Skip to content

Commit 7120489

Browse files
committed
Add: Config class
1 parent 320ef3d commit 7120489

File tree

10 files changed

+98
-115
lines changed

10 files changed

+98
-115
lines changed

src/Config.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
declare(strict_types = 1);
3+
4+
namespace MonoProcessor;
5+
6+
7+
class Config
8+
{
9+
public static function getAll()
10+
{
11+
$config = app()['config']['mono-processor'];
12+
13+
return empty($config) ? [] : $config;
14+
}
15+
16+
public static function getByKey($key)
17+
{
18+
return self::getAll()[$key];
19+
}
20+
21+
public static function isEnabledValue($field)
22+
{
23+
return array_key_exists($field, self::getAll()) ? self::getAll()[$field] === true : true;
24+
}
25+
}

src/EventHandler.php

Lines changed: 11 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -71,74 +71,38 @@ class EventHandler
7171
* @var \Illuminate\Contracts\Events\Dispatcher
7272
*/
7373
private $events;
74-
75-
/**
76-
* Indicates if we should we add SQL queries to the breadcrumbs.
77-
* @var bool
78-
*/
79-
private $recordSqlInfo;
80-
81-
/**
82-
* Indicates if we should we add route to the breadcrumbs.
83-
* @var bool
84-
*/
85-
private $recordRouteInfo;
86-
87-
/**
88-
* Indicates if we should we add queue info to the breadcrumbs.
89-
* @var bool
90-
*/
91-
private $recordQueueInfo;
92-
93-
/**
94-
* Indicates if we should we add auth info to the breadcrumbs.
95-
* @var bool
96-
*/
97-
private $recordAuthInfo;
98-
99-
/**
100-
* Indicates if we should we add console info to the breadcrumbs.
101-
* @var bool
102-
*/
103-
private $recordConsoleInfo;
104-
74+
10575
/**
10676
* EventHandler constructor.
10777
* @param \Illuminate\Contracts\Events\Dispatcher $events
10878
*/
109-
public function __construct(Dispatcher $events, array $config)
79+
public function __construct(Dispatcher $events)
11080
{
11181
$this->events = $events;
112-
113-
$this->recordSqlInfo = ($config['breadcrumbs.sql'] ?? $config['breadcrumbs']['sql'] ?? true) === true;
114-
$this->recordRouteInfo = ($config['breadcrumbs.route'] ?? $config['breadcrumbs']['route'] ?? true) === true;
115-
$this->recordQueueInfo = ($config['breadcrumbs.queue'] ?? $config['breadcrumbs']['queue'] ?? true) === true;
116-
$this->recordAuthInfo = ($config['breadcrumbs.auth'] ?? $config['breadcrumbs']['auth'] ?? true) === true;
117-
$this->recordConsoleInfo = ($config['breadcrumbs.console'] ?? $config['breadcrumbs']['console'] ?? true) === true;
11882
}
11983

12084
/**
12185
* Attach all event handlers.
12286
*/
12387
public function subscribe()
12488
{
125-
if ($this->recordRouteInfo) {
89+
if (Config::isEnabledValue('route')) {
12690
$this->subscribeRoute();;
12791
}
12892

129-
if ($this->recordSqlInfo) {
93+
if (Config::isEnabledValue('sql')) {
13094
$this->subscribeQuery();
13195
}
13296

133-
if ($this->recordConsoleInfo) {
97+
if (Config::isEnabledValue('config')) {
13498
$this->subscribeConsole();
13599
}
136100

137-
if ($this->recordAuthInfo) {
101+
if (Config::isEnabledValue('auth')) {
138102
$this->subscribeAuthEvents();
139103
}
140104

141-
if ($this->recordQueueInfo) {
105+
if (Config::isEnabledValue('queue')) {
142106
$this->subscribeQueueEvents();
143107
}
144108
}
@@ -251,7 +215,7 @@ protected function routeMatchedHandler(RouteMatched $match)
251215
*/
252216
protected function queryHandler($query, $bindings, $time, $connectionName)
253217
{
254-
if (!$this->recordSqlInfo) {
218+
if (!Config::isEnabledValue('sql')) {
255219
return;
256220
}
257221

@@ -272,7 +236,7 @@ protected function queryHandler($query, $bindings, $time, $connectionName)
272236
*/
273237
protected function queryExecutedHandler(QueryExecuted $query)
274238
{
275-
if (!$this->recordSqlInfo) {
239+
if (!Config::isEnabledValue('sql')) {
276240
return;
277241
}
278242
$data = ['connectionName' => $query->connectionName];
@@ -292,7 +256,7 @@ protected function queryExecutedHandler(QueryExecuted $query)
292256
*/
293257
protected function queueJobProcessingHandler(JobProcessing $event)
294258
{
295-
if (!$this->recordQueueInfo) {
259+
if (!Config::isEnabledValue('queue')) {
296260
return;
297261
}
298262

@@ -328,7 +292,7 @@ protected function queueWorkerStoppingHandler(WorkerStopping $event)
328292
protected function commandStartingHandler(CommandStarting $event)
329293
{
330294
if ($event->command) {
331-
if (!$this->recordQueueInfo) {
295+
if (!Config::isEnabledValue('queue')) {
332296
return;
333297
}
334298
Breadcrumbs::getInstance()

src/MonoProcessors.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,12 @@ public function __invoke(Logger $logger)
3838
true
3939
);
4040

41-
$format->setJsonPrettyPrint(true);
42-
$format->includeStacktraces(true);
41+
if (Config::isEnabledValue('json_format')){
42+
$format->setJsonPrettyPrint(true);
43+
}
44+
if (Config::isEnabledValue('stacktrace')){
45+
$format->includeStacktraces(true);
46+
}
4347
$handler->setFormatter($format);
4448
}
4549
}

src/Processors/AbstractProcessor.php

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,12 @@
44
namespace MonoProcessor\Processors;
55

66
use Monolog\Processor\ProcessorInterface;
7+
use MonoProcessor\Config;
78

89
abstract class AbstractProcessor implements ProcessorInterface
910
{
10-
private $config;
11-
12-
/**
13-
* @param string|int $level The minimum logging level at which this Processor will be triggered
14-
*/
15-
public function __construct()
16-
{
17-
$this->config = $this->getConfig();
18-
}
19-
2011
public function isWrite($level_name)
2112
{
22-
return in_array($level_name, ['ERROR','EMERGENCY']);
23-
}
24-
25-
private function getConfig() : array
26-
{
27-
$config = app()['config']['mono-processor'];
28-
29-
return empty($config) ? [] : $config;
30-
}
13+
return in_array($level_name, Config::getByKey('levels'), true);
14+
}
3115
}
Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,29 @@
11
<?php
2-
declare(strict_types=1);
2+
declare(strict_types = 1);
33

44
namespace MonoProcessor\Processors;
55

66

7+
use MonoProcessor\Config;
8+
79
class GitInfoProcessor extends AbstractProcessor
810
{
9-
public function __invoke(array $record): array
11+
public function __invoke(array $record) : array
1012
{
11-
if ( ! $this->isWrite($record['level_name'])) {
13+
if (!$this->isWrite($record['level_name']) || !Config::isEnabledValue('git')) {
1214
return $record;
1315
}
1416

1517
$branches = shell_exec('git branch -v --no-abbrev');
16-
18+
1719
if (preg_match('{^\* (.+?)\s+([a-f0-9]{40})(?:\s|$)(.*)}m', $branches, $matches)) {
1820
$record['extra']['git'] = [
1921
'branch' => $matches[1],
2022
'commit' => $matches[2],
2123
'name' => $matches[3],
2224
];
2325
}
24-
26+
2527
return $record;
2628
}
2729
}

src/Processors/MemoryProcessor.php

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,29 @@
11
<?php
2-
declare(strict_types=1);
2+
declare(strict_types = 1);
33

44
namespace MonoProcessor\Processors;
55

66

7+
use MonoProcessor\Config;
8+
79
class MemoryProcessor extends AbstractProcessor
810
{
9-
public function __invoke(array $record):array
11+
public function __invoke(array $record) : array
1012
{
11-
if ( ! $this->isWrite($record['level_name'])) {
13+
if (!$this->isWrite($record['level_name']) || !Config::isEnabledValue('memoryPeak')) {
1214
return $record;
1315
}
14-
16+
1517
$usage = memory_get_peak_usage(true);
1618
$usage = $this->formatBytes($usage);
17-
1819
$record['extra']['memory_peak_usage'] = $usage;
19-
20+
2021
return $record;
2122
}
22-
23-
public function formatBytes($bytes) {
24-
$unit=array('b','kb','mb','gb','tb','pb');
25-
return @round($bytes/pow(1024,($i=floor(log($bytes,1024)))),2).' '.$unit[$i];
23+
24+
public function formatBytes($bytes)
25+
{
26+
$unit = ['b', 'kb', 'mb', 'gb', 'tb', 'pb'];
27+
return @round($bytes / pow(1024, ($i = floor(log($bytes, 1024)))), 2) . ' ' . $unit[$i];
2628
}
2729
}
Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,24 @@
11
<?php
2-
declare(strict_types=1);
2+
declare(strict_types = 1);
33

44
namespace MonoProcessor\Processors;
55

66

7+
use MonoProcessor\Config;
8+
79
class PhpInfoProcessor extends AbstractProcessor
810
{
9-
public function __invoke(array $record): array
11+
public function __invoke(array $record) : array
1012
{
11-
if ( ! $this->isWrite($record['level_name'])) {
13+
if (!$this->isWrite($record['level_name']) || !Config::isEnabledValue('phpinfo')) {
1214
return $record;
1315
}
1416
$record['extra'] += [
1517
'php' => [
1618
'version' => PHP_VERSION,
1719
]
1820
];
19-
21+
2022
return $record;
2123
}
2224
}
Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,46 @@
11
<?php
2-
declare(strict_types=1);
2+
declare(strict_types = 1);
33

44
namespace MonoProcessor\Processors;
55

66

7+
use MonoProcessor\Config;
8+
79
class RequestProcessor extends AbstractProcessor
810
{
9-
public function __invoke(array $record): array
11+
public function __invoke(array $record) : array
1012
{
11-
if ( ! $this->isWrite($record['level_name'])) {
13+
if (!$this->isWrite($record['level_name']) || !Config::isEnabledValue('request')) {
1214
return $record;
1315
}
14-
16+
1517
$record['extra']['request'] = [];
16-
$record['extra']['request'] += [
17-
'base_info' => [
18-
'ip' => $_SERVER['REMOTE_ADDR'],
19-
'http_method' => $_SERVER['REQUEST_METHOD'],
20-
'full_url' => $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'],
21-
'server' => $_SERVER['SERVER_NAME'],
22-
'url' => $_SERVER['REQUEST_URI'],
23-
]
24-
];
25-
18+
19+
if (Config::isEnabledValue('request.base_info')) {
20+
$record['extra']['request'] += [
21+
'base_info' => [
22+
'ip' => $_SERVER['REMOTE_ADDR'],
23+
'http_method' => $_SERVER['REQUEST_METHOD'],
24+
'full_url' => $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'],
25+
'server' => $_SERVER['SERVER_NAME'],
26+
'url' => $_SERVER['REQUEST_URI'],
27+
]
28+
];
29+
}
30+
2631
$request = request();
27-
28-
if ($request) {
32+
33+
if ($request && Config::isEnabledValue('request.header')) {
2934
$record['extra']['request'] += [
3035
'header' => $request
3136
];
3237
}
33-
if ($request) {
38+
if ($request && Config::isEnabledValue('request.body')) {
3439
$record['extra']['request'] += [
3540
'body' => $request->all()
3641
];
3742
}
38-
43+
3944
return $record;
4045
}
4146
}

src/ServiceProvider.php

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,8 @@ public function register()
4040
*/
4141
protected function bindEvents()
4242
{
43-
$handler = new EventHandler($this->app->events, $this->getConfig());
43+
$handler = new EventHandler($this->app->events);
4444

4545
$handler->subscribe();
4646
}
47-
48-
private function getConfig() : array
49-
{
50-
$config = $this->app['config'][static::$package];
51-
52-
return empty($config) ? [] : $config;
53-
}
5447
}

src/config/mono-processor.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
'memoryPeak' => true,
55
'git' => true,
66
'phpinfo' => true,
7+
'route' => true,
78

89
'json_format' => true,
910

@@ -19,7 +20,8 @@
1920
'route' => true,
2021
'queue' => true,
2122
],
22-
'types' => [
23-
'debug'
23+
'levels' => [
24+
'ERROR',
25+
'EMERGENCY'
2426
]
2527
];

0 commit comments

Comments
 (0)