Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding support for calling closures before doing a ray request #859

Merged
merged 6 commits into from
Nov 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
});