-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #18 from patchlevel/add-more-docs
add more docs
- Loading branch information
Showing
12 changed files
with
375 additions
and
157 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
# Event Bus | ||
|
||
This library uses the core principle called [event bus](https://martinfowler.com/articles/201701-event-driven.html). | ||
|
||
For all events that are persisted (when the `save` method has been executed on the [repository](./repository.md)), | ||
the event will be dispatched to the `event bus`. All listeners are then called for each event. | ||
|
||
## Symfony Event Bus | ||
|
||
To use the [Symfony Messager](https://symfony.com/doc/current/messenger.html), | ||
you have to define it in messenger.yaml. | ||
It's best to call the bus "event.bus". | ||
An event bus can have 0 or n listener for an event. | ||
So "allow_no_handlers" has to be configured. | ||
|
||
```yaml | ||
framework: | ||
messenger: | ||
buses: | ||
event.bus: | ||
default_middleware: allow_no_handlers | ||
``` | ||
We can then use this message or event bus in event sourcing: | ||
```yaml | ||
patchlevel_event_sourcing: | ||
event_bus: | ||
service: event.bus | ||
``` | ||
## Command Bus | ||
If you use a command bus or cqrs as a pattern, then you should define a new message bus. | ||
The whole thing can look like this: | ||
```yaml | ||
framework: | ||
messenger: | ||
default_bus: command.bus | ||
buses: | ||
command.bus: ~ | ||
event.bus: | ||
default_middleware: allow_no_handlers | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,61 @@ | ||
# Installation | ||
|
||
### Define your aggregates with class namespace and the table name | ||
If you are not using a symfony [flex](https://github.com/symfony/flex) | ||
or the [recipes](https://flex.symfony.com/) for it, | ||
then you have to carry out a few installation steps by hand. | ||
|
||
Class `App\Domain\Profile\Profile` is from the [libraries example](https://github.com/patchlevel/event-sourcing#define-aggregates) and is using the table name `profile` | ||
## Require package | ||
|
||
The first thing to do is to install packet if it has not already been done. | ||
|
||
```bash | ||
composer require patchlevel/event-sourcing-bundle | ||
``` | ||
|
||
> :book: how to install [composer](https://getcomposer.org/doc/00-intro.md) | ||
## Enable bundle | ||
|
||
Then we have to activate the bundle in the `config/bundles.php`: | ||
|
||
```php | ||
return [ | ||
Patchlevel\EventSourcingBundle\PatchlevelEventSourcingBundle::class => ['all' => true], | ||
]; | ||
``` | ||
|
||
If you don't have `config/bundles.php` then you need to add the bundle in the kernel: | ||
|
||
```php | ||
class AppKernel extends Kernel | ||
{ | ||
public function registerBundles() | ||
{ | ||
$bundles = [ | ||
new Patchlevel\EventSourcingBundle\PatchlevelEventSourcingBundle(), | ||
]; | ||
} | ||
} | ||
``` | ||
|
||
## Configuration file | ||
|
||
Now you have to add a minimal configuration file here `config/packages/patchlevel_event_sourcing.yaml`. | ||
|
||
```yaml | ||
patchlevel_event_sourcing: | ||
connection: | ||
url: '%env(EVENTSTORE_URL)%' | ||
store: | ||
type: multi_table | ||
aggregates: | ||
profile: | ||
class: App\Domain\Profile\Profile | ||
``` | ||
## Dotenv | ||
Finally we have to fill the ENV variable with a connection url. | ||
```dotenv | ||
EVENTSTORE_URL: mysql://user:secret@localhost/app | ||
``` | ||
> :book: You can find out more about what a connection url looks like [here](https://www.doctrine-project.org/projects/doctrine-dbal/en/latest/reference/configuration.html#connecting-using-a-url). | ||
Now you can go back to [getting started](https://github.com/patchlevel/event-sourcing-bundle#getting-started). |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
# Processor | ||
|
||
The `processor` is a kind of [event bus](./event_bus.md) listener that can execute actions on certain events. | ||
A process can be for example used to send an email when a guest is checked in: | ||
|
||
```php | ||
namespace App\Domain\Hotel\Listener; | ||
|
||
use App\Domain\Hotel\Event\GuestIsCheckedIn; | ||
use Patchlevel\EventSourcing\Aggregate\AggregateChanged; | ||
use Patchlevel\EventSourcing\EventBus\Listener; | ||
use Symfony\Component\Mailer\MailerInterface; | ||
use Symfony\Component\Mime\Email; | ||
|
||
final class SendCheckInEmailListener implements Listener | ||
{ | ||
private MailerInterface $mailer; | ||
|
||
private function __construct(MailerInterface $mailer) | ||
{ | ||
$this->mailer = $mailer; | ||
} | ||
|
||
public function __invoke(AggregateChanged $event): void | ||
{ | ||
if (!$event instanceof GuestIsCheckedIn) { | ||
return; | ||
} | ||
|
||
$email = (new Email()) | ||
->from('[email protected]') | ||
->to('[email protected]') | ||
->subject('Guest is checked in') | ||
->text(sprintf('A new guest named "%s" is checked in', $event->guestName())); | ||
|
||
$this->mailer->send($email); | ||
} | ||
} | ||
``` | ||
|
||
If you have the symfony default service setting with `autowire`and `autoconfiger` enabled, | ||
the processor is automatically recognized and registered at the `Listener` interface. | ||
Otherwise you have to define the processor in the symfony service file: | ||
|
||
```yaml | ||
services: | ||
App\Domain\Hotel\Listener\SendCheckInEmailListener: | ||
tags: | ||
- event_sourcing.processor | ||
``` |
Oops, something went wrong.