vendor/sentry/sentry/src/State/Hub.php line 36

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Sentry\State;
  4. use Sentry\Breadcrumb;
  5. use Sentry\ClientInterface;
  6. use Sentry\Integration\IntegrationInterface;
  7. use Sentry\SentrySdk;
  8. use Sentry\Severity;
  9. /**
  10.  * This class is a basic implementation of the {@see HubInterface} interface.
  11.  */
  12. final class Hub implements HubInterface
  13. {
  14.     /**
  15.      * @var Layer[] The stack of client/scope pairs
  16.      */
  17.     private $stack = [];
  18.     /**
  19.      * @var string|null The ID of the last captured event
  20.      */
  21.     private $lastEventId;
  22.     /**
  23.      * Hub constructor.
  24.      *
  25.      * @param ClientInterface|null $client The client bound to the hub
  26.      * @param Scope|null           $scope  The scope bound to the hub
  27.      */
  28.     public function __construct(?ClientInterface $client null, ?Scope $scope null)
  29.     {
  30.         $this->stack[] = new Layer($client$scope ?? new Scope());
  31.     }
  32.     /**
  33.      * {@inheritdoc}
  34.      */
  35.     public function getClient(): ?ClientInterface
  36.     {
  37.         return $this->getStackTop()->getClient();
  38.     }
  39.     /**
  40.      * {@inheritdoc}
  41.      */
  42.     public function getLastEventId(): ?string
  43.     {
  44.         return $this->lastEventId;
  45.     }
  46.     /**
  47.      * {@inheritdoc}
  48.      */
  49.     public function pushScope(): Scope
  50.     {
  51.         $clonedScope = clone $this->getScope();
  52.         $this->stack[] = new Layer($this->getClient(), $clonedScope);
  53.         return $clonedScope;
  54.     }
  55.     /**
  56.      * {@inheritdoc}
  57.      */
  58.     public function popScope(): bool
  59.     {
  60.         if (=== \count($this->stack)) {
  61.             return false;
  62.         }
  63.         return null !== array_pop($this->stack);
  64.     }
  65.     /**
  66.      * {@inheritdoc}
  67.      */
  68.     public function withScope(callable $callback): void
  69.     {
  70.         $scope $this->pushScope();
  71.         try {
  72.             $callback($scope);
  73.         } finally {
  74.             $this->popScope();
  75.         }
  76.     }
  77.     /**
  78.      * {@inheritdoc}
  79.      */
  80.     public function configureScope(callable $callback): void
  81.     {
  82.         $callback($this->getScope());
  83.     }
  84.     /**
  85.      * {@inheritdoc}
  86.      */
  87.     public function bindClient(ClientInterface $client): void
  88.     {
  89.         $layer $this->getStackTop();
  90.         $layer->setClient($client);
  91.     }
  92.     /**
  93.      * {@inheritdoc}
  94.      */
  95.     public function captureMessage(string $message, ?Severity $level null): ?string
  96.     {
  97.         $client $this->getClient();
  98.         if (null !== $client) {
  99.             return $this->lastEventId $client->captureMessage($message$level$this->getScope());
  100.         }
  101.         return null;
  102.     }
  103.     /**
  104.      * {@inheritdoc}
  105.      */
  106.     public function captureException(\Throwable $exception): ?string
  107.     {
  108.         $client $this->getClient();
  109.         if (null !== $client) {
  110.             return $this->lastEventId $client->captureException($exception$this->getScope());
  111.         }
  112.         return null;
  113.     }
  114.     /**
  115.      * {@inheritdoc}
  116.      */
  117.     public function captureEvent(array $payload): ?string
  118.     {
  119.         $client $this->getClient();
  120.         if (null !== $client) {
  121.             return $this->lastEventId $client->captureEvent($payload$this->getScope());
  122.         }
  123.         return null;
  124.     }
  125.     /**
  126.      * {@inheritdoc}
  127.      */
  128.     public function captureLastError(): ?string
  129.     {
  130.         $client $this->getClient();
  131.         if (null !== $client) {
  132.             return $this->lastEventId $client->captureLastError($this->getScope());
  133.         }
  134.         return null;
  135.     }
  136.     /**
  137.      * {@inheritdoc}
  138.      */
  139.     public function addBreadcrumb(Breadcrumb $breadcrumb): bool
  140.     {
  141.         $client $this->getClient();
  142.         if (null === $client) {
  143.             return false;
  144.         }
  145.         $options $client->getOptions();
  146.         $beforeBreadcrumbCallback $options->getBeforeBreadcrumbCallback();
  147.         $maxBreadcrumbs $options->getMaxBreadcrumbs();
  148.         if ($maxBreadcrumbs <= 0) {
  149.             return false;
  150.         }
  151.         $breadcrumb $beforeBreadcrumbCallback($breadcrumb);
  152.         if (null !== $breadcrumb) {
  153.             $this->getScope()->addBreadcrumb($breadcrumb$maxBreadcrumbs);
  154.         }
  155.         return null !== $breadcrumb;
  156.     }
  157.     /**
  158.      * {@inheritdoc}
  159.      */
  160.     public static function getCurrent(): HubInterface
  161.     {
  162.         @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);
  163.         return SentrySdk::getCurrentHub();
  164.     }
  165.     /**
  166.      * {@inheritdoc}
  167.      */
  168.     public static function setCurrent(HubInterface $hub): HubInterface
  169.     {
  170.         @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);
  171.         SentrySdk::setCurrentHub($hub);
  172.         return $hub;
  173.     }
  174.     /**
  175.      * {@inheritdoc}
  176.      */
  177.     public function getIntegration(string $className): ?IntegrationInterface
  178.     {
  179.         $client $this->getClient();
  180.         if (null !== $client) {
  181.             return $client->getIntegration($className);
  182.         }
  183.         return null;
  184.     }
  185.     /**
  186.      * Gets the scope bound to the top of the stack.
  187.      */
  188.     private function getScope(): Scope
  189.     {
  190.         return $this->getStackTop()->getScope();
  191.     }
  192.     /**
  193.      * Gets the topmost client/layer pair in the stack.
  194.      */
  195.     private function getStackTop(): Layer
  196.     {
  197.         return $this->stack[\count($this->stack) - 1];
  198.     }
  199. }