Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/lezhnev74/ddd-generator

This handy php tool allows me to quickly generate empty primitives
https://github.com/lezhnev74/ddd-generator

clean-architecture ddd generator php

Last synced: 17 days ago
JSON representation

This handy php tool allows me to quickly generate empty primitives

Awesome Lists containing this project

README

        

[![GitHub license](https://img.shields.io/badge/license-MIT-blue.svg)](https://raw.githubusercontent.com/lezhnev74/ddd-generator/master/LICENSE)
[![Build Status](https://travis-ci.org/lezhnev74/ddd-generator.svg?branch=master)](https://travis-ci.org/lezhnev74/ddd-generator)
[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/lezhnev74/ddd-generator/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/lezhnev74/ddd-generator/?branch=master)
[![Code Coverage](https://scrutinizer-ci.com/g/lezhnev74/ddd-generator/badges/coverage.png?b=master)](https://scrutinizer-ci.com/g/lezhnev74/ddd-generator/?branch=master)

# 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](https://lessthan12ms.com/one-step-towards-clean-architecture-from-rapid-application-development/).

**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.

![](screencast.gif)

## 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
```

Command arguments:

* `` - 3 types available: "app", "domain" or "infrastructure"
* `` - "command" or anything you set up in the config file
* `` - 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.

```php
$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
"/**/Command" => __DIR__ . "/stubs/SimpleStub.stub.php",
"/**/Handler.php" => __DIR__ . "/stubs/SimpleStub.stub.php",
],
],
// these files will go to tests folder
"test" => [
"stubs" => [
"/**/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:
* `/**/` - looks like `\App` (each layer may have different one)
* `/**/` - looks like `\Domain\Tests` (each layer may have different one)
* `/**/` - app or domain or infrastructure
* `/**/` - the name of the primitive f.e. `event` or `command`
* `/**/` - looks like `Account\Command\SignUp`, see argument
* `/**/` - looks like `Account\Command` (without final part)
* `/**/` f.e. `SignedUp` (just final part)
* `/**/` f.e. `SignedUpCommand` (the final filename for this stub)

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

```
argument)
namespace /**/\/**/;

use PHPUnit\Framework\TestCase;

// this is
class /**/ 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 `/**/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?