Message
Message in Pandawa is basically a micro-controller for processing route request. There is 2 key message type for different purposes. Each type of message requires a handler to handle the request.
Commandโ
Command is a type of message for manipulating data (storing or updating dabase) or process a request.
To let Pandawa fill the command with request data, you need to declare the properties with the exact same name or call the origin method.
Generating a new commandโ
You could run php artisan pandawa:make:command to generate a base model.
tip
Or, use the namespace at Pandawa\Component\Message\AbstractCommand.
Template ๐
Replace the things encapsulated with <> including itself as well.
<?php
declare(strict_types=1);
namespace <Namespace>\<Folder>\Command;
use Pandawa\Component\Message\AbstractCommand;
use Pandawa\Component\Message\NameableMessageInterface;
use Pandawa\Component\Support\NameableClassTrait;
final class <Name> extends AbstractCommand implements NameableMessageInterface {
use NameableClassTrait;
// Define property with the same name to let Pandawa fill it with the request data.
// protected string data;
//->
}
Generating a new command handlerโ
You could run php artisan pandawa:make:command-handler to generate a base command handler.
tip
Or, use the namespace at Pandawa\Component\Message\AbstractCommand.
Template ๐
Replace the things encapsulated with <> including itself as well.
<?php
declare(strict_types=1);
namespace <Namespace>\<Folder>\Command;
final class <Command>Handler {
public function __construct() {}
public function handle(<Command> $message) {
//->
}
}
Example ๐งช
<?php
declare(strict_types=1);
namespace Pandawa\Product\Command;
// Service
use Pandawa\Product\Service\ProductCreator;
final class CreateProductHandler {
public function __construct(private ProductCreator $creator) {}
public function handle(CreateProduct $message) {
return $this->creator->create($message);
}
}
Queryโ
Query is like a Command but only for showing data. It works relatively exactly the same as a command
Generating a new queryโ
You could run php artisan pandawa:make:query to generate a base query.
tip
Or, use the namespace at Pandawa\Component\Message\AbstractQuery.
Template ๐
Replace the things encapsulated with <> including itself as well.
<?php
<?php
declare(strict_types=1);
namespace <Namespace>\<Folder>\Query;
use Pandawa\Component\Message\AbstractQuery;
use Pandawa\Component\Message\NameableMessageInterface;
use Pandawa\Component\Support\NameableClassTrait;
final class <Name> extends AbstractQuery implements NameableMessageInterface {
use NameableClassTrait;
//->
}
Generating a new query handlerโ
You could run php artisan pandawa:make:query-handler to generate a base query handler.
tip
Template ๐
Replace the things encapsulated with <> including itself as well.
<?php
declare(strict_types=1);
namespace <Namespace>\<Folder>\Query;
use Pandawa\Component\Message\InteractsWithRepositoryTrait;
final class <Query>Handler {
use InteractsWithRepositoryTrait;
public function __construct(private <Repository> $repository) {}
protected function run(<Repository> $repository, <Query> $query) {
//->
}
protected function repository() {
return $this->repository;
}
}
Example ๐งช
<?php
// GetAvailableProductsHandler.php
declare(strict_types=1);
namespace Pandawa\Product\Query;
use Pandawa\Component\Message\InteractsWithRepositoryTrait;
use Pandawa\Product\Repository\ProductRepository;
final class GetAvailableProductsHandler {
use InteractsWithRepositoryTrait;
public function __construct(private ProductRepository $repository) {}
protected function run($repository, $query) {
return $repository->getAvailable();
}
protected function repository() {
return $this->repository;
}
}
<?php
// ProductRepository.php
// ...
final class ProductRepository extends Repository {
public function getAvailable() {
$qb = $this->createQueryBuilder();
$qb->where('status', 'published')->where('stock', '>', 0);
return $this->execute($qb);
}
}