@@ -24,11 +24,9 @@ use Patchlevel\EventSourcing\Projection\ProjectorId;
24
24
25
25
final class ProfileProjection implements Projector
26
26
{
27
- private Connection $connection;
28
-
29
- public function __construct(Connection $connection)
30
- {
31
- $this->connection = $connection;
27
+ public function __construct(
28
+ private readonly Connection $connection
29
+ ) {
32
30
}
33
31
34
32
public function projectorId(): ProjectorId
@@ -67,6 +65,21 @@ final class ProfileProjection implements Projector
67
65
}
68
66
```
69
67
68
+ Each projector needs an ` projectorId ` composed of a unique name and a version number.
69
+ With the help of this information, a projection can be clearly identified. More on that later.
70
+
71
+ Projectors can also have one ` create ` and ` drop ` method that is executed when the projection is created or deleted.
72
+ In some cases it may be that no schema has to be created for the projection, as the target does it automatically.
73
+ To do this, you must add either the ` Create ` or ` Drop ` attribute to the method. The method name itself doesn't matter.
74
+
75
+ Otherwise, a projector can have any number of handle methods that are called for certain defined events.
76
+ In order to say which method is responsible for which event, you need the ` Handle ` attribute.
77
+ As the first parameter, you must pass the event class to which the reaction should then take place.
78
+ The method itself must expect a ` Message ` , which then contains the event. The method name itself doesn't matter.
79
+
80
+ As soon as the event has been dispatched, the appropriate methods are then executed.
81
+ Several projectors can also listen to the same event.
82
+
70
83
!!! danger
71
84
72
85
You should not execute any actions with projectors,
@@ -77,68 +90,75 @@ final class ProfileProjection implements Projector
77
90
If you are using psalm then you can install the event sourcing [plugin](https://github.com/patchlevel/event-sourcing-psalm-plugin)
78
91
to make the event method return the correct type.
79
92
80
- Projectors have a ` create ` and a ` drop ` method that is executed when the projection is created or deleted.
81
- In some cases it may be that no schema has to be created for the projection, as the target does it automatically.
93
+ ## Projector Repository
82
94
83
- In order for the projector to know which method is responsible for which event,
84
- the methods must be given the ` Handle ` attribute with the respective event class name.
85
-
86
- As soon as the event has been dispatched, the appropriate methods are then executed.
87
- Several projectors can also listen to the same event.
88
-
89
- ## Register projector
90
-
91
- So that the projectors are known and also executed, you have to add them to the ` ProjectionHandler ` .
92
- Then add this to the event bus using the ` ProjectionListener ` .
95
+ The projector repository can hold and make available all projectors.
93
96
94
97
``` php
95
- use Patchlevel\EventSourcing\EventBus\DefaultEventBus;
96
- use Patchlevel\EventSourcing\Projection\MetadataAwareProjectionHandler;
97
- use Patchlevel\EventSourcing\Projection\ProjectionListener;
98
-
99
- $profileProjection = new ProfileProjection($connection);
100
- $messageProjection = new MessageProjection($connection);
98
+ use Patchlevel\EventSourcing\Projection\DefaultProjectorRepository;
101
99
102
- $projectionHandler = new MetadataAwareProjectionHandler([
103
- $profileProjection,
104
- $messageProjection,
100
+ $projectorRepository = new DefaultProjectorRepository([
101
+ new ProfileProjection($connection)
105
102
]);
106
-
107
- $eventBus->addListener(new ProjectionListener($projectionHandler));
108
103
```
109
104
110
- !!! note
111
-
112
- You can find out more about the event bus [here](./event_bus.md).
113
-
114
105
## Setup Projection
115
106
116
107
A projection schama or database usually has to be created beforehand.
117
108
And with a rebuild, the projection has to be deleted.
118
- To make this possible, projections have two methods ` create ` and ` drop ` that can be defined and executed.
109
+ The Projector Helper can help with this:
119
110
120
111
### Create Projection Schema
121
112
122
- Or for all projections in the ` MetadataAwareProjectionHandler ` :
113
+ With this you can prepare the projection :
123
114
124
115
``` php
125
- $projectionRepository = new MetadataAwareProjectionHandler([
126
- $profileProjection,
127
- $messageProjection,
128
- ]);
116
+ use Patchlevel\EventSourcing\Projection\ProjectorHelper;
129
117
130
- $projectionRepository->create();
118
+ (new ProjectorHelper())->createProjection(new ProfileProjection($connection));
119
+ (new ProjectorHelper())->createProjection(...$projectionRepository->projectors());
131
120
```
132
121
133
122
### Drop Projection Schema
134
123
135
- Or for all projections in the ` MetadataAwareProjectionHandler ` :
124
+ The projection can also be removed again :
136
125
137
126
``` php
138
- $projectionRepository = new MetadataAwareProjectionHandler([
139
- $profileProjection,
140
- $messageProjection,
141
- ]);
127
+ use Patchlevel\EventSourcing\Projection\ProjectorHelper;
142
128
143
- $projectionRepository->drop();
129
+ (new ProjectorHelper())->dropProjection(new ProfileProjection($connection));
130
+ (new ProjectorHelper())->dropProjection(...$projectionRepository->projectors());
144
131
```
132
+
133
+ ## Handle Message
134
+
135
+ The helper also offers methods to process messages:
136
+
137
+ ``` php
138
+ use Patchlevel\EventSourcing\Projection\ProjectorHelper;
139
+
140
+ (new ProjectorHelper())->handleMessage($message, new ProfileProjection($connection));
141
+ (new ProjectorHelper())->handleMessage($message, ...$projectionRepository->projectors());
142
+ ```
143
+
144
+ ## Sync Projector Listener
145
+
146
+ The simplest configuration is to run the projectors synchronously.
147
+ Says that you listen to the event bus and update the projections directly.
148
+ Here you can use the ` SyncProjectorListener ` .
149
+
150
+ ``` php
151
+ use Patchlevel\EventSourcing\Projection\SyncProjectorListener
152
+
153
+ $eventBus->addListener(
154
+ new SyncProjectorListener($projectorRepository)
155
+ );
156
+ ```
157
+
158
+ !!! note
159
+
160
+ You can find out more about the event bus [here](./event_bus.md).
161
+
162
+ !!! note
163
+
164
+ In order to exploit the full potential, the [projectionist](./projectionist.md) should be used in production.
0 commit comments