2017 © Pedro Peláez
 

library php-shared-kernel

Simple shared kernel for DDD applications, with many scaffolding code for Hexagonal architecture, event sourcing, etc.

image

zeeloengineering/php-shared-kernel

Simple shared kernel for DDD applications, with many scaffolding code for Hexagonal architecture, event sourcing, etc.

  • Tuesday, July 3, 2018
  • by druiz
  • Repository
  • 1 Watchers
  • 0 Stars
  • 893 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 1 Forks
  • 0 Open issues
  • 28 Versions
  • 32 % Grown

The README.md

Shared kernel

Simple shared kernel for DDD applications, with many scaffolding code for Hexagonal architecture, event sourcing, etc., (*1)

Entities

You can create a new entity just inheriting from Entity class:, (*2)

<?php

use StraTDeS\SharedKernel\Domain\Entity;
use StraTDeS\SharedKernel\Domain\Id;

class Person extends Entity
{
    private $name;

    private $surname;

    public function __construct(Id $id, string $name, string $surname)
    {
        parent::__construct($id);

        $this->name = $name;
        $this->surname = $surname;
    }

    public function getName(): string
    {
        return $this->name;
    }

    public function getSurname(): string
    {
        return $this->surname;
    }
}

Implementing constructor is mandatory as Id is controlled from base class., (*3)

As far as you have inherited from Entity, you have an EventStreamAvailable, so you can record events and retrieve the EventStream:, (*4)

<?php

use StraTDeS\SharedKernel\Domain\Entity;
use StraTDeS\SharedKernel\Domain\Id;
use StraTDeS\SharedKernel\Domain\UUIDV4;

class Person extends Entity
{
    private $name;

    private $surname;

    public function __construct(Id $id, string $name, string $surname)
    {
        parent::__construct($id);

        $this->name = $name;
        $this->surname = $surname;

        $this->recordThat(
            new PersonCreated(
                UUIDV4::generate(),
                $this->getId(),
                $name,
                $surname
            )
        );
    }

    public function getName(): string
    {
        return $this->name;
    }

    public function getSurname(): string
    {
        return $this->surname;
    }
}

$person = new Person(
    UUIDV4::generate(),
    'Alex',
    'Hernández'
);

$eventStream = $person->pullEventStream();

Entity collections

As you are eventually going to return arrays of entities from your repositories, I have prepared a simple entity collection for you to inherit:, (*5)

<?php

use StraTDeS\SharedKernel\Domain\EntityCollection;
use StraTDeS\SharedKernel\Domain\UUIDV4;

class PersonCollection extends EntityCollection
{

}

$personCollection = new PersonCollection([
    new Person(
        UUIDV4::generate(),
        'Alex',
        'Hernández'
    ),
    new Person(
        UUIDV4::generate(),
        'John',
        'Smith'
    )
]);

$entities = $personCollection->getEntities();

DomainEvents

You have a base class DomainEvent to inherit from when you want to create an Event. As simple as that:, (*6)

<?php

use StraTDeS\SharedKernel\Domain\DomainEvent;
use StraTDeS\SharedKernel\Domain\Id;

class PersonCreated extends DomainEvent
{
    private $name;

    private $surname;

    public function __construct(Id $id, Id $entityId, string $name, string $surname)
    {
        parent::__construct($id, $entityId);

        $this->name = $name;
        $this->surname = $surname;
    }

    public function getName(): string
    {
        return $this->name;
    }

    public function getSurname(): string
    {
        return $this->surname;
    }
}

UseCase

A very simple way to access Domain and Infrastructure layers from your console commands or controllers is through an Application UseCase. A UseCase receives a Request and may return (or null) a Response. Let's see an example:, (*7)

<?php

use StraTDeS\SharedKernel\Application\UseCase\UseCase;
use StraTDeS\SharedKernel\Application\UseCase\Request;
use StraTDeS\SharedKernel\Application\UseCase\Response;
use StraTDeS\SharedKernel\Domain\Id;
use StraTDeS\SharedKernel\Application\DataTransformer;

class PersonCollectionToArrayDataTransformer implements DataTransformer
{
    /**
     * @param mixed|PersonCollection $data
     * @return array
     */
    public function transform(mixed $data): mixed
    {
        $persons = [];

        foreach($data as $person) {
            $persons[] = $person->toArray();
        }

        return $persons;
    }
}

