1
|
|
|
<?php namespace Understand\UnderstandLaravel5; |
2
|
|
|
|
3
|
|
|
use Illuminate\Support\ServiceProvider; |
4
|
|
|
|
5
|
|
|
abstract class UnderstandServiceProvider extends ServiceProvider |
6
|
|
|
{ |
7
|
|
|
/** |
8
|
|
|
* Register config |
9
|
|
|
* |
10
|
|
|
* @return void |
11
|
|
|
*/ |
12
|
|
|
protected function registerConfig() |
13
|
|
|
{ |
14
|
|
|
$configPath = __DIR__ . '/../../config/understand-laravel.php'; |
15
|
|
|
$this->mergeConfigFrom($configPath, 'understand-laravel'); |
16
|
|
|
} |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Register token generator class |
20
|
|
|
* |
21
|
|
|
* @return void |
22
|
|
|
*/ |
23
|
|
|
protected function registerTokenProvider() |
24
|
|
|
{ |
25
|
|
|
$this->app->singleton('understand.tokenProvider', function () |
26
|
|
|
{ |
27
|
|
|
return new TokenProvider(); |
28
|
|
|
}); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Register data collector class |
33
|
|
|
* |
34
|
|
|
* @return void |
35
|
|
|
*/ |
36
|
|
|
protected function registerDataCollector() |
37
|
|
|
{ |
38
|
|
|
$this->app->singleton('understand.dataCollector', function () |
39
|
|
|
{ |
40
|
|
|
return new DataCollector(); |
41
|
|
|
}); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Register exception encoder |
46
|
|
|
* |
47
|
|
|
* @return void |
48
|
|
|
*/ |
49
|
|
|
protected function registerExceptionEncoder() |
50
|
|
|
{ |
51
|
|
|
$this->app->bind('understand.exceptionEncoder', function () |
52
|
|
|
{ |
53
|
|
|
return new ExceptionEncoder; |
54
|
|
|
}); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Register exception and event logger |
59
|
|
|
* |
60
|
|
|
* @return void |
61
|
|
|
*/ |
62
|
|
|
protected function registerEventLoggers() |
63
|
|
|
{ |
64
|
|
|
$this->app->bind('understand.eventLogger', function($app) |
65
|
|
|
{ |
66
|
|
|
return new EventLogger($app['understand.logger'], $app['config']); |
67
|
|
|
}); |
68
|
|
|
|
69
|
|
|
$this->app->bind('understand.exceptionLogger', function($app) |
70
|
|
|
{ |
71
|
|
|
return new ExceptionLogger($app['understand.logger'], $app['understand.exceptionEncoder'], $app['config']); |
72
|
|
|
}); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Register understand logger |
77
|
|
|
* |
78
|
|
|
* @return void |
79
|
|
|
*/ |
80
|
|
|
protected function registerLogger() |
81
|
|
|
{ |
82
|
|
|
$this->app->singleton('understand.logger', function($app) |
83
|
|
|
{ |
84
|
|
|
$fieldProvider = $app['understand.fieldProvider']; |
85
|
|
|
$handler = $this->resolveHandler($app); |
86
|
|
|
|
87
|
|
|
return new Logger($fieldProvider, $handler); |
88
|
|
|
}); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Return default handler |
93
|
|
|
* |
94
|
|
|
* @param type $app |
95
|
|
|
* @return mixed |
96
|
|
|
* @throws \ErrorException |
97
|
|
|
*/ |
98
|
|
|
protected function resolveHandler($app) |
99
|
|
|
{ |
100
|
|
|
$inputToken = $app['config']->get('understand-laravel.token'); |
101
|
|
|
|
102
|
|
|
$apiUrl = $app['config']->get('understand-laravel.url', 'https://api.understand.io'); |
103
|
|
|
$handlerType = $app['config']->get('understand-laravel.handler'); |
104
|
|
|
$sslBundlePath = $app['config']->get('understand-laravel.ssl_ca_bundle'); |
105
|
|
|
|
106
|
|
|
if ($handlerType == 'async') |
107
|
|
|
{ |
108
|
|
|
return new Handlers\AsyncHandler($inputToken, $apiUrl, $sslBundlePath); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
if ($handlerType == 'sync') |
112
|
|
|
{ |
113
|
|
|
return new Handlers\SyncHandler($inputToken, $apiUrl, $sslBundlePath); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
throw new \ErrorException('understand-laravel handler misconfiguration:' . $handlerType); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @param $level |
121
|
|
|
* @param $message |
122
|
|
|
* @param $context |
123
|
|
|
* @return bool |
124
|
|
|
*/ |
125
|
|
|
protected function shouldIgnoreEvent($level, $message, $context) |
|
|
|
|
126
|
|
|
{ |
127
|
|
|
$ignoredEventTypes = (array)$this->app['config']->get('understand-laravel.ignored_logs'); |
128
|
|
|
|
129
|
|
|
if (!$ignoredEventTypes) { |
|
|
|
|
130
|
|
|
return false; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
return in_array($level, $ignoredEventTypes, true); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Get the services provided by the provider. |
138
|
|
|
* |
139
|
|
|
* @return array |
140
|
|
|
*/ |
141
|
|
|
public function provides() |
142
|
|
|
{ |
143
|
|
|
return [ |
144
|
|
|
'understand.fieldProvider', |
145
|
|
|
'understand.logger', |
146
|
|
|
'understand.exceptionEncoder', |
147
|
|
|
'understand.exceptionLogger', |
148
|
|
|
'understand.eventLogger', |
149
|
|
|
'understand.tokenProvider', |
150
|
|
|
'understand.dataCollector', |
151
|
|
|
]; |
152
|
|
|
} |
153
|
|
|
} |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.