1. Go to this page and download the library: Download gdbots/pbjx 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/ */
gdbots / pbjx example snippets
interface Router
{
public function forCommand(Message $command): string;
...
}
declare(strict_types = 1);
final class PublishArticleHandler implements CommandHandler
{
protected function handleCommand(Message $command, Pbjx $pbjx): void
{
// handle the command here
}
}
declare(strict_types = 1);
final class ArticleController extends Controller
{
/**
* @Route("/articles/{article_id}/publish", * @return Response
*/
public function publishAction(Request $request): Response
{
$command = PublishArticleV1::create()->set('article_id', $request->attributes->get('article_id'));
$this->getPbjx()->send($command);
$this->addFlash('success', 'Article was published');
return $this->redirectToRoute('app_article_index');
}
}
declare(strict_types = 1);
namespace Acme\Blog;
use Gdbots\Pbjx\EventSubscriber;
final class MyEventSubscriber implements EventSubscriber
{
public function onArticlePublished(Message $event, Pbjx $pbjx): void
{
// do something with this event.
}
public static function getSubscribedEvents()
{
return [
'acme:blog:event:article-published' => 'onArticlePublished',
];
}
}
declare(strict_types = 1);
final class ArticleController extends Controller
{
/**
* @Route("/articles/{article_id}", *
* @return Response
*/
public function getAction(Request $request): Response
{
$getArticleRequest = GetArticleRequestV1::create()->set('article_id', $request->attributes->get('article_id'));
$getArticleResponse = $this->getPbjx()->request($getArticleRequest);
return $this->render('article.html.twig', ['article' => $getArticleResponse->get('article')]);
}
}
declare(strict_types = 1);
final class PublishArticleHandler implements CommandHandler
{
protected function handleCommand(Message $command, Pbjx $pbjx): void
{
// in this example it's ultra basic, create the event and push it to a stream
$event = ArticlePublishedV1::create()->set('article_id', $command->get('article_id'));
// copies contextual data from the previous message (ctx_* fields)
$pbjx->copyContext($command, $event);
$streamId = StreamId::fromString(sprintf('acme:article:%s', $command->get('article_id')));
$pbjx->getEventStore()->putEvents($streamId, [$event]);
// after the event is persisted it will be published either via a
// two phase commit or a publisher reading the EventStore streams
// (DynamoDb streams for example)
}
}