Skip to content

Commit

Permalink
✨ Add async macros to common Facade methods (#89)
Browse files Browse the repository at this point in the history
* ✨ Add async macros for common methods to the `Cache`, `Http`, `Storage` and `File` facades
* ✨ Create an `CanAsync` trait to easily perform async tasks in the event loop
  • Loading branch information
Log1x authored Jun 11, 2024
1 parent 88f45bf commit 9feeba3
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 0 deletions.
38 changes: 38 additions & 0 deletions src/Concerns/CanAsync.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace Laracord\Concerns;

use Exception;
use React\Promise\Promise;

trait CanAsync
{
/**
* Perform an asynchronous operation.
*/
public static function handleAsync(callable $callback): Promise
{
return new Promise(function ($resolve, $reject) use ($callback) {
if (! $loop = app('bot')?->getLoop()) {
throw new Exception('The Laracord event loop is not available.');
}

$loop->futureTick(function () use ($callback, $resolve, $reject) {
try {
$result = $callback();
$resolve($result);
} catch (Exception $e) {
$reject($e);
}
});
});
}

/**
* Perform an asynchronous operation.
*/
public function async(callable $callback): Promise
{
return static::async($callback);
}
}
3 changes: 3 additions & 0 deletions src/Laracord.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Illuminate\Support\Str;
use Laracord\Commands\Command;
use Laracord\Commands\SlashCommand;
use Laracord\Concerns\CanAsync;
use Laracord\Console\Commands\Command as ConsoleCommand;
use Laracord\Discord\Message;
use Laracord\Events\Event;
Expand All @@ -35,6 +36,8 @@

class Laracord
{
use CanAsync;

/**
* The event loop.
*/
Expand Down
26 changes: 26 additions & 0 deletions src/LaracordServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
namespace Laracord;

use Illuminate\Contracts\Http\Kernel as KernelContract;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\ServiceProvider;
use Laracord\Http\Kernel;
use LaravelZero\Framework\Components\Database\Provider as DatabaseProvider;
Expand Down Expand Up @@ -67,6 +71,8 @@ public function boot()
Console\Commands\ServiceMakeCommand::class,
Console\Commands\TokenMakeCommand::class,
]);

$this->registerMacros();
}

/**
Expand Down Expand Up @@ -123,4 +129,24 @@ protected function registerDatabase(): void
$this->app->booting(fn () => $this->app->register(DatabaseProvider::class));
}
}

/**
* Register the macros.
*/
protected function registerMacros(): void
{
Cache::macro('getAsync', fn (string $key) => Laracord::handleAsync(fn () => Cache::get($key)));
Cache::macro('putAsync', fn (string $key, mixed $value, int $seconds) => Laracord::handleAsync(fn () => Cache::put($key, $value, $seconds)));
Cache::macro('rememberAsync', fn (string $key, int $seconds, callable $callback) => Laracord::handleAsync(fn () => Cache::remember($key, $seconds, $callback)));
Cache::macro('rememberForeverAsync', fn (string $key, callable $callback) => Laracord::handleAsync(fn () => Cache::rememberForever($key, $callback)));

Http::macro('getAsync', fn (string $url, array|string|null $query = []) => Laracord::handleAsync(fn () => Http::get($url, $query)));
Http::macro('postAsync', fn (string $url, array $data = []) => Laracord::handleAsync(fn () => Http::post($url, $data = [])));

File::macro('getAsync', fn (string $path) => Laracord::handleAsync(fn () => File::get($path)));
File::macro('putAsync', fn (string $path, mixed $contents) => Laracord::handleAsync(fn () => File::put($path, $contents)));

Storage::macro('getAsync', fn (string $path) => Laracord::handleAsync(fn () => Storage::get($path)));
Storage::macro('putAsync', fn (string $path, mixed $contents) => Laracord::handleAsync(fn () => Storage::put($path, $contents)));
}
}

0 comments on commit 9feeba3

Please sign in to comment.