From c5dad4f8909d6558e1dec5adeaea6d2aed32b901 Mon Sep 17 00:00:00 2001 From: Sebastiaan Kloos Date: Tue, 7 Nov 2023 14:19:24 +0100 Subject: [PATCH 1/6] adding support for calling functions before doing a ray request --- src/Ray.php | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/Ray.php b/src/Ray.php index 26091cc..435d8b9 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 ?Closure $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, $meta); + } + 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): void + { + static::$beforeSendRequest = $closure; + } } From fb8d01fe8668dd3667d27f0a6ffca7413cb16893 Mon Sep 17 00:00:00 2001 From: Sebastiaan Kloos Date: Tue, 7 Nov 2023 14:23:54 +0100 Subject: [PATCH 2/6] fixed issue --- src/Ray.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Ray.php b/src/Ray.php index 435d8b9..33310fe 100644 --- a/src/Ray.php +++ b/src/Ray.php @@ -105,7 +105,7 @@ class Ray public static $projectName = ''; /** @var Closure|null */ - public static ?Closure $beforeSendRequest = null; + public static null|Closure $beforeSendRequest = null; public static function create(Client $client = null, string $uuid = null): self { From 3728a9dc7c44350ed3dd205f7f8253f97999501e Mon Sep 17 00:00:00 2001 From: Sebastiaan Kloos Date: Tue, 7 Nov 2023 14:25:08 +0100 Subject: [PATCH 3/6] added option to disable after --- src/Ray.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Ray.php b/src/Ray.php index 33310fe..b673f5d 100644 --- a/src/Ray.php +++ b/src/Ray.php @@ -859,7 +859,7 @@ protected function notifyWhenRateLimitReached(): void self::rateLimiter()->notify(); } - public static function beforeSendRequest(Closure $closure): void + public static function beforeSendRequest(null|Closure $closure): void { static::$beforeSendRequest = $closure; } From 6472c7a8f7350aa0a7044660eacf04e42c5d20fe Mon Sep 17 00:00:00 2001 From: Sebastiaan Kloos Date: Tue, 7 Nov 2023 14:42:54 +0100 Subject: [PATCH 4/6] added test to test if closure was called before sending request --- src/Ray.php | 2 +- tests/RayTest.php | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/src/Ray.php b/src/Ray.php index b673f5d..f79e72e 100644 --- a/src/Ray.php +++ b/src/Ray.php @@ -817,7 +817,7 @@ public function sendRequest($payloads, array $meta = []): self ], $meta); if ($closure = static::$beforeSendRequest) { - $closure($payloads, $meta); + $closure($payloads, $allMeta); } foreach ($payloads as $payload) { 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(); +}); From 934e0ae18ebb97d036c2c67e978f2efa842c2c0a Mon Sep 17 00:00:00 2001 From: Sebastiaan Kloos Date: Tue, 7 Nov 2023 14:44:04 +0100 Subject: [PATCH 5/6] fixed styling issues --- src/Ray.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Ray.php b/src/Ray.php index f79e72e..e5be758 100644 --- a/src/Ray.php +++ b/src/Ray.php @@ -105,7 +105,7 @@ class Ray public static $projectName = ''; /** @var Closure|null */ - public static null|Closure $beforeSendRequest = null; + public static Closure|null $beforeSendRequest = null; public static function create(Client $client = null, string $uuid = null): self { @@ -859,7 +859,7 @@ protected function notifyWhenRateLimitReached(): void self::rateLimiter()->notify(); } - public static function beforeSendRequest(null|Closure $closure): void + public static function beforeSendRequest(Closure|null $closure): void { static::$beforeSendRequest = $closure; } From 7b94013ae8ec9d24af23525281fc2c28890ba945 Mon Sep 17 00:00:00 2001 From: Sebastiaan Kloos Date: Tue, 7 Nov 2023 14:47:17 +0100 Subject: [PATCH 6/6] fixed styling issues --- src/Ray.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Ray.php b/src/Ray.php index e5be758..c0fb957 100644 --- a/src/Ray.php +++ b/src/Ray.php @@ -105,7 +105,7 @@ class Ray public static $projectName = ''; /** @var Closure|null */ - public static Closure|null $beforeSendRequest = null; + public static $beforeSendRequest = null; public static function create(Client $client = null, string $uuid = null): self { @@ -859,7 +859,7 @@ protected function notifyWhenRateLimitReached(): void self::rateLimiter()->notify(); } - public static function beforeSendRequest(Closure|null $closure): void + public static function beforeSendRequest(?Closure $closure = null): void { static::$beforeSendRequest = $closure; }