| 1 |
<?php |
| 2 |
|
| 3 |
declare (strict_types=1); |
| 4 |
namespace WCPOS\Vendor\Sentry; |
| 5 |
|
| 6 |
use WCPOS\Vendor\Sentry\Logs\Logs; |
| 7 |
use WCPOS\Vendor\Sentry\Metrics\TraceMetrics; |
| 8 |
use WCPOS\Vendor\Sentry\State\Hub; |
| 9 |
use WCPOS\Vendor\Sentry\State\HubInterface; |
| 10 |
use WCPOS\Vendor\Sentry\State\RuntimeContext; |
| 11 |
use WCPOS\Vendor\Sentry\State\RuntimeContextManager; |
| 12 |
/** |
| 13 |
* This class is the main entry point for all the most common SDK features. |
| 14 |
* |
| 15 |
* @author Stefano Arlandini <[email protected]> |
| 16 |
*/ |
| 17 |
final class SentrySdk |
| 18 |
{ |
| 19 |
/** |
| 20 |
* @var HubInterface|null The baseline hub |
| 21 |
*/ |
| 22 |
private static $currentHub; |
| 23 |
/** |
| 24 |
* @var RuntimeContextManager|null |
| 25 |
*/ |
| 26 |
private static $runtimeContextManager; |
| 27 |
/** |
| 28 |
* Constructor. |
| 29 |
*/ |
| 30 |
private function __construct() |
| 31 |
{ |
| 32 |
} |
| 33 |
/** |
| 34 |
* Initializes the SDK by creating a new hub instance each time this method |
| 35 |
* gets called. |
| 36 |
*/ |
| 37 |
public static function init() : HubInterface |
| 38 |
{ |
| 39 |
self::$currentHub = new Hub(); |
| 40 |
self::$runtimeContextManager = new RuntimeContextManager(self::$currentHub); |
| 41 |
return self::getCurrentHub(); |
| 42 |
} |
| 43 |
/** |
| 44 |
* Gets the current hub. If it's not initialized then creates a new instance |
| 45 |
* and sets it as current hub. |
| 46 |
*/ |
| 47 |
public static function getCurrentHub() : HubInterface |
| 48 |
{ |
| 49 |
return self::getRuntimeContextManager()->getCurrentHub(); |
| 50 |
} |
| 51 |
/** |
| 52 |
* Sets the current hub. |
| 53 |
* |
| 54 |
* If called while an explicit runtime context is active, the hub update is |
| 55 |
* scoped to that active context only. Otherwise, it updates the baseline |
| 56 |
* hub used by the global fallback context and future contexts. |
| 57 |
* |
| 58 |
* @param HubInterface $hub The hub to set |
| 59 |
*/ |
| 60 |
public static function setCurrentHub(HubInterface $hub) : HubInterface |
| 61 |
{ |
| 62 |
$wasSetOnActiveRuntimeContext = self::getRuntimeContextManager()->setCurrentHub($hub); |
| 63 |
if (!$wasSetOnActiveRuntimeContext) { |
| 64 |
self::$currentHub = $hub; |
| 65 |
} |
| 66 |
return $hub; |
| 67 |
} |
| 68 |
public static function startContext() : void |
| 69 |
{ |
| 70 |
self::getRuntimeContextManager()->startContext(); |
| 71 |
} |
| 72 |
public static function endContext(?int $timeout = null) : void |
| 73 |
{ |
| 74 |
self::getRuntimeContextManager()->endContext($timeout); |
| 75 |
} |
| 76 |
/** |
| 77 |
* Executes the given callback within an isolated context. |
| 78 |
* |
| 79 |
* If a context is already active for the current execution key, this method |
| 80 |
* reuses it and only executes the callback. |
| 81 |
* |
| 82 |
* @param callable $callback The callback to execute |
| 83 |
* |
| 84 |
* @phpstan-template T |
| 85 |
* |
| 86 |
* @phpstan-param callable(): T $callback |
| 87 |
* |
| 88 |
* @return mixed |
| 89 |
* |
| 90 |
* @phpstan-return T |
| 91 |
*/ |
| 92 |
public static function withContext(callable $callback, ?int $timeout = null) |
| 93 |
{ |
| 94 |
$runtimeContextManager = self::getRuntimeContextManager(); |
| 95 |
$startedNewContext = !$runtimeContextManager->hasActiveContext(); |
| 96 |
if ($startedNewContext) { |
| 97 |
$runtimeContextManager->startContext(); |
| 98 |
} |
| 99 |
try { |
| 100 |
return $callback(); |
| 101 |
} finally { |
| 102 |
if ($startedNewContext) { |
| 103 |
$runtimeContextManager->endContext($timeout); |
| 104 |
} |
| 105 |
} |
| 106 |
} |
| 107 |
/** |
| 108 |
* Gets the current runtime-local context. |
| 109 |
* |
| 110 |
* @internal |
| 111 |
*/ |
| 112 |
public static function getCurrentRuntimeContext() : RuntimeContext |
| 113 |
{ |
| 114 |
return self::getRuntimeContextManager()->getCurrentContext(); |
| 115 |
} |
| 116 |
/** |
| 117 |
* Flushes all buffered telemetry data. |
| 118 |
* |
| 119 |
* This is a convenience facade that forwards the flush operation to all |
| 120 |
* internally managed components. |
| 121 |
* |
| 122 |
* Calling this method is equivalent to invoking `flush()` on each component |
| 123 |
* individually. It does not change flushing behavior, improve performance, |
| 124 |
* or reduce the number of network requests. |
| 125 |
*/ |
| 126 |
public static function flush() : void |
| 127 |
{ |
| 128 |
Logs::getInstance()->flush(); |
| 129 |
TraceMetrics::getInstance()->flush(); |
| 130 |
$client = self::getCurrentHub()->getClient(); |
| 131 |
if ($client !== null) { |
| 132 |
$client->flush(); |
| 133 |
} |
| 134 |
} |
| 135 |
private static function getRuntimeContextManager() : RuntimeContextManager |
| 136 |
{ |
| 137 |
if (self::$currentHub === null) { |
| 138 |
self::$currentHub = new Hub(); |
| 139 |
} |
| 140 |
if (self::$runtimeContextManager === null) { |
| 141 |
self::$runtimeContextManager = new RuntimeContextManager(self::$currentHub); |
| 142 |
} |
| 143 |
return self::$runtimeContextManager; |
| 144 |
} |
| 145 |
} |
| 146 |
|