Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow for decorating the client builder #290

Merged
merged 3 commits into from
Nov 7, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## Unreleased

- Allow decorating the `ClientBuilderInterface` from the `register` method of a Service Provider (#290)

## 1.4.1

- Fix default Monolog logger level being invalid when using the Log channel (#287)
Expand Down
12 changes: 11 additions & 1 deletion src/Sentry/Laravel/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Sentry\ClientBuilder;
use Sentry\State\HubInterface;
use Illuminate\Log\LogManager;
use Sentry\ClientBuilderInterface;
use Laravel\Lumen\Application as Lumen;
use Sentry\Integration as SdkIntegration;
use Illuminate\Foundation\Application as Laravel;
Expand Down Expand Up @@ -95,7 +96,7 @@ protected function registerArtisanCommands(): void
*/
protected function configureAndRegisterClient(): void
{
$this->app->singleton(static::$abstract, function () {
$this->app->bind(ClientBuilderInterface::class, function () {
$basePath = base_path();
$userConfig = $this->getUserConfig();

Expand All @@ -120,9 +121,18 @@ protected function configureAndRegisterClient(): void
);

$clientBuilder = ClientBuilder::create($options);

// Set the Laravel SDK identifier and version
$clientBuilder->setSdkIdentifier(Version::SDK_IDENTIFIER);
$clientBuilder->setSdkVersion(Version::SDK_VERSION);

return $clientBuilder;
});

$this->app->singleton(static::$abstract, function () {
/** @var \Sentry\ClientBuilderInterface $clientBuilder */
$clientBuilder = $this->app->make(ClientBuilderInterface::class);

$options = $clientBuilder->getOptions();

if ($options->hasDefaultIntegrations()) {
Expand Down
35 changes: 35 additions & 0 deletions test/Sentry/ServiceClientBuilderDecoratorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace Sentry\Laravel\Tests;

use Sentry\ClientBuilderInterface;
use Sentry\Laravel\ServiceProvider;

class ServiceClientBuilderDecoratorTest extends \Orchestra\Testbench\TestCase
{
protected function getEnvironmentSetUp($app)
{
$app['config']->set('sentry.dsn', 'http://publickey:[email protected]/123');

$app->extend(ClientBuilderInterface::class, function (ClientBuilderInterface $clientBuilder) {
$clientBuilder->getOptions()->setEnvironment('from_service_container');

return $clientBuilder;
});
}

protected function getPackageProviders($app)
{
return [
ServiceProvider::class,
];
}

public function testClientHasCustomSerializer()
{
/** @var \Sentry\Options $options */
$options = $this->app->make('sentry')->getClient()->getOptions();

$this->assertEquals('from_service_container', $options->getEnvironment());
}
}