t4web/domain-module

ZF2 Module for Domain implementation.

Installs: 1 488

Dependents: 5

Suggesters: 0

Security: 0

Stars: 2

Watchers: 4

Forks: 0

Open Issues: 0

Type:zf2-module

1.5.0 2019-05-13 12:46 UTC

README

Master: Build Status codecov.io Scrutinizer Code Quality

ZF2 Module for Domain implementation and Infrastructure implementation. Provide dynamically setup Domain layer.

Main Setup

By cloning project

  1. Clone this project into your ./vendor/ directory.

With composer

  1. Add this project in your composer.json:
"require": {
    "t4web/domain-module": "~1.3.0"
}
  1. Now tell composer to download DomainModule by running the command:
$ php composer.phar update

Post installation

  1. Enabling it in your application.config.phpfile.
<?php
return array(
    'modules' => array(
        // ...
        'T4web\DomainModule',
    ),
    // ...
);

Quick start

Describe entity:

class Task extends \T4webDomain\Entity {
    protected $name;
    protected $assigneeId;
    protected $status;
    protected $type;

    /**
     * @var Users\User\User
     */
    protected $assignee;

    public function __construct(array $data, Users\User\User $assignee = null) {
        parent::__construct($data);
        $this->assignee = $assignee;
    }

    /**
     * @return Users\User\User
     */
    public function getAssignee() {
        return $this->assignee;
    }
}

Describe entity_map config in your module.config.php:

return [
    // ...
    
    'entity_map' => [
        'Task' => [
            // table name
            'table' => 'tasks',

            // optional, only if you have use short service names
            'entityClass' => 'Tasks\Task\Task',
            
            // optional, only if you have use short service names
            'collectionClass' => 'Tasks\Task\TaskCollection',

            // optional, by default 'id'
            'primaryKey' => 'id',

            // map entity field with table field
            'columnsAsAttributesMap' => [
                'id' => 'id',
                'name' => 'name',
                'assigneeId' => 'assignee_id',
                'status' => 'status',
                'type' => 'type',
            ],

            // optional, aliases for criteria - for pretty query args
            'criteriaMap' => [
                'id' => 'id_equalTo'
            ],

            // optional, relations for filtering and fetching aggregate entity
            'relations' => [
                'User' => ['tasks.assignee_id', 'users.id']
            ],
        ],
    ],
];

You can get Domain layer from ServiceManager:

// in your controller
$creator = $serviceLocator->get('Task\Service\Creator');

$task = $creator->create(['name' => 'buy milk', 'type' => 2]);

if (!$task) {
    return ['errors' => $creator->getMessages()];
}

$repository = $serviceLocator->get('Task\Infrastructure\Repository');
/** @var Tasks\Task\Task $task */
$task = $repository->findById(123);

$repository = $serviceLocator->get('Task\Infrastructure\AggregateRepository');
$task = $repository->findWith('User')->findById(123);
/** @var Users\User\User $assignee */
$assignee = $task->getAssignee();

Components

  • MODULE-NAME\ENTITY-NAME\Infrastructure\Repository
  • MODULE-NAME\ENTITY-NAME\Service\Creator
  • MODULE-NAME\ENTITY-NAME\Service\Deleter
  • MODULE-NAME\ENTITY-NAME\Service\Updater
  • MODULE-NAME\ENTITY-NAME\EntityFactory

Service classes:

  • MODULE-NAME\ENTITY-NAME\Infrastructure\Config
  • MODULE-NAME\ENTITY-NAME\Infrastructure\Mapper
  • MODULE-NAME\ENTITY-NAME\Infrastructure\QueryBuilder

We recommend use short service names - without module name

  • ENTITY-NAME\Infrastructure\Repository
  • ENTITY-NAME\Service\Creator
  • ENTITY-NAME\Service\Deleter
  • ENTITY-NAME\Service\Updater
  • ENTITY-NAME\EntityFactory

Service classes:

  • ENTITY-NAME\Infrastructure\Config
  • ENTITY-NAME\Infrastructure\Mapper
  • ENTITY-NAME\Infrastructure\QueryBuilder

When you use short service names - entityClass config parameter is required.