vendor/sentry/sentry/src/State/Scope.php line 70

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Sentry\State;
  4. use Sentry\Breadcrumb;
  5. use Sentry\Context\Context;
  6. use Sentry\Context\TagsContext;
  7. use Sentry\Context\UserContext;
  8. use Sentry\Event;
  9. use Sentry\Severity;
  10. /**
  11.  * The scope holds data that should implicitly be sent with Sentry events. It
  12.  * can hold context data, extra parameters, level overrides, fingerprints etc.
  13.  */
  14. final class Scope
  15. {
  16.     /**
  17.      * @var Breadcrumb[] The list of breadcrumbs recorded in this scope
  18.      */
  19.     private $breadcrumbs = [];
  20.     /**
  21.      * @var UserContext The user data associated to this scope
  22.      */
  23.     private $user;
  24.     /**
  25.      * @var array<string, array<string, mixed>> The list of contexts associated to this scope
  26.      */
  27.     private $contexts = [];
  28.     /**
  29.      * @var TagsContext The list of tags associated to this scope
  30.      */
  31.     private $tags;
  32.     /**
  33.      * @var Context<mixed> A set of extra data associated to this scope
  34.      */
  35.     private $extra;
  36.     /**
  37.      * @var string[] List of fingerprints used to group events together in
  38.      *               Sentry
  39.      */
  40.     private $fingerprint = [];
  41.     /**
  42.      * @var Severity|null The severity to associate to the events captured in
  43.      *                    this scope
  44.      */
  45.     private $level;
  46.     /**
  47.      * @var callable[] List of event processors
  48.      */
  49.     private $eventProcessors = [];
  50.     /**
  51.      * @var callable[] List of event processors
  52.      */
  53.     private static $globalEventProcessors = [];
  54.     /**
  55.      * Constructor.
  56.      */
  57.     public function __construct()
  58.     {
  59.         $this->user = new UserContext();
  60.         $this->tags = new TagsContext();
  61.         $this->extra = new Context();
  62.         $this->contexts = [];
  63.     }
  64.     /**
  65.      * Sets a new tag in the tags context.
  66.      *
  67.      * @param string $key   The key that uniquely identifies the tag
  68.      * @param string $value The value
  69.      *
  70.      * @return $this
  71.      */
  72.     public function setTag(string $keystring $value): self
  73.     {
  74.         $this->tags[$key] = $value;
  75.         return $this;
  76.     }
  77.     /**
  78.      * Merges the given tags into the current tags context.
  79.      *
  80.      * @param array<string, string> $tags The tags to merge into the current context
  81.      *
  82.      * @return $this
  83.      */
  84.     public function setTags(array $tags): self
  85.     {
  86.         $this->tags->merge($tags);
  87.         return $this;
  88.     }
  89.     /**
  90.      * Sets context data with the given name.
  91.      *
  92.      * @param string               $name  The name that uniquely identifies the context
  93.      * @param array<string, mixed> $value The value
  94.      *
  95.      * @return $this
  96.      */
  97.     public function setContext(string $name, array $value): self
  98.     {
  99.         $this->contexts[$name] = $value;
  100.         return $this;
  101.     }
  102.     /**
  103.      * Removes the context from the scope.
  104.      *
  105.      * @param string $name The name that uniquely identifies the context
  106.      *
  107.      * @return $this
  108.      */
  109.     public function removeContext(string $name): self
  110.     {
  111.         unset($this->contexts[$name]);
  112.         return $this;
  113.     }
  114.     /**
  115.      * Sets a new information in the extra context.
  116.      *
  117.      * @param string $key   The key that uniquely identifies the information
  118.      * @param mixed  $value The value
  119.      *
  120.      * @return $this
  121.      */
  122.     public function setExtra(string $key$value): self
  123.     {
  124.         $this->extra[$key] = $value;
  125.         return $this;
  126.     }
  127.     /**
  128.      * Merges the given data into the current extras context.
  129.      *
  130.      * @param array<string, mixed> $extras Data to merge into the current context
  131.      *
  132.      * @return $this
  133.      */
  134.     public function setExtras(array $extras): self
  135.     {
  136.         $this->extra->merge($extras);
  137.         return $this;
  138.     }
  139.     /**
  140.      * Sets the given data in the user context.
  141.      *
  142.      * @param array<string, mixed> $data  The data
  143.      * @param bool                 $merge If true, $data will be merged into user context instead of replacing it
  144.      *
  145.      * @return $this
  146.      */
  147.     public function setUser(array $databool $merge false): self
  148.     {
  149.         if ($merge) {
  150.             $this->user->merge($data);
  151.             return $this;
  152.         }
  153.         @trigger_error('Replacing the data is deprecated since version 2.3 and will stop working from version 3.0. Set the second argument to `true` to merge the data instead.'\E_USER_DEPRECATED);
  154.         $this->user->replaceData($data);
  155.         return $this;
  156.     }
  157.     /**
  158.      * Sets the list of strings used to dictate the deduplication of this event.
  159.      *
  160.      * @param string[] $fingerprint The fingerprint values
  161.      *
  162.      * @return $this
  163.      */
  164.     public function setFingerprint(array $fingerprint): self
  165.     {
  166.         $this->fingerprint $fingerprint;
  167.         return $this;
  168.     }
  169.     /**
  170.      * Sets the severity to apply to all events captured in this scope.
  171.      *
  172.      * @param Severity|null $level The severity
  173.      *
  174.      * @return $this
  175.      */
  176.     public function setLevel(?Severity $level): self
  177.     {
  178.         $this->level $level;
  179.         return $this;
  180.     }
  181.     /**
  182.      * Add the given breadcrumb to the scope.
  183.      *
  184.      * @param Breadcrumb $breadcrumb     The breadcrumb to add
  185.      * @param int        $maxBreadcrumbs The maximum number of breadcrumbs to record
  186.      *
  187.      * @return $this
  188.      */
  189.     public function addBreadcrumb(Breadcrumb $breadcrumbint $maxBreadcrumbs 100): self
  190.     {
  191.         $this->breadcrumbs[] = $breadcrumb;
  192.         $this->breadcrumbs \array_slice($this->breadcrumbs, -$maxBreadcrumbs);
  193.         return $this;
  194.     }
  195.     /**
  196.      * Clears all the breadcrumbs.
  197.      *
  198.      * @return $this
  199.      */
  200.     public function clearBreadcrumbs(): self
  201.     {
  202.         $this->breadcrumbs = [];
  203.         return $this;
  204.     }
  205.     /**
  206.      * Adds a new event processor that will be called after {@see Scope::applyToEvent}
  207.      * finished its work.
  208.      *
  209.      * @param callable $eventProcessor The event processor
  210.      *
  211.      * @return $this
  212.      */
  213.     public function addEventProcessor(callable $eventProcessor): self
  214.     {
  215.         $this->eventProcessors[] = $eventProcessor;
  216.         return $this;
  217.     }
  218.     /**
  219.      * Adds a new event processor that will be called after {@see Scope::applyToEvent}
  220.      * finished its work.
  221.      *
  222.      * @param callable $eventProcessor The event processor
  223.      */
  224.     public static function addGlobalEventProcessor(callable $eventProcessor): void
  225.     {
  226.         self::$globalEventProcessors[] = $eventProcessor;
  227.     }
  228.     /**
  229.      * Clears the scope and resets any data it contains.
  230.      *
  231.      * @return $this
  232.      */
  233.     public function clear(): self
  234.     {
  235.         $this->tags->clear();
  236.         $this->extra->clear();
  237.         $this->user->clear();
  238.         $this->level null;
  239.         $this->fingerprint = [];
  240.         $this->breadcrumbs = [];
  241.         $this->contexts = [];
  242.         return $this;
  243.     }
  244.     /**
  245.      * Applies the current context and fingerprint to the event. If the event has
  246.      * already some breadcrumbs on it, the ones from this scope won't get merged.
  247.      *
  248.      * @param Event                $event   The event object that will be enriched with scope data
  249.      * @param array<string, mixed> $payload The raw payload of the event that will be propagated to the event processors
  250.      */
  251.     public function applyToEvent(Event $event, array $payload): ?Event
  252.     {
  253.         if (empty($event->getFingerprint())) {
  254.             $event->setFingerprint($this->fingerprint);
  255.         }
  256.         if (empty($event->getBreadcrumbs())) {
  257.             $event->setBreadcrumb($this->breadcrumbs);
  258.         }
  259.         if (null !== $this->level) {
  260.             $event->setLevel($this->level);
  261.         }
  262.         $event->getTagsContext()->merge($this->tags->toArray());
  263.         $event->getExtraContext()->merge($this->extra->toArray());
  264.         $event->getUserContext()->merge($this->user->toArray());
  265.         foreach (array_merge($this->contexts$event->getContexts()) as $name => $data) {
  266.             $event->setContext($name$data);
  267.         }
  268.         foreach (array_merge(self::$globalEventProcessors$this->eventProcessors) as $processor) {
  269.             $event $processor($event$payload);
  270.             if (null === $event) {
  271.                 return null;
  272.             }
  273.             if (!$event instanceof Event) {
  274.                 throw new \InvalidArgumentException(sprintf('The event processor must return null or an instance of the %s class'Event::class));
  275.             }
  276.         }
  277.         return $event;
  278.     }
  279.     public function __clone()
  280.     {
  281.         $this->user = clone $this->user;
  282.         $this->tags = clone $this->tags;
  283.         $this->extra = clone $this->extra;
  284.     }
  285. }