Raven.php in Raven: Sentry Integration 3.x
File
src/Plugin/CspReportingHandler/Raven.php
View source
<?php
namespace Drupal\raven\Plugin\CspReportingHandler;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Url;
use Drupal\csp\Csp;
use Drupal\csp\Plugin\ReportingHandlerBase;
use Sentry\Dsn;
use Symfony\Component\DependencyInjection\ContainerInterface;
class Raven extends ReportingHandlerBase implements ContainerFactoryPluginInterface {
protected $configFactory;
public function __construct(array $configuration, $plugin_id, $plugin_definition, $configFactory) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->configFactory = $configFactory;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('config.factory'));
}
public function alterPolicy(Csp $policy) {
if (!class_exists(Dsn::class)) {
return;
}
$config = $this->configFactory
->get('raven.settings');
try {
$dsn = Dsn::createFromString(empty($_SERVER['SENTRY_DSN']) ? $config
->get('public_dsn') : $_SERVER['SENTRY_DSN']);
} catch (\InvalidArgumentException $e) {
return;
}
$query = [
'sentry_key' => $dsn
->getPublicKey(),
];
if ($environment = empty($_SERVER['SENTRY_ENVIRONMENT']) ? $config
->get('environment') : $_SERVER['SENTRY_ENVIRONMENT']) {
$query['sentry_environment'] = $environment;
}
if ($release = empty($_SERVER['SENTRY_RELEASE']) ? $config
->get('release') : $_SERVER['SENTRY_RELEASE']) {
$query['sentry_release'] = $release;
}
$policy
->setDirective('report-uri', Url::fromUri(str_replace('/store/', '/security/', $dsn
->getStoreApiEndpointUrl()), [
'query' => $query,
])
->toString());
}
}
Classes
Name |
Description |
Raven |
CSP Reporting Plugin for a Sentry endpoint. |