Skip to content

Commit

Permalink
Add support for calling closures before doing a ray request (#859)
Browse files Browse the repository at this point in the history
* adding support for calling functions before doing a ray request

* fixed issue

* added option to disable after

* added test to test if closure was called before sending request

* fixed styling issues

* fixed styling issues
  • Loading branch information
SebastiaanKloos authored Nov 7, 2023
1 parent 2c2bccd commit af6c700
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/Ray.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -851,4 +858,9 @@ protected function notifyWhenRateLimitReached(): void

self::rateLimiter()->notify();
}

public static function beforeSendRequest(?Closure $closure = null): void
{
static::$beforeSendRequest = $closure;
}
}
14 changes: 14 additions & 0 deletions tests/RayTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});

0 comments on commit af6c700

Please sign in to comment.