Skip to content

Commit 312ae6f

Browse files
committed
Update docs
1 parent a7fc4be commit 312ae6f

File tree

3 files changed

+136
-43
lines changed

3 files changed

+136
-43
lines changed

.github/FUNDING.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1 @@
1-
# These are supported funding model platforms
2-
31
github: driesvints

README.md

Lines changed: 135 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<p align="center">
2-
<img src="https://eventsauce.io/static/logo.svg" height="100px" width="100px">
2+
<a href="https://eventsauce.io">
3+
<img src="https://eventsauce.io/static/logo.svg" height="100px" width="100px">
4+
</a>
35
</p>
46

57
<p align="center">
@@ -19,7 +21,11 @@
1921

2022
# Laravel EventSauce
2123

22-
This library is a work in progress. More docs coming soon...
24+
This library allows you to easily integrate [EventSauce](https://eventsauce.io) with your Laravel application. It takes out the tedious work of having to set up your own message dispatcher and provides an easy API to set up Aggregate Roots, Aggregate Root Repositories, Consumers, and more. It also comes with a range of scaffolding console commands to easily generate the boilerplate needed to get started with an Event Sourced application.
25+
26+
Thanks to [Frank de Jonge](https://github.com/frankdejonge) for building and maintaining the main [EventSauce](https://eventsauce.io) project.
27+
28+
While already usable, this library is currently still a work in progress. More documentation and features will be added over time. We appreciate pull requests that help extend and improve this project.
2329

2430
## Table of Contents
2531

@@ -29,8 +35,14 @@ This library is a work in progress. More docs coming soon...
2935
- [Migrations](#migrations)
3036
- [Default Connection](#default-connection)
3137
- [Default Table](#default-table)
32-
- [Usage](#usage)
38+
- [Scaffolding](#scaffolding)
39+
- [Generating Aggregate Roots](#generating-aggregate-roots)
40+
- [Generating Consumers](#generating-consumers)
3341
- [Generating Commands & Events](#generating-commands--events)
42+
- [Usage](#usage)
43+
- [Aggregate Roots](#aggregate-roots)
44+
- [Aggregate Root Repositories](#aggregate-root-repositories)
45+
- [Consumers](#consumers)
3446
- [Changelog](#changelog)
3547
- [Maintainers](#maintainers)
3648
- [Acknowledgments](#acknowledgments)
@@ -93,50 +105,44 @@ The default table name for your domain messages can be set with the `EVENTSAUCE_
93105
EVENTSAUCE_TABLE=event_store
94106
```
95107

96-
## Usage
108+
## Scaffolding
97109

98-
### Create aggregate root & repository
110+
Laravel EventSauce comes with some commands that you can use to scaffold objects and files which you'll need to build your Event Sourced app. These commands take out the tedious work of writing these yourself and instead let you focus on actually writing your domain logic.
99111

100-
Use the `php artisan make:aggregate-root YourAggregateRootName` command to generate skeleton classes for your aggregate root.
101-
Generated classes:
102-
* Aggregate root
103-
* Aggregate root repository
104-
* Aggregate root Id
105-
* migration
112+
### Generating Aggregate Roots
106113

107-
### Create consumers
114+
Laravel EventSauce can generate aggregate roots and its related files for you. By using the `make:aggregate-root` command, you can generate the following objects and files:
108115

109-
Use the `php artisan make:consumer YourConsumerName` command to generate a consumer.
110-
Within this consumer you can create methods following the `handle{EventName}` specification.
116+
- The `AggregateRoot`
117+
- The `AggregateRootId`
118+
- The `AggregateRootRepository`
119+
- The migration file
111120

112-
For example, if you want to handle the `UserRegistered` event, the method within the consumer would look like:
113-
```php
114-
public function handleUserRegistered(UserRegistered $userRegistered, Message $message)
115-
{
116-
$idOfAggregateRoot = $message->aggregateRootId()->toString();
117-
}
121+
To generate these files for a "Registration" process, run the following command:
122+
123+
```bash
124+
php artisan make:aggregate-root Domain/Registration
118125
```
119-
120-
By default, consumers are handled synchronous. To queue a consumer implement `ShouldQueue` on your consumer class.
121-
122-
### Registering consumers
123-
124-
Consumers can be registered within the `$consumers` property on the AggregateRootRepository.
125-
126-
The queue consumers should run on can be set using the `$queue` property on the AggregateRootRepository.
127-
128-
```php
129-
final class RegistrationAggregateRootRepository extends AggregateRootRepository
130-
{
131-
...
132-
protected array $consumers = [
133-
\Tests\Fixtures\SendConfirmationNotification::class
134-
];
135-
136-
protected string $queue = 'user-aggregate-root';
137-
}
126+
127+
This will scaffold the following files:
128+
129+
- `App\Domain\Registration`
130+
- `App\Domain\RegistrationId`
131+
- `App\Domain\RegistrationRepository`
132+
- `database/migrations/xxxx_xx_xx_create_registration_domain_messages_table.php`
133+
134+
These are all the files you need to get started with an Aggregate Root.
135+
136+
### Generating Consumers
137+
138+
Laravel EventSauce can also generate consumers for you. For example, run the `make:consumer` command to generate a `SendEmailConfirmation` process manager:
139+
140+
```bash
141+
php artisan make:consumer Domain/SendEmailConfirmation
138142
```
139143

144+
This will create a class at `App\Domain\SendEmailConfirmation` where you can now define `handle{EventName}` methods to handle events.
145+
140146
### Generating Commands & Events
141147

142148
EventSauce can generate commands and events for you so you don't need to write these yourself. First, define a `commands_and_events.yml` file which contains your definitions:
@@ -184,7 +190,96 @@ You can now generate commands and events for all repositories that you've added
184190
php artisan eventsauce:generate
185191
```
186192

187-
For more info on creating events and commands with EventSauce, as well as how to define different types, see: https://eventsauce.io/docs/getting-started/create-events-and-commands
193+
For more info on creating events and commands with EventSauce, as well as how to define different types, [see the EventSauce documentation](https://eventsauce.io/docs/event-sourcing/create-events-and-commands).
194+
195+
## Usage
196+
197+
### Aggregate Roots
198+
199+
More docs coming soon...
200+
201+
### Aggregate Root Repositories
202+
203+
More docs coming soon...
204+
205+
#### Queue Property
206+
207+
You can instruct Laravel to queue all consumers onto a specific queue by setting the `$queue` property:
208+
209+
```php
210+
use App\Domain\SendConfirmationNotification;
211+
use EventSauce\LaravelEventSauce\AggregateRootRepository;
212+
213+
final class RegistrationAggregateRootRepository extends AggregateRootRepository
214+
{
215+
protected array $consumers = [
216+
SendConfirmationNotification::class,
217+
];
218+
219+
protected string $queue = 'registrations';
220+
}
221+
```
222+
223+
This will force all consumers who have the `ShouldQueue` contract implemented to make use of the `registrations` queue instead of the default queue defined in your `queue.php` config file.
224+
225+
### Consumers
226+
227+
Consumers are classes that react to events fired from your aggregate roots. There's two types of consumers: projections and process managers. Projections update read models (think updating data in databases, updating reports,...) while process managers handle one-time tasks (think sending emails, triggering builds, ...). For more information on how to use them, check out EventSauce's [Reacting to Events](https://eventsauce.io/docs/reacting-to-events/setup-consumers/) documentation.
228+
229+
A `SendEmailConfirmation` process manager, for example, can look like this:
230+
231+
```php
232+
use App\Events\UserWasRegistered;
233+
use App\Models\User;
234+
use App\Notifications\NewUserNotification;
235+
use EventSauce\LaravelEventSauce\Consumer;
236+
237+
final class SendConfirmationNotification extends Consumer
238+
{
239+
protected function handleUserWasRegistered(UserWasRegistered $event): void
240+
{
241+
User::where('email', $event->email())
242+
->first()
243+
->notify(new NewUserNotification());
244+
}
245+
}
246+
```
247+
248+
Within this consumer you always define methods following the `handle{EventName}` specification.
249+
250+
#### Registering Consumers
251+
252+
After writing your consumer, you can register them with the `$consumers` property on the related `AggregateRootRepository`:
253+
254+
```php
255+
use App\Domain\SendConfirmationNotification;
256+
use EventSauce\LaravelEventSauce\AggregateRootRepository;
257+
258+
final class RegistrationAggregateRootRepository extends AggregateRootRepository
259+
{
260+
protected array $consumers = [
261+
SendConfirmationNotification::class,
262+
];
263+
}
264+
```
265+
266+
The sequence of adding consumers shouldn't matter as the data handling within these consumers should always be treated as independent from each other.
267+
268+
#### Queueing Consumers
269+
270+
By default, consumers are handled synchronous. To queue a consumer you should implement the `ShouldQueue` contract on your consumer class.
271+
272+
```php
273+
use EventSauce\LaravelEventSauce\Consumer;
274+
use Illuminate\Contracts\Queue\ShouldQueue;
275+
276+
final class SendConfirmationNotification extends Consumer implements ShouldQueue
277+
{
278+
...
279+
}
280+
```
281+
282+
By doing so, we'll instruct Laravel to queue the consumer and let the data handling be done at a later point in time. This is useful to delay long-running data processing.
188283

189284
## Changelog
190285

config/eventsauce.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
* We'll use this info to generate commands and events.
2121
*
2222
* More info on code generation here:
23-
* https://eventsauce.io/docs/getting-started/create-events-and-commands
23+
* https://eventsauce.io/docs/event-sourcing/create-events-and-commands
2424
*/
2525

2626
'repositories' => [

0 commit comments

Comments
 (0)