1. Go to this page and download the library: Download bear/defer 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/ */
bear / defer example snippets
use BEAR\Defer\Module\DeferModule;
protected function configure(): void
{
$this->install(new DeferModule(new YourHttpResponderModule()));
}
use BEAR\Defer\Attribute\Defer;
use BEAR\Resource\Annotation\Link;
use BEAR\Resource\ResourceObject;
class Article extends ResourceObject
{
public function __construct(
private readonly ArticleRepositoryInterface $articles,
) {
}
#[Defer(['publish', 'release-note'])]
#[Link(rel: 'publish', href: 'app://self/article/publish{?id}', method: 'post')]
#[Link(rel: 'release-note', href: 'app://self/release-note{?id}', method: 'post')]
public function onPost(string $title, string $body): static
{
$id = $this->articles->save($title, $body); // light work only
$this->code = 202; // Accepted
$this->body = ['id' => $id];
return $this; // no defer call in the body
}
}
class Publish extends ResourceObject
{
public function onPost(string $id): static
{
$this->indexer->index($id); // heavy work, runs after the response is sent
$this->notifier->notify($id);
return $this;
}
}
use BEAR\Defer\DeferInterface;
use BEAR\Resource\ResourceInterface;
use BEAR\Resource\ResourceObject;
class Article extends ResourceObject
{
public function __construct(
private readonly ResourceInterface $resource,
private readonly DeferInterface $defer,
) {
}
public function onPost(string $title, string $body, bool $publish = false): static
{
$id = $this->articles->save($title, $body);
$this->code = 202;
$this->body = ['id' => $id];
if ($publish) {
$request = $this->resource->post->uri('app://self/publish')->withQuery(['id' => $id]);
$this->defer->add($request);
}
return $this;
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.