Skip to content

Commit

Permalink
Fixed first client initialization and fixed raygun4php version to new…
Browse files Browse the repository at this point in the history
… patches only
  • Loading branch information
redJ4y committed Feb 1, 2023
1 parent 7603163 commit 2126c63
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 12 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
},
"require": {
"php": ">=7.4",
"mindscape/raygun4php": "^2.3",
"mindscape/raygun4php": "~2.3.0",
"monolog/monolog": "^2.4"
},
"config": {
Expand Down
21 changes: 10 additions & 11 deletions src/RaygunClientManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@
use Monolog\Handler\FirePHPHandler;
use Monolog\Handler\StreamHandler;
use Monolog\Logger;
use Raygun4php\Interfaces\TransportInterface;
use Raygun4php\Transports\GuzzleAsync;
use Raygun4php\Transports\GuzzleSync;

class RaygunClientManager {
// Singleton-esque RaygunClient instance
private static RaygunClient $instance;
// Store RaygunClient modifiers for reuse
private static Client $httpClient;
private static Logger $logger;
// Remember the current async state to detect changes
Expand All @@ -33,7 +33,7 @@ public static function getInstance(string $customApiKey = null, bool $customUser
$async = $customAsyncState ?? 1 == get_option('rg4wp_async');

if (!isset(self::$instance)) {
// An instance has not yet been created
// An instance has not yet been created...
$apiKey = $customApiKey ?? get_option('rg4wp_apikey');
// Initialize httpClient:
self::$httpClient = new Client([
Expand All @@ -52,34 +52,33 @@ public static function getInstance(string $customApiKey = null, bool $customUser
->pushHandler(new FirePHPHandler());
}

self::createNewInstance($async, $userTracking);
self::$instance = new RaygunClient(self::getTransport($async), !$userTracking);
self::$currentAsyncState = $async;
} else {
// An instance already exists
// An instance already exists...
if ($async != self::$currentAsyncState) {
// Ensure async state changes take effect
self::createNewInstance($async, $userTracking); // Also updates user tracking state
} else {
self::$instance = self::$instance->constructNew(self::getTransport($async), $userTracking);
self::$currentAsyncState = $async;
} else if ($userTracking == self::$instance->getDisableUserTracking()) {
// Ensure user tracking state changes take effect
self::$instance->setDisableUserTracking(!$userTracking);
}
}
return self::$instance;
}

private static function createNewInstance($async, $userTracking): void {
private static function getTransport(bool $async): TransportInterface {
/**
* Create the appropriate asynchronous or synchronous transport
* @see https://raygun.com/documentation/language-guides/php/crash-reporting/installation/#synchronous-usage
*/
$transport = $async ? new GuzzleAsync(self::$httpClient) : new GuzzleSync(self::$httpClient);

if (isset(self::$logger)) {
// Attach the logger to the transport
$transport->setLogger(self::$logger);
}

self::$instance = self::$instance->constructNew($transport, $userTracking);
self::$currentAsyncState = $async;
return $transport;
}

protected static function getLogsPath(): string {
Expand Down

0 comments on commit 2126c63

Please sign in to comment.