class GetUserByIdRequest extends Request
{
    private $id;

    public function __construct(Id $id) 
    {
        $this->id = $id;
    }

    public function getId(): Id
    {
        return $this->id;
    }
}

class GetUserByIdResponse extends Response
{
    private $persons;

    public function __construct(mixed $persons) 
    {
        $this->persons = $persons;
    }

    public function getPersons(): mixed
    {
        return $this->persons;
    }
}

class GetUserByIdUseCase extends UseCase
{
    private $dataTransformer;

    public function __construct(DataTransformer $dataTransformer) 
    {
        $this->dataTransformer = $dataTransformer;
    }

    public function execute(Request $getUserByIdRequest): Response
    {
        $userCollection = //my repository query returns a PersonCollection

        return new GetUserByIdResponse($this->dataTransformer->transform($userCollection));
    }
}

Probably you have noted the DataTransformer object. It provides functionality to transform any object into any other in the application layer. This means you can use the same UseCase to retrieve information in different formats just injecting a different data transformer. This is basic to not return domain objects to the infrastructure layer., (*8)

I recommend defining your use cases with different names based in the data transformer you inject. For example:, (*9)

  • get_user_by_id_use_case_data_transformed_to_array
  • get_user_by_id_use_case_data_transformed_to_json

This can be easily done if you use a decent dependency injector., (*10)

CQRS

CQRS stands for Command Query Responsibility Segregation and basically means that a method should either return a value of modify it's context, never both things at the same time., (*11)

I have provided with some useful interfaces to work both with Commands (to change context) and Queries (to retrieve information). This is used to be done through command and query buses. A bus accepts some kind of request (a Command or a Query) and processes it through a handler (a CommandHandler or a QueryHandler). Middlewares can be added along the process., (*12)

So in the namespace StraTDeS\SharedKernel\Application\CQRS you have abstract classes for both Command and Query, and interfaces for both CommandHandler and QueryHandler., (*13)

Repositories

There are some useful interfaces to implement repositories, including get, find, all interface and save interface. Apart, I have included two basic doctrine repositories, DoctrineRepository and DoctrinePersistentRepository. You can use it to have some features for free:, (*14)

<?php

use StraTDeS\SharedKernel\Infrastructure\DoctrinePersistentRepository;
use StraTDeS\SharedKernel\Domain\UUIDV4;

class DoctrinePersonRepository extends DoctrinePersistentRepository implements PersonRepository
{
    public function getEntityName(): string
    {
        return Person::class;
    }
}

// person repository creation ...

$person = $personRepository->get(UUIDV4::fromString('6238ec41-71d0-4482-97f5-4c5c4919e635'));
$person->changeName('John');
$personRepository->save($person);

The Versions

03/07 2018

dev-master

9999999-dev

Simple shared kernel for DDD applications, with many scaffolding code for Hexagonal architecture, event sourcing, etc.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Alex Hernández

03/07 2018

dev-AllowTextCriteriaItemToMatchNullValues

dev-AllowTextCriteriaItemToMatchNullValues

Simple shared kernel for DDD applications, with many scaffolding code for Hexagonal architecture, event sourcing, etc.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Alex Hernández

21/06 2018

dev-AddingClearToReadModel

dev-AddingClearToReadModel

Simple shared kernel for DDD applications, with many scaffolding code for Hexagonal architecture, event sourcing, etc.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Alex Hernández

11/06 2018

dev-BuildEventFromData

dev-BuildEventFromData

Simple shared kernel for DDD applications, with many scaffolding code for Hexagonal architecture, event sourcing, etc.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Alex Hernández

08/06 2018

dev-createGenericApplicationException

dev-createGenericApplicationException

Simple shared kernel for DDD applications, with many scaffolding code for Hexagonal architecture, event sourcing, etc.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Alex Hernández

29/05 2018

dev-fixOrderByMapping

dev-fixOrderByMapping

Simple shared kernel for DDD applications, with many scaffolding code for Hexagonal architecture, event sourcing, etc.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Alex Hernández

29/05 2018

dev-makeOrderByGoThroughMapKeys

dev-makeOrderByGoThroughMapKeys

Simple shared kernel for DDD applications, with many scaffolding code for Hexagonal architecture, event sourcing, etc.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Alex Hernández

10/05 2018

dev-AddingListableQuery

dev-AddingListableQuery

