This package provides the building blocks to build PHP applications using Hexagonal Architecture (a.k.a. Ports-and-Adapters).
Via composer:
composer require mohmann/hexagonal
<?php
use mohmann\Hexagonal\Command\AbstractCommand;
use mohmann\Hexagonal\Command\Bus\SimpleCommandBus;
use mohmann\Hexagonal\CommandInterface;
use mohmann\Hexagonal\Exception\HexagonalException;
use mohmann\Hexagonal\Handler\Resolver\HandlerResolver;
use mohmann\Hexagonal\HandlerInterface;
require_once dirname(dirname(__FILE__)) . '/vendor/autoload.php';
class FooCommand extends AbstractCommand
{
public $payload;
public function __construct(string $payload)
{
$this->payload = $payload;
}
}
class FooHandler implements HandlerInterface
{
/**
* {@inheritDoc}
*/
public function handle(CommandInterface $command)
{
return \sprintf('%s baz', $command->payload);
}
}
$handlerResolver = new HandlerResolver([
FooCommand::class => new FooHandler(),
]);
$commandBus = new SimpleCommandBus($handlerResolver);
try {
$command = new FooCommand('bar');
$result = $commandBus->execute($command);
var_dump($result);
} catch (HexagonalException $e) {
var_dump($e);
}
Check the examples subdirectory for more usage examples.
Refer to the Makefile
for helpful commands, e.g.:
make stan
make test
make inf
hexagonal is released under the MIT License. See the bundled LICENSE file for details.