spatie / ray

Debug with Ray to fix problems faster
https://myray.app
MIT License
554 stars 102 forks source link

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

Closed SebastiaanKloos closed 8 months ago

SebastiaanKloos commented 8 months ago

I want to call an Apple Script to start Ray if not already active before doing any requests to Ray.

With the PR you would be able to add this to your service provider

use Spatie\Ray\Ray;

Ray::beforeSendRequest(function () {
    $script = '
        set appBundleID to "be.spatie.ray"

        tell application "System Events"
            if not (exists (processes whose bundle identifier is appBundleID)) then
                tell application "Ray" to activate
                delay 1
            end if
        end tell
    ';

    $escapedScript = escapeshellarg($script);
    $command = "osascript -e $escapedScript";

    $output = shell_exec($command);
});
riasvdv commented 8 months ago

Can you add a test that verifies the closure gets called?

SebastiaanKloos commented 8 months ago

Can you add a test that verifies the closure gets called?

@riasvdv test added!

riasvdv commented 8 months ago

Thanks!

bhaidar commented 5 months ago

Hey @SebastiaanKloos I am getting an error that beforeSendRequest doesn't exist. I've already imported Ray class.

Any idea what's happening here?

  Method beforeSendRequest does not exist.

  at vendor/spatie/macroable/src/Macroable.php:40
     36▕ 
     37▕     public static function __callStatic($method, $parameters)
     38▕     {
     39▕         if (! static::hasMacro($method)) {
  ➜  40▕             throw new BadMethodCallException("Method {$method} does not exist.");
     41▕         }
     42▕ 
     43▕         $macro = static::$macros[$method];
     44▕