22
33namespace Sentry \Laravel ;
44
5- use function Sentry \configureScope ;
6- use Sentry \ClientBuilder ;
75use 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 ;
811
9- class ServiceProvider extends \ Illuminate \ Support \ServiceProvider
12+ class ServiceProvider extends IlluminateServiceProvider
1013{
1114 /**
1215 * Abstract type to bind Sentry as in the Service Container.
@@ -16,108 +19,102 @@ class ServiceProvider extends \Illuminate\Support\ServiceProvider
1619 public static $ abstract = 'sentry ' ;
1720
1821 /**
19- * Bootstrap the application events.
20- *
21- * @return void
22+ * Boot the service provider.
2223 */
23- public function boot ()
24+ public function boot (): void
2425 {
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 );
2927
3028 $ this ->bindEvents ($ this ->app );
3129
3230 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+
3337 $ this ->registerArtisanCommands ();
3438 }
3539 }
3640
3741 /**
38- * Register the artisan commands .
42+ * Register the service provider .
3943 */
40- protected function registerArtisanCommands ()
44+ public function register (): void
4145 {
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+ }
4559 }
4660
4761 /**
4862 * Bind to the Laravel event dispatcher to log events.
49- *
50- * @param $app
5163 */
52- protected function bindEvents ($ app )
64+ protected function bindEvents (): void
5365 {
54- $ userConfig = $ app ['config ' ][static ::$ abstract ];
66+ $ userConfig = $ this -> app ['config ' ][static ::$ abstract ];
5567
5668 $ handler = new EventHandler ($ userConfig );
5769
58- $ handler ->subscribe ($ app ->events );
70+ $ handler ->subscribe ($ this -> app ->events );
5971
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 );
6374 }
6475 }
6576
6677 /**
67- * Register the service provider.
68- *
69- * @return void
78+ * Register the artisan commands.
7079 */
71- public function register ()
80+ protected function registerArtisanCommands (): void
7281 {
73- $ this ->mergeConfigFrom (__DIR__ . '/../../../config/sentry.php ' , static ::$ abstract );
74-
75- $ app = $ this ->app ;
82+ $ this ->commands ([
83+ TestCommand::class,
84+ ]);
85+ }
7686
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 () {
7993 $ basePath = base_path ();
94+ $ userConfig = $ this ->app ['config ' ][static ::$ abstract ];
8095
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
8297 unset($ userConfig ['breadcrumbs.sql_bindings ' ]);
98+
8399 $ options = \array_merge (
84100 [
85- 'environment ' => $ app ->environment (),
86- 'prefixes ' => array ( $ basePath) ,
101+ 'environment ' => $ this -> app ->environment (),
102+ 'prefixes ' => [ $ basePath] ,
87103 'project_root ' => $ basePath ,
88- 'in_app_exclude ' => array ( $ basePath . '/vendor ' ) ,
89- 'integrations ' => [new Integration ()]
104+ 'in_app_exclude ' => [ $ basePath . '/vendor ' ] ,
105+ 'integrations ' => [new Integration ],
90106 ],
91107 $ userConfig
92108 );
109+
93110 $ clientBuilder = ClientBuilder::create ($ options );
94111 $ clientBuilder ->setSdkIdentifier (Version::SDK_IDENTIFIER );
95112 $ clientBuilder ->setSdkVersion (Version::SDK_VERSION );
96- Hub::setCurrent (new Hub ($ clientBuilder ->getClient ()));
97113
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 ()));
110115
111116 return Hub::getCurrent ();
112117 });
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- }
121118 }
122119
123120 /**
0 commit comments