2
2
3
3
namespace Sentry \Laravel ;
4
4
5
- use function Sentry \configureScope ;
6
- use Sentry \ClientBuilder ;
7
5
use Sentry \State \Hub ;
6
+ use Sentry \ClientBuilder ;
7
+ use Illuminate \Log \LogManager ;
8
+ use Laravel \Lumen \Application as Lumen ;
9
+ use Illuminate \Foundation \Application as Laravel ;
10
+ use Illuminate \Support \ServiceProvider as IlluminateServiceProvider ;
8
11
9
- class ServiceProvider extends \ Illuminate \ Support \ServiceProvider
12
+ class ServiceProvider extends IlluminateServiceProvider
10
13
{
11
14
/**
12
15
* Abstract type to bind Sentry as in the Service Container.
@@ -16,108 +19,102 @@ class ServiceProvider extends \Illuminate\Support\ServiceProvider
16
19
public static $ abstract = 'sentry ' ;
17
20
18
21
/**
19
- * Bootstrap the application events.
20
- *
21
- * @return void
22
+ * Boot the service provider.
22
23
*/
23
- public function boot ()
24
+ public function boot (): void
24
25
{
25
- // Publish the configuration file
26
- $ this ->publishes (array (
27
- __DIR__ . '/../../../config/sentry.php ' => config_path (static ::$ abstract . '.php ' ),
28
- ), 'config ' );
26
+ $ this ->app ->make (self ::$ abstract );
29
27
30
28
$ this ->bindEvents ($ this ->app );
31
29
32
30
if ($ this ->app ->runningInConsole ()) {
31
+ if ($ this ->app instanceof Laravel) {
32
+ $ this ->publishes ([
33
+ __DIR__ . '/../../../config/sentry.php ' => config_path (static ::$ abstract . '.php ' ),
34
+ ], 'config ' );
35
+ }
36
+
33
37
$ this ->registerArtisanCommands ();
34
38
}
35
39
}
36
40
37
41
/**
38
- * Register the artisan commands .
42
+ * Register the service provider .
39
43
*/
40
- protected function registerArtisanCommands ()
44
+ public function register (): void
41
45
{
42
- $ this ->commands (array (
43
- 'Sentry\Laravel\TestCommand ' ,
44
- ));
46
+ if ($ this ->app instanceof Lumen) {
47
+ $ this ->app ->configure ('sentry ' );
48
+ }
49
+
50
+ $ this ->mergeConfigFrom (__DIR__ . '/../../../config/sentry.php ' , static ::$ abstract );
51
+
52
+ $ this ->configureAndRegisterClient ($ this ->app ['config ' ][static ::$ abstract ]);
53
+
54
+ if (($ logManager = $ this ->app ->make ('log ' )) instanceof LogManager) {
55
+ $ logManager ->extend ('sentry ' , function ($ app , array $ config ) {
56
+ return (new LogChannel ($ app ))($ config );
57
+ });
58
+ }
45
59
}
46
60
47
61
/**
48
62
* Bind to the Laravel event dispatcher to log events.
49
- *
50
- * @param $app
51
63
*/
52
- protected function bindEvents ($ app )
64
+ protected function bindEvents (): void
53
65
{
54
- $ userConfig = $ app ['config ' ][static ::$ abstract ];
66
+ $ userConfig = $ this -> app ['config ' ][static ::$ abstract ];
55
67
56
68
$ handler = new EventHandler ($ userConfig );
57
69
58
- $ handler ->subscribe ($ app ->events );
70
+ $ handler ->subscribe ($ this -> app ->events );
59
71
60
- // In Laravel >=5.3 we can get the user context from the auth events
61
- if (isset ($ userConfig ['send_default_pii ' ]) && $ userConfig ['send_default_pii ' ] !== false && version_compare ($ app ::VERSION , '5.3 ' ) >= 0 ) {
62
- $ handler ->subscribeAuthEvents ($ app ->events );
72
+ if (isset ($ userConfig ['send_default_pii ' ]) && $ userConfig ['send_default_pii ' ] !== false ) {
73
+ $ handler ->subscribeAuthEvents ($ this ->app ->events );
63
74
}
64
75
}
65
76
66
77
/**
67
- * Register the service provider.
68
- *
69
- * @return void
78
+ * Register the artisan commands.
70
79
*/
71
- public function register ()
80
+ protected function registerArtisanCommands (): void
72
81
{
73
- $ this ->mergeConfigFrom (__DIR__ . '/../../../config/sentry.php ' , static ::$ abstract );
74
-
75
- $ app = $ this ->app ;
82
+ $ this ->commands ([
83
+ TestCommand::class,
84
+ ]);
85
+ }
76
86
77
- $ this ->app ->singleton (static ::$ abstract , function ($ app ) {
78
- $ userConfig = $ app ['config ' ][static ::$ abstract ];
87
+ /**
88
+ * Configure and register the Sentry client with the container.
89
+ */
90
+ protected function configureAndRegisterClient (): void
91
+ {
92
+ $ this ->app ->singleton (static ::$ abstract , function () {
79
93
$ basePath = base_path ();
94
+ $ userConfig = $ this ->app ['config ' ][static ::$ abstract ];
80
95
81
- // We do not want this setting to hit our main client
96
+ // We do not want this setting to hit our main client because it's Laravel specific
82
97
unset($ userConfig ['breadcrumbs.sql_bindings ' ]);
98
+
83
99
$ options = \array_merge (
84
100
[
85
- 'environment ' => $ app ->environment (),
86
- 'prefixes ' => array ( $ basePath) ,
101
+ 'environment ' => $ this -> app ->environment (),
102
+ 'prefixes ' => [ $ basePath] ,
87
103
'project_root ' => $ basePath ,
88
- 'in_app_exclude ' => array ( $ basePath . '/vendor ' ) ,
89
- 'integrations ' => [new Integration ()]
104
+ 'in_app_exclude ' => [ $ basePath . '/vendor ' ] ,
105
+ 'integrations ' => [new Integration ],
90
106
],
91
107
$ userConfig
92
108
);
109
+
93
110
$ clientBuilder = ClientBuilder::create ($ options );
94
111
$ clientBuilder ->setSdkIdentifier (Version::SDK_IDENTIFIER );
95
112
$ clientBuilder ->setSdkVersion (Version::SDK_VERSION );
96
- Hub::setCurrent (new Hub ($ clientBuilder ->getClient ()));
97
113
98
- if (isset ($ userConfig ['send_default_pii ' ]) && $ userConfig ['send_default_pii ' ] !== false && version_compare ($ app ::VERSION , '5.3 ' ) < 0 ) {
99
- try {
100
- // Bind user context if available
101
- if ($ app ['auth ' ]->check ()) {
102
- configureScope (function (Scope $ scope ) use ($ app ): void {
103
- $ scope ->setUser (['id ' => $ app ['auth ' ]->user ()->getAuthIdentifier ()]);
104
- });
105
- }
106
- } catch (Exception $ e ) {
107
- error_log (sprintf ('sentry.breadcrumbs error=%s ' , $ e ->getMessage ()));
108
- }
109
- }
114
+ Hub::setCurrent (new Hub ($ clientBuilder ->getClient ()));
110
115
111
116
return Hub::getCurrent ();
112
117
});
113
-
114
- // Add a sentry log channel for Laravel 5.6+
115
- if (version_compare ($ app ::VERSION , '5.6 ' ) >= 0 ) {
116
- $ app ->make ('log ' )->extend ('sentry ' , function ($ app , array $ config ) {
117
- $ channel = new LogChannel ($ app );
118
- return $ channel ($ config );
119
- });
120
- }
121
118
}
122
119
123
120
/**
0 commit comments