22
33namespace Sentry \Laravel ;
44
5+ use Sentry \FlushableClientInterface ;
6+ use Sentry \SentrySdk ;
7+ use Sentry \State \Hub ;
8+ use Sentry \State \HubInterface ;
59use function Sentry \addBreadcrumb ;
610use function Sentry \configureScope ;
711use Sentry \Breadcrumb ;
812use Sentry \Event ;
9- use Sentry \Client ;
1013use Sentry \Integration \IntegrationInterface ;
11- use Sentry \State \Hub ;
1214use Sentry \State \Scope ;
13- use Sentry \Transport \HttpTransport ;
1415
1516class Integration implements IntegrationInterface
1617{
@@ -25,7 +26,7 @@ class Integration implements IntegrationInterface
2526 public function setupOnce (): void
2627 {
2728 Scope::addGlobalEventProcessor (function (Event $ event ): Event {
28- $ self = Hub:: getCurrent ()->getIntegration (self ::class);
29+ $ self = static :: getCurrentHub ()->getIntegration (self ::class);
2930
3031 if (!$ self instanceof self) {
3132 return $ event ;
@@ -44,7 +45,7 @@ public function setupOnce(): void
4445 */
4546 public static function addBreadcrumb (Breadcrumb $ breadcrumb ): void
4647 {
47- $ self = Hub:: getCurrent ()->getIntegration (self ::class);
48+ $ self = static :: getCurrentHub ()->getIntegration (self ::class);
4849
4950 if (!$ self instanceof self) {
5051 return ;
@@ -60,7 +61,7 @@ public static function addBreadcrumb(Breadcrumb $breadcrumb): void
6061 */
6162 public static function configureScope (callable $ callback ): void
6263 {
63- $ self = Hub:: getCurrent ()->getIntegration (self ::class);
64+ $ self = static :: getCurrentHub ()->getIntegration (self ::class);
6465
6566 if (!$ self instanceof self) {
6667 return ;
@@ -93,21 +94,57 @@ public static function setTransaction($transaction): void
9394 */
9495 public static function flushEvents (): void
9596 {
96- $ client = Hub:: getCurrent ()->getClient ();
97+ $ client = static :: getCurrentHub ()->getClient ();
9798
98- if ($ client instanceof Client) {
99- $ transportProperty = new \ReflectionProperty (Client::class, 'transport ' );
100- $ transportProperty ->setAccessible (true );
99+ if ($ client instanceof FlushableClientInterface) {
100+ $ client ->flush ();
101+ }
102+ }
101103
102- $ transport = $ transportProperty ->getValue ($ client );
104+ /**
105+ * Gets the current hub. If it's not initialized then creates a new instance
106+ * and sets it as current hub.
107+ *
108+ * The is here for legacy reasons where we used the Hub directly as a singleton.
109+ *
110+ * @TODO: This method should be removed and replaced with calls to `SentrySdk::getCurrentHub()` directly once
111+ * `sentry/sentry` 3.0 is released and pinned as an dependency.
112+ *
113+ * @internal This is not part of the public API and is here temporarily.
114+ *
115+ * @return \Sentry\State\HubInterface
116+ */
117+ public static function getCurrentHub (): HubInterface
118+ {
119+ if (class_exists (SentrySdk::class)) {
120+ SentrySdk::getCurrentHub ();
121+ }
103122
104- if ($ transport instanceof HttpTransport) {
105- $ closure = \Closure::bind (function () {
106- $ this ->cleanupPendingRequests ();
107- }, $ transport , $ transport );
123+ return Hub::getCurrent ();
124+ }
108125
109- $ closure ();
110- }
126+ /**
127+ * Sets the current hub.
128+ *
129+ * The is here for legacy reasons where we used the Hub directly as a singleton.
130+ *
131+ * @TODO: This method should be removed and replaced with calls to `SentrySdk::getCurrentHub()` directly once
132+ * `sentry/sentry` 3.0 is released and pinned as an dependency.
133+ *
134+ * @internal This is not part of the public API and is here temporarily.
135+ *
136+ * @param \Sentry\State\HubInterface $hub
137+ *
138+ * @return void
139+ */
140+ public static function setCurrentHub (HubInterface $ hub ): void
141+ {
142+ if (class_exists (SentrySdk::class)) {
143+ SentrySdk::setCurrentHub ($ hub );
144+
145+ return ;
111146 }
147+
148+ Hub::setCurrent ($ hub );
112149 }
113150}
0 commit comments