Skip to content

Commit

Permalink
#6 - add rabbitmq and examples (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
MrHDOLEK authored Nov 3, 2023
1 parent 3b12795 commit 8163f74
Show file tree
Hide file tree
Showing 122 changed files with 4,476 additions and 412 deletions.
8 changes: 7 additions & 1 deletion .env.test
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,10 @@ DB_HOST=localhost
DB_PORT=5432
DB_NAME=slim
DB_USER=slim
DB_PASSWORD=password
DB_PASSWORD=password

RABBITMQ_HOST=rabbitmq
RABBITMQ_PORT=5672
RABBITMQ_USER=rabbitmq
RABBITMQ_PASS=rabbitmq
RABBITMQ_VHOST=app
2 changes: 1 addition & 1 deletion .github/workflows/php.yml
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ jobs:
run: |
cp .env.test .env
composer migrate
vendor/bin/phpunit --testsuite unit --fail-on-incomplete --log-junit junit.xml --coverage-clover clover.xml
vendor/bin/phpunit --testsuite unit --log-junit junit.xml --coverage-clover clover.xml
- name: Send test coverage to codecov.io
uses: codecov/codecov-action@v3
Expand Down
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,6 @@
/logs/
.deptrac.cache
junit.xml
clover.xml
clover.xml
tests/*.txt
/test/*/*Success__1.json
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ kill-all: ## Kill all running containers
openapi: ## Generate documentation for api
docker-compose exec php vendor/bin/openapi /var/www/src --output resources/docs/openapi.yaml

db-create: ## Create db from doctrine schema
docker-compose exec php php vendor/bin/doctrine orm:schema-tool:create
db-create: ## Create db from migrations
$(MAKE) migrate

migrate: ## Run migrations
docker-compose exec php php vendor/bin/doctrine-migrations migrate
Expand Down
98 changes: 97 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
[![Codecov.io](https://codecov.io/gh/MrHDOLEK/slim4-boirlerplate/graph/badge.svg?token=KKBMW5HJVM)](https://codecov.io/gh/MrHDOLEK/slim4-boirlerplate)
[![License](https://img.shields.io/github/license/robiningelbrecht/slim-skeleton-ddd-amqp?color=428f7e&logo=open%20source%20initiative&logoColor=white)](https://github.com/MrHDOLEK/slim4-boirlerplate/blob/master/LICENSE)
[![PHPStan Enabled](https://img.shields.io/badge/PHPStan-level%205-succes.svg?logo=php&logoColor=white&color=31C652)](https://phpstan.org/)
[![PHP](https://img.shields.io/packagist/php-v/robiningelbrecht/php-slim-skeleton/dev-master?color=%23777bb3&logo=php&logoColor=white)](https://php.net/)
[![PHP](https://img.shields.io/packagist/php-v/mrhdolek/slim4-boirlerplate/dev-main?color=%23777bb3&logo=php&logoColor=white)](https://php.net/)


---
Expand Down Expand Up @@ -35,6 +35,8 @@ Please install packages makefile for [Windows](http://gnuwin32.sourceforge.net/p
- `http://localhost`
## Documentation for a Rest Api
- `http://localhost/docs/v1`
## RabbitMq dashboard
- `http://localhost:15672`
## All commands

- `make help`
Expand All @@ -59,12 +61,15 @@ Please install packages makefile for [Windows](http://gnuwin32.sourceforge.net/p
- Console <- Related to loading console commands into the application's dependency container.
- DependencyInjection <- Manages the dependencies and their relationships in the application. It ensures that objects get the right services they depend upon.
- Environment <- Might handle environment-specific configurations or utilities.
- Events <- Here are the things that make it possible to throw and handle events
- AMQP <- Here you will find the implementation of the protocol together with its full support
- Persistence <- Deals with data storage and retrieval.
- Doctrine
- Fixtures <- Contains sample data that can be loaded into the database for testing or initial setup.
- Mapping <- Manages how objects are mapped to database tables.
- Migrations <- Helps in versioning and migrating database schemas.
- Repository <- Repositories are used to retrieve and store data in the database.
- Queues <- Here are all registered queues with configuration
- Serialization


Expand Down Expand Up @@ -106,6 +111,7 @@ return function (App $app) {

The console application uses the Symfony console component to leverage CLI functionality.


```php
#[AsCommand(name: 'app:user:create')]
class CreateUserConsoleCommand extends Command
Expand All @@ -117,6 +123,96 @@ class CreateUserConsoleCommand extends Command
}
}
```

### Domain event and event handlers

The framework implements the amqp protocol with handlers that allow events to be easily pushed onto the queue.
Each event must have a handler implemented that consumes the event.

#### Creating a new event

```php
class UserWasCreated extends DomainEvent
{

}
```

#### Creating the corresponding event handler

```php
namespace App\Domain\Entity\User\DomainEvents;

#[AsEventHandler]
class UserWasCreatedEventHandler implements EventHandler
{
public function __construct(
) {
}

public function handle(DomainEvent $event): void
{
assert($event instanceof UserWasCreated);

// Do stuff.
}
}
```

### Eventing

#### Create a new event

```php
class UserWasCreated extends DomainEvent
{
public function __construct(
private UserId $userId,
) {
}

public function getUserId(): UserId
{
return $this->userId;
}
}
```

### Async processing of commands with RabbitMQ

The chosen AMQP implementation for this project is RabbitMQ, but it can be easily switched to for example Amazon's AMQP solution.

#### Registering new queues

```php
#[AsAmqpQueue(name: "user-command-queue", numberOfWorkers: 1)]
class UserEventQueue extends EventQueue
{
}
```

#### Queueing events

```php
final readonly class UserEventsService
{
public function __construct(
private UserEventQueue $userEventQueue,
) {}

public function userWasCreated(User $user): void
{
$this->userEventQueue->queue(new UserWasCreated($user));
}
}
```

#### Consuming your queue

```bash
> docker-compose run --rm php bin/console.php app:amqp:consume user-command-queue
```

### Create new entity

If you have created a new entity and want to map it to a database you must create a xml in src/Infrastructure/Persistence/Doctrine/Mapping .
Expand Down
1 change: 0 additions & 1 deletion bin/console.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
$_ENV['APP_ENV'] = $env;
}

/** @var ContainerInterface $container */
$container = ContainerFactory::create();

try {
Expand Down
7 changes: 4 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@
"ext-json": "*",
"ext-pcntl": "*",
"ext-sockets": "*",
"awurth/slim-validation": "^5.0",
"awurth/slim-validation": "^3.3",
"doctrine/data-fixtures": "^1.6",
"doctrine/migrations": "^3.6",
"doctrine/orm": "^2.15",
"fakerphp/faker": "^1.23",
"lcobucci/clock": "^3.1",
"monolog/monolog": "^2.8",
"php-amqplib/php-amqplib": "^2.8",
"php-amqplib/php-amqplib": "^3.2",
"php-di/php-di": "^7.0",
"php-di/slim-bridge": "^3.4",
"ramsey/uuid": "^4.7",
Expand All @@ -53,7 +53,8 @@
"phpstan/extension-installer": "^1.2.0",
"phpstan/phpstan": "^1.8",
"phpunit/phpunit": "^9.5.26",
"qossmic/deptrac-shim": "^1.0"
"qossmic/deptrac-shim": "^1.0",
"spatie/phpunit-snapshot-assertions": "^4.2"
},
"config": {
"allow-plugins": {
Expand Down
Loading

0 comments on commit 8163f74

Please sign in to comment.