mohmann/hexagonal

提供构建使用六边形架构的应用程序的实用工具

v0.2.0 2018-10-21 15:33 UTC

This package is auto-updated.

Last update: 2024-09-22 05:35:30 UTC


README

Latest Stable Version Build Status Coverage Status License PHP 7.1+

此包提供了构建使用六边形架构(又名端口和适配器)的PHP应用程序的基本构件。

安装

通过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);
}

查看examples子目录以获取更多使用示例。

开发和测试

参考Makefile以获取有用的命令,例如。

make stan
make test
make inf

许可证

hexagonal在MIT许可证下发布。有关详细信息,请参阅捆绑的LICENSE文件。