承接 lezhnev74/ddd-generator 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

lezhnev74/ddd-generator

Composer 安装命令:

composer require lezhnev74/ddd-generator

包简介

DDD Generator for 3-layered applications

README 文档

README

GitHub license Build Status Scrutinizer Code Quality Code Coverage

DDD Classes Generator for 3-layered Application

When you develop clean and decoupled app you have to deal with many interfaces and objects. Where you had one object in RAPID development flow, you have plenty of objects in DDD flow. To speed things up I use this tool to generate primitives related to ServiceBus pattern, CQRS pattern and Clean Architecture. If you interested - I blogged about ideas behind this generator.

Note: I use PSR-4 so any folder inheritance leads to namespace inheritance (and vice versa). And also type of slashes is irrelevant - \ and / will do the same.

Note: This tool is designed to be extensible. So while it contains packs to generate widely used primitives like commands, there can be easily added packs to generate http controllers (+tests) and views and anything else.

Note: No Windows support in mind.

In a nutshell

This is a handy tool to generate empty class files + test files from templates and put them in predictable places.

  • set the config
  • run command with given config

What it can generate

  • anything you want
  • CommandBus: command and handler classes
  • QueryBus: request, response and handler
  • VO and Entity
  • Event
  • anything else

Each class is complimented with empty test so I can keep TDD-ing.

Installation

Via composer:

composer require lezhnev74/ddd-generator

Usage

#Comman API
bin/dddtool generate <layer_name> <primitive_type> <primitive_namespace>

Command arguments:

  • <layer_name> - 3 types available: "app", "domain" or "infrastructure"
  • <primitive_type> - "command" or anything you set up in the config file
  • <primitive_namespace> - psr-4 inspired path to the file, which also serves as a namespace. F.e. "Account/Commands/SignUp"

More usage examples:

# Command generation
bin/dddtool generate domain command Account\Commands\SignUp
  
# Specify config
bin/dddtool generate app event Http\Event\HttpRequestRecieved -c path/to/config.php

# Run with no interaction (mind -y flag)
bin/dddtool generate app event Http\Event\HttpRequestRecieved -y
  

Config

You can set folders where new files will go to. You can configure each primitive - set its alias and set stubs to generate new files from.

$config = [
    
    // 3 layers with independent folders for sources and for tests
    "layers" => [
        "app" => [
            "src" => [
                "qcn" => "\\App", // What base namespace to use
                "dir" => __DIR__."/src/app", // Where to put new source files
            ],
            "tests" => [
                "qcn" => "\\Tests", // what base namespace to use
                "dir" => __DIR__."/tests/app", // Where to put new tests files
            ],
        ],
        "domain" => [
            "src" => [
                "qcn" => "\\Domain",
                "dir" => "src/domain",
            ],
            "tests" => [
                "qcn" => "\\Tests",
                "dir" => __DIR__."/tests/domain",
            ],
        ],
        "infrastructure" => [
            "src" => [
                "qcn" => "\\Infrastructure",
                "dir" => __DIR__ . "/src/infrastructure",
            ],
            "tests" => [
                "qcn" => "\\Tests",
                "dir" => __DIR__ . "/tests/infrastructure",
            ],
        ],
    ],
        
    "primitives" => [
        "command" => [
            // these stubs will go to source folder
            "src" => [
                "stubs" => [
                    // See Templates paragraph on placeholders
                    "/*<PSR4_NAMESPACE_LAST>*/Command" => __DIR__ . "/stubs/SimpleStub.stub.php",
                    "/*<PSR4_NAMESPACE_LAST>*/Handler.php" => __DIR__ . "/stubs/SimpleStub.stub.php",
                ],
            ],
            // these files will go to tests folder
            "test" => [
                "stubs" => [
                    "/*<PSR4_NAMESPACE_LAST>*/CommandTest" => __DIR__ . "/stubs/SimpleTestStub.stub.php",
                ],
            ],
        
        ],
    ],
    
];

Templates

Each primitive can have multiple templates (stubs). F.e. command has command and handler templates, query has request, response and handler templates. Event will only have event template and test. So configuration explicitly declares which files to generate and in which folder.

Template support few placeholders which reflects user input:

  • /*<BASE_SRC_NAMESPACE>*/ - looks like \App (each layer may have different one)
  • /*<BASE_TEST_NAMESPACE>*/ - looks like \Domain\Tests (each layer may have different one)
  • /*<LAYER>*/ - app or domain or infrastructure
  • /*<PRIMITIVE>*/ - the name of the primitive f.e. event or command
  • /*<PSR4_NAMESPACE>*/ - looks like Account\Command\SignUp, see <primitive_namespace> argument
  • /*<PSR4_NAMESPACE_BASE>*/ - looks like Account\Command (without final part)
  • /*<PSR4_NAMESPACE_LAST>*/ f.e. SignedUp (just final part)
  • /*<FILENAME>*/ f.e. SignedUpCommand (the final filename for this stub)

This how stub for a test file can look like inside:

<?php
declare(strict_types = 1);

// Full class' namespace is combined from layer's base namespace + user provided namespace (see <primitive_namespace> argument) 
namespace /*<BASE_TEST_NAMESPACE>*/\/*<PSR4_NAMESPACE>*/;

use PHPUnit\Framework\TestCase;

// this is 
class /*<PSR4_NAMESPACE_LAST>*/ extends TestCase {
    
    function test_it_works() {
        $this->markTestIncomplete();
    }
    
}

How it works

Take for example the config file shown above and let's explain this command: bin/dddtool generate domain command Account\Commands\SignUp

The script will do this:

  • first, command will detect the layer for which you want to generate new files. In our case it is "domain"
  • then layer's config is detected
  • then command analyze the primitive name, in our case it is "command"
  • then config for this primitive is being detected
  • then for each stub in the config a new file is prepared. For example, test stub with name /*<PSR4_NAMESPACE_LAST>*/CommandTest will actually be created in file __DIR__ . "/tmp/tests/Account/Commands/SignUp/SignUpCommandTest.php".
  • user sees the list of files which are supposed to be generated
  • after confirmation real files are generated and put to file system.

TODO

  • I am thinking about adding conditions to stub configuration, so user can be asked if he wants to skip some optional stubs for a given primitive.
  • I wonder if I can simplify the design of this tool in any way?

lezhnev74/ddd-generator 适用场景与选型建议

lezhnev74/ddd-generator 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 804 次下载、GitHub Stars 达 26, 最近一次更新时间为 2017 年 03 月 11 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「generator」 「php」 「ddd」 「clean-architecture」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 lezhnev74/ddd-generator 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 lezhnev74/ddd-generator 我们能提供哪些服务?
定制开发 / 二次开发

基于 lezhnev74/ddd-generator 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 804
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 26
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 26
  • Watchers: 7
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2017-03-11