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

Update the PHP transport classes section #1276

Closed
Closed
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
68 changes: 60 additions & 8 deletions src/collections/_documentation/platforms/php/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -324,10 +324,21 @@ the events, but report them as sent.
use Sentry\ClientBuilder;
use Sentry\Transport\NullTransport;
use Sentry\State\Hub;
use Sentry\Transport\TransportFactoryInterface;

$transport = new NullTransport();
$builder = ClientBuilder::create(['dsn' => '___PUBLIC_DSN___']);
$builder->setTransport($transport);
$options = ['dsn' => '___PUBLIC_DSN___'];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would just inline this variable since we won't reuse it besides in the ClientBuilder::create method


$transportFactory = new class implements TransportFactoryInterface {

public function create(\Sentry\Options $options): \Sentry\Transport\TransportInterface
{
return new NullTransport();
}
};


$builder = ClientBuilder::create($options);
$builder->setTransportFactory($transportFactory);

Hub::getCurrent()->bindClient($builder->getClient());
```
Expand All @@ -345,16 +356,50 @@ The best adapter available is automatically selected when creating a client inst
through the client builder, but you can override it using the appropriate methods.

```php
use Http\Discovery\HttpAsyncClientDiscovery;
use Http\Discovery\MessageFactoryDiscovery;
use Http\Discovery\StreamFactoryDiscovery;
use Http\Discovery\UriFactoryDiscovery;
use Http\Message\ResponseFactory;
use Http\Message\UriFactory;
use Sentry\ClientBuilder;
use Sentry\HttpClient\HttpClientFactory;
use Sentry\HttpClient\HttpClientFactoryInterface;
use Sentry\Transport\HttpTransport;
use Sentry\State\Hub;
use Sentry\Transport\TransportFactoryInterface;

$options = ['dsn' => '___PUBLIC_DSN___'];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would just inline this variable since we won't reuse it besides in the ClientBuilder::create method


$transport = new HttpTransport($options, HttpAsyncClientDiscovery::find(), MessageFactoryDiscovery::find());
$httpClientFactory = new HttpClientFactory(
UriFactoryDiscovery::find(),
MessageFactoryDiscovery::find(),
StreamFactoryDiscovery::find(),
null,
'sentry.php',
'2.3'
);

$transportFactory = new class($httpClientFactory) implements TransportFactoryInterface {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of creating a custom factory, the DefaultTransportFactory can be used too in this case as the logic is basically the same. Of course if there was custom logic involved creating your own transport factory is still needed

/**
* @var HttpClientFactoryInterface
*/
private $clientFactory;

public function __construct(HttpClientFactoryInterface $clientFactory)
{
$this->clientFactory = $clientFactory;
}

public function create(\Sentry\Options $options): \Sentry\Transport\TransportInterface
{
return new HttpTransport($options, $this->clientFactory->create($options), MessageFactoryDiscovery::find());
}
};


$builder = ClientBuilder::create($options);
$builder->setTransport($transport);
$builder->setTransportFactory($transportFactory);

Hub::getCurrent()->bindClient($builder->getClient());
```
Expand Down Expand Up @@ -382,12 +427,19 @@ use Sentry\State\Hub;

$options = ['dsn' => '___PUBLIC_DSN___'];

$spool = new MemorySpool();
$transport = new SpoolTransport($spool);

$httpTransport = new HttpTransport($options, HttpAsyncClientDiscovery::find(), MessageFactoryDiscovery::find());

$transportFactory = new class implements TransportFactoryInterface {
public function create(\Sentry\Options $options): \Sentry\Transport\TransportInterface
{
$spool = new MemorySpool();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since the spool is the class that users should interact with to flush the events, it should be instantiated outside of the factory

return new SpoolTransport($spool);
}
};

$builder = ClientBuilder::create($options);
$builder->setTransport($transport);
$builder->setTransportFactory($transportFactory);

Hub::getCurrent()->bindClient($builder->getClient());

Expand Down