Skip to content

Commit

Permalink
Deprecate the methods on the HubInterface to get and set the current …
Browse files Browse the repository at this point in the history
…hub (#847)
  • Loading branch information
ste93cry authored Sep 18, 2019
1 parent 65edd2d commit e0b6e8c
Show file tree
Hide file tree
Showing 26 changed files with 390 additions and 112 deletions.
4 changes: 3 additions & 1 deletion .php_cs → .php_cs.dist
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ return PhpCsFixer\Config::create()
'@Symfony:risky' => true,
'array_syntax' => ['syntax' => 'short'],
'concat_space' => ['spacing' => 'one'],
'ordered_imports' => true,
'ordered_imports' => [
'imports_order' => ['class', 'function', 'const'],
],
'declare_strict_types' => true,
'psr0' => true,
'psr4' => true,
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
- Support force sending events on-demand and fix sending of events in long-running processes (#813)
- Update PHPStan and introduce Psalm (#846)
- Add an integration to set the transaction attribute of the event (#865)
- Deprecate `Hub::getCurrent` and `Hub::setCurrent` methods to set the current hub instance (#847)

## 2.1.2 (2019-08-22)

Expand Down
7 changes: 2 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
},
"autoload": {
"files": [
"src/Sdk.php"
"src/functions.php"
],
"psr-4" : {
"Sentry\\" : "src/"
Expand All @@ -59,11 +59,8 @@
"tests": [
"vendor/bin/phpunit --verbose"
],
"tests-report": [
"vendor/bin/phpunit --verbose --configuration phpunit.xml.dist --coverage-html tests/html-report"
],
"phpcs": [
"vendor/bin/php-cs-fixer fix --config=.php_cs --verbose --diff --dry-run"
"vendor/bin/php-cs-fixer fix --verbose --diff --dry-run"
],
"phpstan": [
"vendor/bin/phpstan analyse"
Expand Down
4 changes: 2 additions & 2 deletions src/Integration/ErrorListenerIntegration.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use Sentry\Exception\FatalErrorException;
use Sentry\Exception\SilencedErrorException;
use Sentry\Options;
use Sentry\State\Hub;
use Sentry\SentrySdk;

/**
* This integration hooks into the global error handlers and emits events to
Expand Down Expand Up @@ -58,7 +58,7 @@ public function setupOnce(): void
return;
}

$currentHub = Hub::getCurrent();
$currentHub = SentrySdk::getCurrentHub();
$integration = $currentHub->getIntegration(self::class);
$client = $currentHub->getClient();

Expand Down
4 changes: 2 additions & 2 deletions src/Integration/ExceptionListenerIntegration.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace Sentry\Integration;

use Sentry\ErrorHandler;
use Sentry\State\Hub;
use Sentry\SentrySdk;

/**
* This integration hooks into the global error handlers and emits events to
Expand All @@ -21,7 +21,7 @@ public function setupOnce(): void
/** @psalm-suppress DeprecatedMethod */
$errorHandler = ErrorHandler::registerOnce(ErrorHandler::DEFAULT_RESERVED_MEMORY_SIZE, false);
$errorHandler->addExceptionHandlerListener(static function (\Throwable $exception): void {
$currentHub = Hub::getCurrent();
$currentHub = SentrySdk::getCurrentHub();
$integration = $currentHub->getIntegration(self::class);

// The client bound to the current hub, if any, could not have this
Expand Down
4 changes: 2 additions & 2 deletions src/Integration/FatalErrorListenerIntegration.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use Sentry\ErrorHandler;
use Sentry\Exception\FatalErrorException;
use Sentry\Options;
use Sentry\State\Hub;
use Sentry\SentrySdk;

/**
* This integration hooks into the error handler and captures fatal errors.
Expand Down Expand Up @@ -42,7 +42,7 @@ public function setupOnce(): void
{
$errorHandler = ErrorHandler::registerOnceFatalErrorHandler();
$errorHandler->addFatalErrorHandlerListener(function (FatalErrorException $exception): void {
$currentHub = Hub::getCurrent();
$currentHub = SentrySdk::getCurrentHub();
$integration = $currentHub->getIntegration(self::class);
$client = $currentHub->getClient();

Expand Down
4 changes: 2 additions & 2 deletions src/Integration/ModulesIntegration.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use Jean85\PrettyVersions;
use PackageVersions\Versions;
use Sentry\Event;
use Sentry\State\Hub;
use Sentry\SentrySdk;
use Sentry\State\Scope;

/**
Expand All @@ -27,7 +27,7 @@ final class ModulesIntegration implements IntegrationInterface
public function setupOnce(): void
{
Scope::addGlobalEventProcessor(function (Event $event) {
$integration = Hub::getCurrent()->getIntegration(self::class);
$integration = SentrySdk::getCurrentHub()->getIntegration(self::class);

// The integration could be bound to a client that is not the one
// attached to the current hub. If this is the case, bail out
Expand Down
4 changes: 2 additions & 2 deletions src/Integration/RequestIntegration.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use Sentry\Event;
use Sentry\Exception\JsonException;
use Sentry\Options;
use Sentry\State\Hub;
use Sentry\SentrySdk;
use Sentry\State\Scope;
use Sentry\Util\JSON;
use Zend\Diactoros\ServerRequestFactory;
Expand Down Expand Up @@ -61,7 +61,7 @@ public function __construct(?Options $options = null)
public function setupOnce(): void
{
Scope::addGlobalEventProcessor(function (Event $event): Event {
$currentHub = Hub::getCurrent();
$currentHub = SentrySdk::getCurrentHub();
$integration = $currentHub->getIntegration(self::class);
$client = $currentHub->getClient();

Expand Down
5 changes: 2 additions & 3 deletions src/Integration/TransactionIntegration.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace Sentry\Integration;

use Sentry\Event;
use Sentry\State\Hub;
use Sentry\SentrySdk;
use Sentry\State\Scope;

/**
Expand All @@ -23,8 +23,7 @@ final class TransactionIntegration implements IntegrationInterface
public function setupOnce(): void
{
Scope::addGlobalEventProcessor(static function (Event $event, array $payload): Event {
$currentHub = Hub::getCurrent();
$integration = $currentHub->getIntegration(self::class);
$integration = SentrySdk::getCurrentHub()->getIntegration(self::class);

// The client bound to the current hub, if any, could not have this
// integration enabled. If this is the case, bail out
Expand Down
70 changes: 70 additions & 0 deletions src/SentrySdk.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

declare(strict_types=1);

namespace Sentry;

use Sentry\State\Hub;
use Sentry\State\HubInterface;

/**
* This class is the main entry point for all the most common SDK features.
*
* @author Stefano Arlandini <sarlandini@alice.it>
*/
final class SentrySdk
{
/**
* @var HubInterface|null The current hub
*/
private static $currentHub;

/**
* Constructor.
*/
private function __construct()
{
}

/**
* Initializes the SDK by creating a new hub instance each time this method
* gets called.
*
* @return HubInterface
*/
public static function init(): HubInterface
{
self::$currentHub = new Hub();

return self::$currentHub;
}

/**
* Gets the current hub. If it's not initialized then creates a new instance
* and sets it as current hub.
*
* @return HubInterface
*/
public static function getCurrentHub(): HubInterface
{
if (null === self::$currentHub) {
self::$currentHub = new Hub();
}

return self::$currentHub;
}

/**
* Sets the current hub.
*
* @param HubInterface $hub The hub to set
*
* @return HubInterface
*/
public static function setCurrentHub(HubInterface $hub): HubInterface
{
self::$currentHub = $hub;

return $hub;
}
}
22 changes: 7 additions & 15 deletions src/State/Hub.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Sentry\Breadcrumb;
use Sentry\ClientInterface;
use Sentry\Integration\IntegrationInterface;
use Sentry\SentrySdk;
use Sentry\Severity;

/**
Expand All @@ -24,11 +25,6 @@ final class Hub implements HubInterface
*/
private $lastEventId;

/**
* @var HubInterface|null The hub that is set as the current one
*/
private static $currentHub;

/**
* Hub constructor.
*
Expand All @@ -37,11 +33,7 @@ final class Hub implements HubInterface
*/
public function __construct(?ClientInterface $client = null, ?Scope $scope = null)
{
if (null === $scope) {
$scope = new Scope();
}

$this->stack[] = new Layer($client, $scope);
$this->stack[] = new Layer($client, $scope ?? new Scope());
}

/**
Expand Down Expand Up @@ -204,19 +196,19 @@ public function addBreadcrumb(Breadcrumb $breadcrumb): bool
*/
public static function getCurrent(): HubInterface
{
if (null === self::$currentHub) {
self::$currentHub = new self();
}
@trigger_error(sprintf('The %s() method is deprecated since version 2.2 and will be removed in 3.0. Use SentrySdk::getCurrentHub() instead.', __METHOD__), E_USER_DEPRECATED);

return self::$currentHub;
return SentrySdk::getCurrentHub();
}

/**
* {@inheritdoc}
*/
public static function setCurrent(HubInterface $hub): HubInterface
{
self::$currentHub = $hub;
@trigger_error(sprintf('The %s() method is deprecated since version 2.2 and will be removed in 3.0. Use SentrySdk::setCurrentHub() instead.', __METHOD__), E_USER_DEPRECATED);

SentrySdk::setCurrentHub($hub);

return $hub;
}
Expand Down
Loading

0 comments on commit e0b6e8c

Please sign in to comment.