Skip to content

Commit

Permalink
ENH: Provide support for raygun4php ^1 and ^2.
Browse files Browse the repository at this point in the history
  • Loading branch information
GuySartorelli committed Nov 29, 2021
1 parent 1265386 commit 0ffe961
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 12 deletions.
8 changes: 8 additions & 0 deletions _config/cache.yml
@@ -0,0 +1,8 @@
---
Name: raygun-cache
---
SilverStripe\Core\Injector\Injector:
Psr\SimpleCache\CacheInterface.raygunCache:
factory: SilverStripe\Core\Cache\CacheFactory
constructor:
namespace: "raygunCache"
2 changes: 1 addition & 1 deletion composer.json
Expand Up @@ -13,7 +13,7 @@
],
"require": {
"php": ">=7.1",
"mindscape/raygun4php": "^1",
"mindscape/raygun4php": "^1 || ^2",
"silverstripe/framework": "^4.3"
},
"autoload": {
Expand Down
98 changes: 87 additions & 11 deletions src/RaygunClientFactory.php
Expand Up @@ -2,11 +2,17 @@

namespace SilverStripe\Raygun;

use GuzzleHttp\Client;
use LogicException;
use Psr\SimpleCache\CacheInterface;
use Raygun4php\RaygunClient;
use Raygun4php\Transports\GuzzleAsync;
use SilverStripe\Control\Director;
use SilverStripe\Core\Config\Config;
use SilverStripe\Core\Injector\Factory;
use SilverStripe\Core\Environment;
use SilverStripe\Control\Director;
use Raygun4php\RaygunClient;
use SilverStripe\Core\Injector\Factory;
use SilverStripe\Core\Injector\Injector;
use SilverStripe\Core\Path;

class RaygunClientFactory implements Factory
{
Expand Down Expand Up @@ -34,38 +40,77 @@ public function create($service, array $params = [])
{
// extract api key from .env file
$apiKey = $this->getCustomRaygunAppKey() ?? (string) Environment::getEnv(self::RAYGUN_APP_KEY_NAME);

// log error to warn user that exceptions will not be logged to Raygun
if (empty($apiKey) && !Director::isDev()) {
$name = self::RAYGUN_APP_KEY_NAME;
user_error("You need to set the {$name} environment variable in order to log to Raygun.", E_USER_WARNING);
}

// check if user tracking is enabled
$disableTracking = Config::inst()->get(
RaygunClient::class,
'disable_user_tracking'
);
$disableTracking = is_bool($disableTracking) ? $disableTracking : false;

// log error to warn user that exceptions will not be logged to Raygun
if (empty($apiKey) && !Director::isDev()) {
$name = self::RAYGUN_APP_KEY_NAME;
user_error("You need to set the {$name} environment variable in order to log to Raygun.", E_USER_WARNING);
// Setup new client in the way that is best for the current SDK version.
if (substr(ltrim(static::getSdkVersion(), 'v'), 0, 2) === '1.') {
$this->createForV1($apiKey, $disableTracking, $params);
} else {
$this->createForV2($apiKey, $disableTracking, $params);
}

// setup new client
$this->filterSensitiveData();

return $this->client;
}

protected function createForV1($apiKey, $disableTracking, $params)
{
// Instantiate actual client
$this->client = new RaygunClient(
$apiKey,
true,
false,
$disableTracking
);

// set proxy
// Set proxy
if (!empty($params['proxyHost'])) {
$proxy = $params['proxyHost'];
if (!empty($params['proxyPort'])) {
$proxy .= ':' . $params['proxyPort'];
}
$this->client->setProxy($proxy);
}
}

$this->filterSensitiveData();
protected function createForV2($apiKey, $disableTracking, $params)
{
// Prepare transport config.
$transportConfig = [
'base_uri' => 'https://api.raygun.com',
'timeout' => 2.0,
'headers' => [
'X-ApiKey' => $apiKey,
],
];

return $this->client;
// Set proxy
if (!empty($params['proxyHost'])) {
$proxy = $params['proxyHost'];
if (!empty($params['proxyPort'])) {
$proxy .= ':' . $params['proxyPort'];
}
$transportConfig['proxy'] = $proxy;
}

// Create raygun client using async transport.
$transport = new GuzzleAsync(
new Client($transportConfig)
);
$this->client = new RaygunClient($transport, $disableTracking);
}

protected function filterSensitiveData()
Expand All @@ -86,4 +131,35 @@ protected function filterSensitiveData()
'Cookie' => true,
]);
}

/**
* Get the currently installed version of the raygun4php package according to composer.lock
*
* @return string
*/
public static function getSdkVersion()
{
$cache = Injector::inst()->get(CacheInterface::class . '.raygunCache');
// If the SDK version isn't cached, get it from the composer.lock file.
if (!$version = $cache->get('raygun4phpVersion')) {
$composerLockRaw = file_get_contents(Path::join(Director::baseFolder(), 'composer.lock'));
if (!$composerLockRaw) {
throw new LogicException('composer.lock file is missing.');
}
$packageList = json_decode($composerLockRaw, true)['packages'];
foreach ($packageList as $package) {
if ($package['name'] === 'mindscape/raygun4php') {
$version = $package['version'];
break;
}
}
if (!$version) {
throw new LogicException('mindscape/raygun4php not found in composer.lock');
}
// Cache the SDK version so we don't have to do this every request.
$cache->set('raygun4phpVersion', $version);
}

return $version;
}
}

0 comments on commit 0ffe961

Please sign in to comment.