1. Go to this page and download the library: Download waffle-commons/async library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
waffle-commons / async example snippets
use Psr\Log\LoggerInterface;
use Waffle\Commons\Async\DeferredTaskRunner;
use Waffle\Commons\Contracts\Async\DeferredTaskInterface;
use Waffle\Commons\Contracts\Async\TaskRunnerInterface;
// 1. A self-contained, worker-safe task.
final readonly class SendReceiptTask implements DeferredTaskInterface
{
public function __construct(
private LoggerInterface $logger,
private string $orderId,
) {}
#[\Override]
public function run(): void
{
// short post-response work: mail / webhook / audit write
$this->logger->info("receipt sent for {$this->orderId}");
}
#[\Override]
public function name(): string
{
return 'order.receipt';
}
}
// 2. Registered once per worker iteration — the kernel does this (see "Wiring" below).
$runner = new DeferredTaskRunner(budget: DeferredTaskRunner::DEFAULT_BUDGET, logger: $logger);
// 3. In a handler — defer, return immediately, the kernel drains on TerminateEvent.
function placeOrder(TaskRunnerInterface $runner): ResponseInterface
{
$runner->defer(new SendReceiptTask($logger, $orderId)); // queued, not run yet
return jsonResponse(['pending' => $runner->pending()]);
// ── response flushed ── then run() executes SendReceiptTask in an isolated Fiber.
}
public const int DEFAULT_BUDGET = 64;
public function __construct(
int $budget = self::DEFAULT_BUDGET, // hard ceiling on tasks deferred per request (>= 1)
LoggerInterface $logger = new NullLogger(), // PSR-3; receives per-task failure/abandon logs
);
public function defer(DeferredTaskInterface $task): void; // throws DeferralBudgetExceededException at the ceiling
public function run(): void; // snapshot, clear, then drain each task in its own Fiber
public function pending(): int; // tasks currently queued this request
public function reset(): void; // worker reset: empty the pending queue
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.