PHPackages                             lezhnev74/ddd-generator - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. lezhnev74/ddd-generator

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

lezhnev74/ddd-generator
=======================

DDD Generator for 3-layered applications

0.2.0(6y ago)268041MITPHPPHP ^7.1

Since Mar 11Pushed 6y ago7 watchersCompare

[ Source](https://github.com/lezhnev74/ddd-generator)[ Packagist](https://packagist.org/packages/lezhnev74/ddd-generator)[ RSS](/packages/lezhnev74-ddd-generator/feed)WikiDiscussions master Synced 3w ago

READMEChangelogDependencies (3)Versions (9)Used By (0)

[![GitHub license](https://camo.githubusercontent.com/7013272bd27ece47364536a221edb554cd69683b68a46fc0ee96881174c4214c/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d626c75652e737667)](https://raw.githubusercontent.com/lezhnev74/ddd-generator/master/LICENSE)[![Build Status](https://camo.githubusercontent.com/da00b620147659292061e72a050ecd3090633b41f4244e7028c59d6c3812ffcf/68747470733a2f2f7472617669732d63692e6f72672f6c657a686e657637342f6464642d67656e657261746f722e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/lezhnev74/ddd-generator)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/7b2aa141c6591c7ccf3bda4303f76a34709ceb21cc0438cdf4673723319940f9/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f6c657a686e657637342f6464642d67656e657261746f722f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/lezhnev74/ddd-generator/?branch=master)[![Code Coverage](https://camo.githubusercontent.com/4de533e2a42cbddcf492c315a82e42c8a8364b83df6774fa3daa45b089be0916/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f6c657a686e657637342f6464642d67656e657261746f722f6261646765732f636f7665726167652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/lezhnev74/ddd-generator/?branch=master)

DDD Classes Generator for 3-layered Application
===============================================

[](#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)](screencast.gif)

In a nutshell
-------------

[](#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
--------------------

[](#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
------------

[](#installation)

Via composer:

```
composer require lezhnev74/ddd-generator

```

Usage
-----

[](#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
------

[](#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
                    "/**/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
---------

[](#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 &lt;primitive\_namespace&gt; 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:

```
