2
2
3
3
This documentation covers just the configuration of the Prooph Service Bus in Symfony.
4
4
To inform yourself about the ProophServiceBus please have a look at the
5
- [ official documentation] ( http://getprooph.org/service-bus/intro.html ) .
5
+ [ official documentation] ( http://docs. getprooph.org/service-bus/ ) .
6
6
7
7
## Download the Bundle
8
8
@@ -16,6 +16,7 @@ at the root of your Symfony project.
16
16
17
17
To start using this bundle, register the bundle in your application's kernel class:
18
18
``` php
19
+ <?php
19
20
// app/AppKernel.php
20
21
// …
21
22
class AppKernel extends Kernel
@@ -34,127 +35,69 @@ class AppKernel extends Kernel
34
35
}
35
36
```
36
37
38
+ or, if you are using [ the new flex structure] ( https://symfony.com/doc/current/setup/flex.html ) :
39
+ ``` php
40
+ <?php
41
+ // config/bundles.php
42
+
43
+ return [
44
+ // …
45
+ Prooph\Bundle\ServiceBus\ProophServiceBusBundle::class => ['all' => true],
46
+ ];
47
+ ```
48
+
37
49
## Configure your first command bus
38
50
39
51
There are three different types of message bus supported by the ProophServiceBus.
40
52
While they have totally different purposes, their configuration is nearly the same.
41
53
As an example, we will configure a command bus.
42
54
For query bus and event bus, please have a look at the [ configuration reference] ( ./configuration_reference.html ) .
43
55
44
- The command bus is configured in ` app/config/config.yml ` :
56
+ The command bus is configured in ` app/config/config.yml `
57
+ (or ` config/packages/prooph_service_bus.yaml ` if you are using flex):
45
58
``` yaml
46
- # app/config/config.yml
47
59
prooph_service_bus :
48
60
command_buses :
49
61
acme_command_bus : ~
50
62
` ` `
51
63
52
64
That's all you need to define a (useless) command bus. Let's make it useful.
53
65
54
- ### Route to a command handler
55
-
56
- There are two ways to route a command to a command handler.
57
- You can simply add it to the ProophServiceBus configuration:
58
-
59
- ` ` ` yaml
60
- # app/config/config.yml
61
- prooph_service_bus :
62
- command_buses :
63
- acme_command_bus :
64
- router :
65
- routes :
66
- ' Acme\Command\RegisterUser ' : ' @acme.command.register_user_handler'
67
- ` ` `
68
-
69
- In this case ` Acme\Command\RegisterUser` would be the name of your command (which usually corresponds to its class name)
70
- and `acme.command.register_user_handler` the service-id of the handler (that you have to normally configure in Symfony).
71
- The `@` before the service-id can be omitted, but it provides auto completion in some IDEs.
72
-
73
- > **Note**: When configuring the event bus you can pass an array of service IDs for each event instead of a single service ID.
74
- > This is necessary because events can be routed to multiple event handlers.
66
+ ### Route your first command handler
75
67
76
- The main benefit of this way is that you have all command handlers registered in one place.
77
- But it has also some drawbacks :
78
- You have to configure every command handler for itself and add it to the routes of the command bus,
79
- which implies changes at two different places for adding one command handler.
80
- Also, because the name of the command usually corresponds to the class of the command, it is vulnerable for refactoring.
81
-
82
- Therefore, you can also route a command to a command handler using tags. This will look like this :
68
+ We assume that you already have a command ` Acme\Command\RegisterUser`
69
+ and a command handler `Acme\Command\RegisterUserHandler`.
70
+ We will define the command handler as a regular service in Symfony :
83
71
` ` ` yaml
84
- # app/config/services.yml
72
+ # app/config/services.yml or (flex) config/packages/prooph_service_bus.yaml
85
73
services:
86
74
acme.command.register_user_handler:
87
75
class: Acme\C ommand\R egisterUserHandler
88
- tags:
89
- - { name: 'prooph_service_bus.acme_command_bus.route_target' }
90
76
` ` `
91
- The bundle will try to detect the name of the command by itself.
92
77
93
- If this feels like too much magic or if the detection fails, you can still pass the name of the command as attribute :
78
+ To route the command `Acme\Command\RegisterUser` to this service, we just need to add a tag to this definition :
94
79
` ` ` yaml
95
- # app/config/services.yml
96
80
services:
97
81
acme.command.register_user_handler:
98
82
class: Acme\C ommand\R egisterUserHandler
99
83
tags:
100
- - { name: 'prooph_service_bus.acme_command_bus.route_target', message: 'Acme\C ommand\R egisterUser' }
101
- ` ` `
102
-
103
- > **Hint:** If you rely on automatic message detection and your handler handles multiple messages of the same message bus,
104
- > you need to tag the handler just once.
105
-
106
- Both options have its advantages and disadvantages.
107
- The result is the same, so it's up to your personal preference which option you choose.
108
-
109
- # ## Add a plugin
110
-
111
- [Plugins](http://getprooph.org/service-bus/plugins.html) are a great way to expand a message bus.
112
- Let's assume that we want to use the `HandleCommandStrategy` for our command bus.
113
- Again, there are two options to do this.
114
- Both require to define a service for the plugin :
115
- ` ` ` yaml
116
- # app/config/services.yml
117
- services:
118
- acme.prooph.plugin.handle_command_strategy:
119
- class: Prooph\S erviceBus\P lugin\I nvokeStrategy\H andleCommandStrategy
120
- ` ` `
121
-
122
- Let's start with the first option, modifying our `app/config/config.yml` :
123
- ` ` ` yaml
124
- # app/config/config.yml
125
- prooph_service_bus:
126
- command_buses:
127
- acme_command_bus:
128
- plugins:
129
- - "acme.prooph.plugin.handle_command_strategy"
130
- ` ` `
131
- That is all you need to do to register the plugin.
132
-
133
- Now we will have a look at the other option, using tags.
134
- Therefore, we just need to add a tag to the service configuration :
135
- ` ` ` yaml
136
- # app/config/services.yml
137
- services:
138
- acme.prooph.plugin.handle_command_strategy:
139
- class: Prooph\S erviceBus\P lugin\I nvokeStrategy\H andleCommandStrategy
140
- tags:
141
- - { name: 'prooph_service_bus.acme_command_bus.plugin' }
84
+ - { name: 'prooph_service_bus.acme_command_bus.route_target' }
142
85
` ` `
143
86
144
- > **Hint:** If you want to register the plugin for more than one message bus, you can use
145
- > - `prooph_service_bus.command_bus.plugin` to register it for every command bus (resp. `prooph_service_bus.query_bus.plugin` and `prooph_service_bus.event_bus.plugin`) or
146
- > - `prooph_service_bus.plugin` to register it for every message bus.
87
+ Now we are ready to dispatch our command.
147
88
148
- # # Access the command bus
89
+ # # Dispatching the command
149
90
150
91
Given our configuration
151
92
` ` ` yaml
152
- # app/config/config.yml
93
+ # app/config/config.yml or (flex) config/packages/prooph_service_bus.yaml
153
94
prooph_service_bus:
154
95
command_buses:
155
96
acme_command_bus: ~
156
97
` ` `
98
+
157
99
we can access the command bus from the container using the ID `prooph_service_bus.acme_command_bus` :
100
+
158
101
` ` ` php
159
102
<?php
160
103
// …
@@ -163,9 +106,15 @@ class RegisterUserController extends Controller
163
106
public function indexAction()
164
107
{
165
108
// …
166
- $this->get('prooph_service_bus.acme_command_bus')
109
+ $this
110
+ ->get('prooph_service_bus.acme_command_bus')
167
111
->dispatch(new RegisterUser(/* … */));
168
112
// …
169
113
}
170
114
}
171
115
` ` `
116
+
117
+ That was everything we need to configure and dispatch our first command.
118
+ Perhaps you want to know more about [routing](./routing.html),
119
+ about how to customize message buses with [plugins](./plugins.html)
120
+ or about what information will be included in the [profiler](./profiler.html)?
0 commit comments