Simple shared kernel for DDD applications, with many scaffolding code for Hexagonal architecture, event sourcing, etc.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Alex Hernández

24/04 2018

dev-createGenericAPIRestClient

dev-createGenericAPIRestClient

Simple shared kernel for DDD applications, with many scaffolding code for Hexagonal architecture, event sourcing, etc.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Alex Hernández

09/03 2018

dev-creatingCodeToManageQueues

dev-creatingCodeToManageQueues

Simple shared kernel for DDD applications, with many scaffolding code for Hexagonal architecture, event sourcing, etc.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Alex Hernández

07/03 2018

dev-AddingPageableCollection

dev-AddingPageableCollection

Simple shared kernel for DDD applications, with many scaffolding code for Hexagonal architecture, event sourcing, etc.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Alex Hernández

05/03 2018

dev-AddingCriteria

dev-AddingCriteria

Simple shared kernel for DDD applications, with many scaffolding code for Hexagonal architecture, event sourcing, etc.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Alex Hernández

27/02 2018

dev-AddingLimitAndOffsetToReadModelRepository

dev-AddingLimitAndOffsetToReadModelRepository

Simple shared kernel for DDD applications, with many scaffolding code for Hexagonal architecture, event sourcing, etc.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Alex Hernández

26/02 2018

dev-AddingReadModel

dev-AddingReadModel

Simple shared kernel for DDD applications, with many scaffolding code for Hexagonal architecture, event sourcing, etc.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Alex Hernández

22/02 2018

dev-AddingDeleteToRepository

dev-AddingDeleteToRepository

Simple shared kernel for DDD applications, with many scaffolding code for Hexagonal architecture, event sourcing, etc.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Alex Hernández

21/02 2018

dev-AddingEntityDisablingCapabilities

dev-AddingEntityDisablingCapabilities

Simple shared kernel for DDD applications, with many scaffolding code for Hexagonal architecture, event sourcing, etc.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Alex Hernández

21/02 2018

dev-AddingEventSourcingCapabilities

dev-AddingEventSourcingCapabilities

Simple shared kernel for DDD applications, with many scaffolding code for Hexagonal architecture, event sourcing, etc.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Alex Hernández

20/02 2018

dev-RemovingToArrayFromEntity

dev-RemovingToArrayFromEntity

Simple shared kernel for DDD applications, with many scaffolding code for Hexagonal architecture, event sourcing, etc.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Alex Hernández

19/02 2018

dev-AddingCriteriaToRepository

dev-AddingCriteriaToRepository

Simple shared kernel for DDD applications, with many scaffolding code for Hexagonal architecture, event sourcing, etc.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Alex Hernández

19/02 2018

dev-doctrineFixes

dev-doctrineFixes

Simple shared kernel for DDD applications, with many scaffolding code for Hexagonal architecture, event sourcing, etc.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Alex Hernández

07/02 2018

dev-AddingFieldsToDomainEvent

dev-AddingFieldsToDomainEvent

Simple shared kernel for DDD applications, with many scaffolding code for Hexagonal architecture, event sourcing, etc.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Alex Hernández

20/12 2017

0.7

0.7.0.0

Simple shared kernel for DDD applications, with many scaffolding code for Hexagonal architecture, event sourcing, etc.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Alex Hernández

20/12 2017

0.6

0.6.0.0

Simple shared kernel for DDD applications, with many scaffolding code for Hexagonal architecture, event sourcing, etc.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Alex Hernández

19/12 2017

0.5

0.5.0.0

Simple shared kernel for DDD applications, with many scaffolding code for Hexagonal architecture, event sourcing, etc.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Alex Hernández

19/12 2017

0.4

0.4.0.0

Simple shared kernel for DDD applications, with many scaffolding code for Hexagonal architecture, event sourcing, etc.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Alex Hernández

03/12 2017

0.3

0.3.0.0

Simple shared kernel for DDD applications, with many scaffolding code for Hexagonal architecture, event sourcing, etc.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Alex Hernández

03/12 2017

0.2

0.2.0.0

Simple shared kernel for DDD applications, with many scaffolding code for Hexagonal architecture, event sourcing, etc.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Alex Hernández

03/12 2017

0.1

0.1.0.0

Simple shared kernel for DDD applications, with many scaffolding code for Hexagonal architecture, event sourcing, etc.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Alex Hernández