1. Go to this page and download the library: Download gpslab/cqrs library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?phprequire_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
gpslab / cqrs example snippets
useGpsLab\Component\Command\Command;
classRenameArticleCommandimplementsCommand{
public $article_id;
public $new_name = '';
}
useGpsLab\Component\Command\Command;
useDoctrine\ORM\EntityManagerInterface;
classRenameArticleHandler{
private $em;
publicfunction__construct(EntityManagerInterface $em){
$this->em = $em;
}
publicfunctionhandleRenameArticle(RenameArticleCommand $command): void{
// get article by id
$article = $this->em->getRepository(Article::class)->find($command->article_id);
$article->rename($command->new_name);
}
}
useGpsLab\Component\Command\Bus\HandlerLocatedCommandBus;
useGpsLab\Component\Command\Handler\Locator\DirectBindingCommandHandlerLocator;
// register command handler in handler locator
$handler = new RenameArticleHandler($em);
$locator = new DirectBindingCommandHandlerLocator();
$locator->registerHandler(RenameArticleCommand::class, [$handler, 'handleRenameArticle']);
// create bus with command handler locator
$bus = new HandlerLocatedCommandBus($locator);
// ...// create rename article command
$command = new RenameArticleCommand();
$command->article_id = $article_id;
$command->new_name = $new_name;
// handle command
$bus->handle($command);
useGpsLab\Component\Query\Query;
classArticleByIdentityQueryimplementsQuery{
public $article_id;
}
useGpsLab\Component\Query\Query;
useDoctrine\ORM\EntityManagerInterface;
classArticleByIdentityHandler{
private $em;
publicfunction__construct(EntityManagerInterface $em){
$this->em = $em;
}
publicfunctionhandleArticleByIdentity(ArticleByIdentityQuery $query){
// get article by idreturn$this->em->getRepository(Article::class)->find($query->article_id);
}
}
useGpsLab\Component\Query\Bus\HandlerLocatedQueryBus;
useGpsLab\Component\Query\Handler\Locator\DirectBindingQueryHandlerLocator;
// register query handler in handler locator
$handler = new ArticleByIdentityHandler($em);
$locator = new DirectBindingQueryHandlerLocator();
$locator->registerHandler(ArticleByIdentityQuery::class, [$handler, 'handleArticleByIdentity']);
// create bus with query handler locator
$bus = new HandlerLocatedQueryBus($locator);
// ...// create find article query
$query = new ArticleByIdentityQuery();
$query->article_id = $article_id;
// handle query
$article = $bus->handle($query);
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.