1. Go to this page and download the library: Download itantik/cq-dispatcher 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/ */
itantik / cq-dispatcher example snippets
class AddUserCommand implements IRequest
{
/** @var int */
private $id;
/** @var string */
private $name;
/** @var string */
private $surname;
public function __construct(int $id, string $name, string $surname)
{
$this->id = $id;
$this->name = $name;
$this->surname = $surname;
}
public function id(): int
{
return $this->id;
}
public function name(): string
{
return $this->name;
}
public function surname(): string
{
return $this->surname;
}
}
class AddUserHandler implements ICommandHandler
{
/** @var UserRepository */
private $repository;
public function __construct(UserRepository $repository)
{
$this->repository = $repository;
}
public function handle(AddUserCommand $command): void
{
$user = new User($command->id(), $command->name(), $command->surname());
$this->repository->add($user);
}
}
class UserCommands extends \Itantik\CQDispatcher\Commands
{
}
// simplified code
class UserController
{
/** @var UserCommands */
private $userCommands;
public function actionAddUser(string $name, string $surname): void
{
// create a command
$id = SomeIdGenerator::next();
$command = new AddUserCommand($id, $name, $surname);
try {
// execute command
$this->userCommands->execute($command);
} catch (\Exception $ex) {
// handle error
// ...
}
// redirect to user detail page
// ...
}
}
final class TransactionalMiddleware implements Itantik\Middleware\IMiddleware
{
/** @var DatabaseConnection */
private $connection;
public function __construct(DatabaseConnection $connection)
{
$this->connection = $connection;
}
public function handle(IRequest $request, ILayer $nextLayer): IResponse
{
$connection = $this->connection;
$connection->beginTransaction();
try {
$res = $nextLayer->handle($request);
$connection->commit();
return $res;
} catch (Exception $e) {
$connection->rollback();
throw $e;
}
}
}
class UserCommands extends \Itantik\CQDispatcher\Commands
{
public function __construct(
ICommandDispatcher $commandDispatcher,
DatabaseConnection $connection
) {
parent::__construct($commandDispatcher);
$this->appendMiddleware(new TransactionalMiddleware($connection));
}
}
class FindAllUsersQuery implements IRequest
{
}
class FindAllUsersHandler implements IQueryHandler
{
/** @var UserRepository */
private $repository;
public function __construct(UserRepository $repository)
{
$this->repository = $repository;
}
public function handle(FindAllUsersQuery $query): UserList
{
return $this->repository->findAll();
}
}
class UserQueries extends \Itantik\CQDispatcher\Queries
{
}
// simplified code
class UserController
{
/** @var UserQueries */
private $userQueries;
public function actionAllUsers(): void
{
// create a query
$query = new FindAllUsersQuery();
// execute query
$userList = $this->userQueries->execute($query);
// fill-in template
// ...
}
}