diff --git a/src/Ray.php b/src/Ray.php index 26091cc..c0fb957 100644 --- a/src/Ray.php +++ b/src/Ray.php @@ -104,6 +104,9 @@ class Ray /** @var string */ public static $projectName = ''; + /** @var Closure|null */ + public static $beforeSendRequest = null; + public static function create(Client $client = null, string $uuid = null): self { $settings = SettingsFactory::createFromConfigFile(); @@ -813,6 +816,10 @@ public function sendRequest($payloads, array $meta = []): self 'project_name' => static::$projectName, ], $meta); + if ($closure = static::$beforeSendRequest) { + $closure($payloads, $allMeta); + } + foreach ($payloads as $payload) { $payload->remotePath = $this->settings->remote_path; $payload->localPath = $this->settings->local_path; @@ -851,4 +858,9 @@ protected function notifyWhenRateLimitReached(): void self::rateLimiter()->notify(); } + + public static function beforeSendRequest(?Closure $closure = null): void + { + static::$beforeSendRequest = $closure; + } } diff --git a/tests/RayTest.php b/tests/RayTest.php index c700e26..bfff3fd 100644 --- a/tests/RayTest.php +++ b/tests/RayTest.php @@ -1176,3 +1176,17 @@ function (InvalidArgumentException $e, $ray) { expect($sentRequests[0]['payloads'][0]['content']['level'])->toBe(999); }); + +it('can add a closure for before send and actually calls it', function () { + $this->closureCalled = false; + + $this->ray::beforeSendRequest(function () { + $this->closureCalled = true; + }); + + $this->ray->send(function ($ray) { + $ray->text('Hello world'); + }); + + expect($this->closureCalled)->toBeTrue(); +});