rtens/domin

通用、DDD友好型管理接口

v0.8.2 2016-03-03 08:53 UTC

This package is not auto-updated.

Last update: 2024-09-14 16:29:30 UTC


README

domin ( domain model interface ) 是一个使用 命令对象模式 对抽象 领域模型 进行管理的界面。

有关如何使用示例,请参阅 示例应用程序

模型

系统中的每个 能力 都由一个 Action 表示,该操作指定如何执行它以及它需要的 Parameters。因此,domin 可以使用 Fields 从用户那里获取缺失的参数。操作可以返回值,这些值通过 Renderers 显示。

安装

要在项目中使用 domin,请使用 Composer 引入它

composer require rtens/domin

如果您想开发 domin,请使用 Composer 下载它,并使用 scrut 执行规范

composer create-project -sdev rtens/domin
cd domin
vendor/bin/scrut spec

快速开始

要将 domin 作为使用 curir 作为交付系统的 Web 应用程序运行,请将以下代码粘贴到 index.php

use rtens\domin\delivery\web\adapters\curir\root\IndexResource;
use rtens\domin\delivery\web\WebApplication;
use watoki\curir\WebDelivery;

WebDelivery::quickResponse(IndexResource::class,
    WebDelivery::init(null,
        WebApplication::init(function (WebApplication $app) {
            // Set-up $app here (e.g. $app->actions->add('foo', ...))
        })));

要使用 silex 运行 domin,请将此代码粘贴到 index.php

use rtens\domin\delivery\web\adapters\silex\SilexControllerProvider;
use rtens\domin\delivery\web\WebApplication;
use Silex\Application;

require_once __DIR__ . '/vendor/autoload.php';

$app = new Application();
$app->mount('/', new SilexControllerProvider(
    WebApplication::init(function (WebApplication $app) {
        // Set-up $app here (e.g. $app->actions->add('foo', ...))
    })));
$app->run();

然后启动开发服务器,在 localhost:8000 访问应用程序

$ php -S localhost:8000 index.php

要运行 CLI 应用程序,请将此代码粘贴到 cli.php

use rtens\domin\delivery\cli\CliApplication;

CliApplication::run(CliApplication::init(function (CliApplication $app) {
    // Set-up $app here (e.g. $app->actions->add('foo', ...))
}));

然后运行它

$ php cli.php

行动!

Actions 决定它们需要什么 Parameters,如何用默认值填充它们,以及最重要的是如何 execute() 它们。_domin_ 了解有哪些操作是通过 ActionRegistry 实现的,因此所有操作都需要添加到其中。

创建操作有几种方法

实现 Action

最直接但也可能不是最方便的方法是为系统的每个能力创建 Action 的实现。

class MyAction implements Action {

    public function caption() {
        return 'Some Action';
    }

    public function description() {
        return 'Some Description';
    }

    public function parameters() {
        return [
            new Parameter('foo', new StringType()),
            new Parameter('bar', new ClassType(\DateTime::class))
        ];
    }

    public function fill(array $parameters) {
        $parameters['foo'] = 'default value of foo';
        return $parameters;
    }

    public function execute(array $parameters) {
        return "Make it so! " . json_encode($parameters);
    }
}

$actionRegistry->add('my', new MyAction());

扩展 ObjectAction

如果您使用 DTOs 来表示能力,您可以从 ObjectAction 扩展您的操作,以使用反射从这些类的属性中推断 Parameters。然后,可以通过使用 Command Bus 将此子类通用化。

class MyAction extends ObjectAction {
    
    public function __construct($class, TypeFactory $types, CommandBus $bus) {
        parent::__construct($class, $types);
        $this->bus = $bus;
    }

    protected function executeWith($object) {
        $this->bus->handle($object);
    }
}

$actionRegistry->add('my', new MyAction(MyCommand::class, $types, $bus));
$actionRegistry->add('your', new MyAction(YourCommand::class, $types, $bus));
$actionRegistry->add('their', new MyAction(TheirCommand::class, $types, $bus));

生成 ObjectActions

通过通用的执行动作方式,您可以使用ObjectActionGenerator来自动从文件夹中的所有类生成并注册动作。

(new ObjectActionGenerator($actionRegistry, $typeFactory))->fromFolder('model/commands', function ($object) {
    $bus->handle($object);
});

使用MethodAction

如果您不想为每个命令创建一个类,您可以使用MethodAction从方法签名中推断参数。

$actionRegistry->add('my', new MethodAction($handler, 'handleMyCommand', $typeFactory));
$actionRegistry->add('your', new MethodAction($handler, 'handleYourCommand', $typeFactory));
$actionRegistry->add('their', new MethodAction($handler, 'handleTheirCommand', $typeFactory));

生成MethodActions

还有一个MethodActionGenerator可以注册对象的全部方法。

(new MethodActionGenerator($actionRegistry, $typeFactory))->fromObject($handler);

许可证

domin发布在MIT许可证下。