Skip to content
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
50 changes: 50 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,56 @@ $ composer require codex-team/hawk.php
]);
```

#### Passing custom Transport

The default transport requires the `ext-curl` PHP extension.
If `ext-curl` is unavailable, provide a custom transport via the `transport` option.

You can also provide your own transport by implementing `TransportInterface`.
Custom transports own their endpoint and timeout configuration, so the `url` and `timeout` options are used only by the default transport.

For example, install a PHP-version-compatible Guzzle release in your application and wrap it with a custom transport:

```php
use Hawk\Event;
use Hawk\Options;
use Hawk\Transport\TransportInterface;

class CustomGuzzleTransport implements TransportInterface
{
private $url;
private $client;

public function __construct(string $url = Options::DEFAULT_URL)
{
$this->url = $url;
$this->client = new \GuzzleHttp\Client();
}

public function getUrl(): string
{
return $this->url;
}

public function send(Event $event)
{
$this->client->request('POST', $this->url, [
'headers' => [
'Content-Type' => 'application/json',
],
'body' => json_encode($event, JSON_UNESCAPED_UNICODE),
]);
}
}

\Hawk\Catcher::init([
'integrationToken' => 'your integration token',
'transport' => new CustomGuzzleTransport(),
]);
Comment thread
Copilot marked this conversation as resolved.
```

#### Passing User and Context

After initialization you can set `user` or `context` for any event that will be send to Hawk

```php
Expand Down
6 changes: 4 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@
"description": "PHP errors Catcher module for Hawk.so",
"keywords": ["hawk", "php", "error", "catcher"],
"type": "library",
"version": "2.2.11",
"version": "2.3.0",
"license": "MIT",
"require": {
"ext-curl": "*",
"ext-json": "*",
"php": "^7.2 || ^8.0",
"jean85/pretty-package-versions": "^1.5 || ^2.0"
},
"suggest": {
"ext-curl": "Required to use the default CurlTransport. Not needed when providing a custom transport."
},
"require-dev": {
"phpunit/phpunit": "^8.2",
"friendsofphp/php-cs-fixer": "^2.15",
Expand Down
2 changes: 1 addition & 1 deletion src/Catcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ private function __construct(array $options)
$builder->registerAddon(new Headers());
$builder->registerAddon(new Environment());

$transport = new CurlTransport($options->getUrl(), $options->getTimeout());
$transport = $options->getTransport() ?? new CurlTransport($options->getUrl(), $options->getTimeout());

$this->handler = new Handler($options, $transport, $builder);

Expand Down
25 changes: 24 additions & 1 deletion src/Options.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,15 @@

namespace Hawk;

use Hawk\Transport\TransportInterface;

/**
* Class Options is responsible for configuring the Hawk catcher.
*/
class Options
{
public const DEFAULT_URL = 'https://k1.hawk.so/';

/**
* @var string
*/
Expand All @@ -17,7 +21,7 @@ class Options
/**
* @var string
*/
private $url = 'https://k1.hawk.so/';
private $url = self::DEFAULT_URL;

/**
* @var string
Expand All @@ -44,6 +48,11 @@ class Options
*/
private $timeout = 2;

/**
* @var TransportInterface|null
*/
private $transport = null;

/**
* Map of accepted option keys to class properties.
*/
Expand All @@ -59,6 +68,7 @@ class Options
'beforeSend' => 'beforeSend',
'before_send' => 'beforeSend',
'timeout' => 'timeout',
'transport' => 'transport',
];

/**
Expand Down Expand Up @@ -130,6 +140,14 @@ private function setOption(string $key, $value): void

break;

case 'transport':
if (!$value instanceof TransportInterface && $value !== null) {
throw new \InvalidArgumentException("Option 'transport' must implement TransportInterface or be null.");
}
$this->transport = $value;

break;

default:
throw new \InvalidArgumentException("Unknown option '$key'.");
}
Expand Down Expand Up @@ -169,4 +187,9 @@ public function getTimeout(): int
{
return $this->timeout;
}

public function getTransport(): ?TransportInterface
{
return $this->transport;
}
}
6 changes: 4 additions & 2 deletions src/Transport/GuzzleTransport.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
/**
* Class GuzzleTransport
*
* @deprecated Use a custom TransportInterface implementation instead.
*
* @package Hawk\Transport
*/
class GuzzleTransport implements TransportInterface
Expand All @@ -31,8 +33,8 @@ public function getUrl(): string
/**
* @inheritDoc
*/
public function send(Event $event): void
public function send(Event $event)
{
// TODO: Implement send() method.
throw new \BadMethodCallException('GuzzleTransport is not implemented. Please provide your own TransportInterface implementation.');
}
}
2 changes: 1 addition & 1 deletion tests/Unit/OptionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public function testDefaultOptions(): void
$this->assertNull($options->getBeforeSend());
$this->assertEmpty($options->getIntegrationToken());
$this->assertEmpty($options->getRelease());
$this->assertEquals('https://k1.hawk.so/', $options->getUrl());
$this->assertEquals(Options::DEFAULT_URL, $options->getUrl());
$this->assertEquals(error_reporting(), $options->getErrorTypes());
Comment on lines +19 to 20
}

Expand Down
Loading