Skip to content

Commit

Permalink
Adds guzzle7-adapter support (#156)
Browse files Browse the repository at this point in the history
* Adds guzzle7-adapter support

* Adds guzzle6-adapter support

---------

Co-authored-by: Deeka Wong <[email protected]>
  • Loading branch information
huangdijia and huangdijia authored Mar 9, 2023
1 parent 7b81616 commit 0c893cb
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 238 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
"hyperf/paginator": "~3.0.0",
"hyperf/scout": "~3.0.0",
"mockery/mockery": "^1.0",
"php-http/guzzle7-adapter": "^1.0",
"phpstan/phpstan": "^1.0",
"phpunit/phpunit": "^9.5",
"swoole/ide-helper": "dev-master",
Expand Down
4 changes: 4 additions & 0 deletions src/sentry/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@
"hyperf/logger": "~3.0.0",
"sentry/sdk": "^3.0"
},
"suggest": {
"php-http/guzzle6-adapter": "Required to use the Guzzle transport (^1.0|^2.0).",
"php-http/guzzle7-adapter": "Required to use the Guzzle transport (^1.0)."
},
"config": {
"sort-packages": true
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
use Hyperf\Di\Aop\ProceedingJoinPoint;
use Sentry\Breadcrumb;

class HttpClientAspect extends AbstractAspect
class GuzzleHttpClientAspect extends AbstractAspect
{
public array $classes = [
Client::class . '::requestAsync',
Expand Down
37 changes: 35 additions & 2 deletions src/sentry/src/Aspect/SentryHttpClientFactoryAspect.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,18 @@
*/
namespace FriendsOfHyperf\Sentry\Aspect;

use FriendsOfHyperf\Sentry\GuzzleAdapter\Client;
use GuzzleHttp\Client as GuzzleClient;
use GuzzleHttp\ClientInterface;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Middleware;
use GuzzleHttp\RequestOptions as GuzzleHttpClientOptions;
use GuzzleHttp\Utils;
use Http\Adapter\Guzzle6\Client as Guzzle6HttpClient;
use Http\Adapter\Guzzle7\Client as Guzzle7HttpClient;
use Hyperf\Di\Aop\AbstractAspect;
use Hyperf\Di\Aop\ProceedingJoinPoint;
use Hyperf\Guzzle\CoroutineHandler;
use Hyperf\Utils\Coroutine;
use Sentry\HttpClient\HttpClientFactory;

class SentryHttpClientFactoryAspect extends AbstractAspect
Expand All @@ -37,6 +45,31 @@ public function process(ProceedingJoinPoint $proceedingJoinPoint)
$guzzleConfig[GuzzleHttpClientOptions::PROXY] = $options->getHttpProxy();
}

return Client::createWithConfig($guzzleConfig);
return match (true) {
class_exists(Guzzle7HttpClient::class) => $this->createHttpClientWithConfig(Guzzle7HttpClient::class, $guzzleConfig),
class_exists(Guzzle6HttpClient::class) => $this->createHttpClientWithConfig(Guzzle6HttpClient::class, $guzzleConfig),
default => $proceedingJoinPoint->process(),
};
}

/**
* @return ClientInterface|HttpAsyncClientInterface
*/
private function createHttpClientWithConfig(string $class, array $config)
{
if (
extension_loaded('swoole')
&& Coroutine::inCoroutine()
&& (\Swoole\Runtime::getHookFlags() & SWOOLE_HOOK_NATIVE_CURL) == 0
) {
$handlerStack = new HandlerStack(new CoroutineHandler());
} else {
$handlerStack = new HandlerStack(Utils::chooseHandler());
}

$handlerStack->push(Middleware::prepareBody(), 'prepare_body');
$config = array_merge(['handler' => $handlerStack], $config);

return new $class(new GuzzleClient($config));
}
}
3 changes: 2 additions & 1 deletion src/sentry/src/ConfigProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@ public function __invoke(): array
return [
'aspects' => [
Aspect\BreadcrumbAspect::class,
Aspect\HttpClientAspect::class,
Aspect\GuzzleHttpClientAspect::class,
Aspect\LoggerAspect::class,
Aspect\RedisAspect::class,
Aspect\SingletonAspect::class,
Aspect\SentryHttpClientFactoryAspect::class,
],
'commands' => [
Command\TestCommand::class,
Expand Down
90 changes: 0 additions & 90 deletions src/sentry/src/GuzzleAdapter/Client.php

This file was deleted.

This file was deleted.

127 changes: 0 additions & 127 deletions src/sentry/src/GuzzleAdapter/Promise.php

This file was deleted.

0 comments on commit 0c893cb

Please sign in to comment.