Skip to content

Commit

Permalink
Merge pull request #14 from minbaby/feature/upgrade-for-hyperf3.0
Browse files Browse the repository at this point in the history
upgrade for hyperf v3
  • Loading branch information
minbaby authored Dec 10, 2022
2 parents de9650f + 10d0fb9 commit 4ba5ac6
Show file tree
Hide file tree
Showing 14 changed files with 70 additions and 68 deletions.
15 changes: 11 additions & 4 deletions .php_cs → .php-cs-fixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
$header = <<<'EOF'
EOF;

return PhpCsFixer\Config::create()
return (new PhpCsFixer\Config())
->setRiskyAllowed(true)
->setRules([
'@PSR2' => true,
'@Symfony' => true,
'@DoctrineAnnotation' => true,
'@PhpCsFixer' => true,
'header_comment' => [
'commentType' => 'PHPDoc',
'comment_type' => 'PHPDoc',
'header' => $header,
'separate' => 'none',
'location' => 'after_declare_strict',
Expand Down Expand Up @@ -56,11 +56,18 @@
'multiline_whitespace_before_semicolons' => [
'strategy' => 'no_multi_line',
],
'constant_case' => [
'case' => 'lower',
],
'global_namespace_import' => [
'import_classes' => true,
'import_constants' => true,
'import_functions' => null,
],
'class_attributes_separation' => true,
'combine_consecutive_unsets' => true,
'declare_strict_types' => true,
'linebreak_after_opening_tag' => true,
'lowercase_constants' => true,
'lowercase_static_reference' => true,
'no_useless_else' => true,
'no_unused_imports' => true,
Expand All @@ -78,4 +85,4 @@
->exclude('vendor')
->in(__DIR__)
)
->setUsingCache(false);
->setUsingCache(false);
22 changes: 16 additions & 6 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
v3.0.0:
- thx for @Vitaliy901 [Hyperf support v3.0](https://github.com/minbaby/hyperf-sentry/pull/13)
- [@deprecated v3.0 please use `hyperf/context` instead.] (https://github.com/hyperf/hyperf/blob/2.2/src/utils/src/Context.php)
- [限制 php:>=8.0](https://github.com/hyperf/hyperf/blob/3.0/composer.json#LL19C13-L19C13)
- upgrade php-cs-fixer >=3.12.0

v2.2.1:
- @ljyljy0211 [Update README.md](https://github.com/minbaby/hyperf-sentry/pull/12)

v2.2.0:
- 移除不必要的包限制,转为靠 `hyperf` 限制
- upgrade to `sentry/sdk:^3.0`
- 使用 [class_map](https://hyperf.wiki/2.0/#/zh-cn/annotation?id=classmap-%e5%8a%9f%e8%83%bd) 重写 `\Sentry\SentrySdk`
- 使用 Aspect,拦截单例,`Minbaby\HyperfSentry\Aspect\SingletonHookAspect::class`
- 增加 `php bin/hyperf.php sentry:test` 命令
- 移除不必要的包限制,转为靠 `hyperf` 限制
- upgrade to `sentry/sdk:^3.0`
- 使用 [class_map](https://hyperf.wiki/2.0/#/zh-cn/annotation?id=classmap-%e5%8a%9f%e8%83%bd) 重写 `\Sentry\SentrySdk`
- 使用 Aspect,拦截单例,`Minbaby\HyperfSentry\Aspect\SingletonHookAspect::class`
- 增加 `php bin/hyperf.php sentry:test` 命令

v2.0.0:
- 升级 hyperf 依赖到 2.0
- 升级 hyperf 依赖到 2.0
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
|0.1.*|1.1.*|-|
|2.0.*|2.0.*|-|
|2.2.*|>=2.1.*| 本次更新移除 hyperf 包依赖|
|3.0.*|>=3.0.*| 因为 `Hyperf\Utils\Context` => `Hyperf\Utils\Context` |

## 使用
1. 安装
Expand Down
4 changes: 2 additions & 2 deletions class_map/SentrySdk.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Sentry;

use Hyperf\Utils\Context;
use Hyperf\Context\Context;
use Sentry\State\Hub;
use Sentry\State\HubInterface;

Expand All @@ -16,7 +16,7 @@ class SentrySdk
/**
* @var null|HubInterface The current hub
*/
private static $currentHub;
private static ?HubInterface $currentHub;

/**
* Constructor.
Expand Down
17 changes: 10 additions & 7 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,18 @@
}
},
"require": {
"php": ">=8.0",
"sentry/sdk": "^3.0"
},
"require-dev": {
"friendsofphp/php-cs-fixer": "^2.14",
"hyperf/command": "^2.0",
"hyperf/database": "^2.0",
"hyperf/di": "^2.0",
"hyperf/framework": "^2.0",
"hyperf/logger": "^2.0",
"hyperf/testing": "^2.0",
"friendsofphp/php-cs-fixer": "^3.12.0",
"hyperf/command": "~3.0.0",
"hyperf/database": "~3.0.0",
"hyperf/di": "~3.0.0",
"hyperf/framework": "~3.0.0",
"hyperf/logger": "~3.0.0",
"hyperf/testing": "~3.0.0",
"hyperf/context": "~3.0.0",
"monolog/monolog": "^2.0",
"phpstan/phpstan": "^0.12",
"swoft/swoole-ide-helper": "dev-master"
Expand All @@ -41,6 +43,7 @@
"analyse": "phpstan analyse --memory-limit 300M -l 0 ./src",
"cs-fix": "php-cs-fixer fix $1"
},
"minimum-stability": "RC",
"extra": {
"hyperf": {
"config": "Minbaby\\HyperfSentry\\ConfigProvider"
Expand Down
7 changes: 2 additions & 5 deletions src/AfterWorkerStartListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,7 @@

class AfterWorkerStartListener implements ListenerInterface
{
/**
* @var \Hyperf\Di\Container|\Psr\Container\ContainerInterface
*/
protected $container;
protected \Hyperf\Di\Container|ContainerInterface $container;

public function __construct(ContainerInterface $container)
{
Expand All @@ -35,7 +32,7 @@ public function listen(): array
* complete before the event is returned to the EventDispatcher.
* @param AfterWorkerStart|object $event
*/
public function process(object $event)
public function process(object $event): void
{
$eventHandler = $this->container->get(EventHandler::class);
$eventHandler->subscribe();
Expand Down
10 changes: 5 additions & 5 deletions src/Aspect/SingletonHookAspect.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,17 @@

namespace Minbaby\HyperfSentry\Aspect;

use Hyperf\Context\Context;
use Hyperf\Di\Aop\AbstractAspect;
use Hyperf\Di\Aop\ProceedingJoinPoint;
use Hyperf\Utils\Context;
use Hyperf\Di\Exception\Exception;

/**
* Hook 不安全的单例实现.
*/
class SingletonHookAspect extends AbstractAspect
{
public $classes = [
public array $classes = [
\Sentry\EventType::class,
\Sentry\ResponseStatus::class,
\Sentry\Integration\IntegrationRegistry::class,
Expand All @@ -22,10 +23,9 @@ class SingletonHookAspect extends AbstractAspect
];

/**
* @throws \Hyperf\Di\Exception\Exception
* @return mixed
* @throws Exception
*/
public function process(ProceedingJoinPoint $proceedingJoinPoint)
public function process(ProceedingJoinPoint $proceedingJoinPoint): mixed
{
if ($proceedingJoinPoint->methodName == 'getInstance') {
$key = $proceedingJoinPoint->className;
Expand Down
6 changes: 4 additions & 2 deletions src/BootApplicationListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
use Sentry\State\Hub;
use Sentry\State\HubInterface;

use const BASE_PATH;

class BootApplicationListener implements ListenerInterface
{
/**
Expand Down Expand Up @@ -46,7 +48,7 @@ public function listen(): array
* complete before the event is returned to the EventDispatcher.
* @param AfterWorkerStart|object $event
*/
public function process(object $event)
public function process(object $event): void
{
$this->configureAndRegisterClient();
}
Expand All @@ -56,7 +58,7 @@ protected function configureAndRegisterClient()
$this->container->define(ClientBuilderInterface::class, function () {
$userConfig = $this->getUserConfig();

$basePath = defined('\BASE_PATH') ? \BASE_PATH : '';
$basePath = defined('\BASE_PATH') ? BASE_PATH : '';

unset($userConfig['breadcrumbs']);

Expand Down
31 changes: 13 additions & 18 deletions src/EventHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Minbaby\HyperfSentry;

use Exception;
use Hyperf\Contract\ConfigInterface;
use Hyperf\Database\Events\ConnectionEvent;
use Hyperf\Database\Events\QueryExecuted;
Expand All @@ -13,26 +14,23 @@
use Hyperf\Event\ListenerProvider;
use Psr\Container\ContainerInterface;
use Psr\EventDispatcher\ListenerProviderInterface;
use RuntimeException;
use Sentry\Breadcrumb;

class EventHandler
{
protected static $eventHandlerMap = [
protected static array $eventHandlerMap = [
QueryExecuted::class => 'queryExecuted',
TransactionBeginning::class => 'transaction',
TransactionCommitted::class => 'transaction',
TransactionRolledBack::class => 'transaction',
];

protected $container;
protected ContainerInterface $container;

/** @var ListenerProvider */
protected $event;
protected ListenerProvider $event;

/**
* @var array
*/
protected $config;
protected array $config;

public function __construct(ContainerInterface $container)
{
Expand All @@ -43,40 +41,37 @@ public function __construct(ContainerInterface $container)

/**
* Pass through the event and capture any errors.
*
* @param string $method
* @param array $arguments
*/
public function __call($method, $arguments)
public function __call(string $method, array $arguments)
{
$handlerMethod = "{$method}Handler";

if (! method_exists($this, $handlerMethod)) {
throw new \RuntimeException("Missing event handler: {$handlerMethod}");
throw new RuntimeException("Missing event handler: {$handlerMethod}");
}

try {
call_user_func_array([$this, $handlerMethod], $arguments);
} catch (\Exception $exception) {
} catch (Exception $exception) {
// Ignore
}
}

public function subscribe()
public function subscribe(): void
{
foreach (self::$eventHandlerMap as $event => $handler) {
$this->event->on($event, [$this, $handler]);
}
}

public function subscribeQueueEvents()
public function subscribeQueueEvents(): void
{
}

/**
* @param ConnectionEvent|object $event
*/
protected function transactionHandler(object $event)
protected function transactionHandler(object $event): void
{
$data = [
'connectionName' => $event->connectionName,
Expand All @@ -94,7 +89,7 @@ protected function transactionHandler(object $event)
/**
* @param object|QueryExecuted $event
*/
protected function queryExecutedHandler(object $event)
protected function queryExecutedHandler(object $event): void
{
if (! data_get($this->config, 'breadcrumbs.sql_queries', false)) {
return;
Expand Down
6 changes: 2 additions & 4 deletions src/Integration.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,13 @@
use Sentry\SentrySdk;
use Sentry\State\Scope;
use Sentry\Tracing\Span;

use function Sentry\addBreadcrumb;
use function Sentry\configureScope;

class Integration implements IntegrationInterface
{
/**
* @var null|string
*/
private static $transaction;
private static ?string $transaction;

/**
* {@inheritdoc}
Expand Down
5 changes: 1 addition & 4 deletions src/Integration/RequestFetcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,7 @@

class RequestFetcher implements RequestFetcherInterface
{
/**
* @var \Psr\Container\ContainerInterface
*/
private $container;
private ContainerInterface $container;

public function __construct(ContainerInterface $container)
{
Expand Down
2 changes: 1 addition & 1 deletion src/SentryExceptionHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

class SentryExceptionHandler extends ExceptionHandler
{
protected $container;
protected ContainerInterface $container;

public function __construct(ContainerInterface $container)
{
Expand Down
10 changes: 1 addition & 9 deletions src/TestCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,7 @@
*/
class TestCommand extends HyperfCommand
{
/**
* @var ContainerInterface
*/
protected $container;
protected ContainerInterface $container;

public function __construct(ContainerInterface $container)
{
Expand Down Expand Up @@ -103,11 +100,6 @@ protected function option($name)

/**
* Generate a test exception to send to Sentry.
*
* @param $command
* @param $arg
*
* @return \Exception
*/
protected function generateTestException($command, $arg): ?Exception
{
Expand Down
2 changes: 1 addition & 1 deletion src/Version.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ final class Version
{
public const SDK_IDENTIFIER = 'sentry.php.hyperf';

public const SDK_VERSION = '2.2.0';
public const SDK_VERSION = '3.0.0';
}

0 comments on commit 4ba5ac6

Please sign in to comment.