Skip to content

Commit 8845038

Browse files
authored
Merge pull request #39 from UFOMelkor/feature/documentation
Documentation
2 parents b613586 + 404a1a1 commit 8845038

File tree

8 files changed

+256
-85
lines changed

8 files changed

+256
-85
lines changed

doc/bookdown.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
"content": [
44
{"intro": "../README.md"},
55
{"getting_started": "getting_started.md"},
6+
{"routing": "routing.md"},
7+
{"plugins": "plugins.md"},
8+
{"profiler": "profiler.md"},
69
{"configuration_reference": "configuration_reference.md"}
710
],
811
"target": "./html",

doc/getting_started.md

Lines changed: 34 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
This documentation covers just the configuration of the Prooph Service Bus in Symfony.
44
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/).
66

77
## Download the Bundle
88

@@ -16,6 +16,7 @@ at the root of your Symfony project.
1616

1717
To start using this bundle, register the bundle in your application's kernel class:
1818
```php
19+
<?php
1920
// app/AppKernel.php
2021
// …
2122
class AppKernel extends Kernel
@@ -34,127 +35,69 @@ class AppKernel extends Kernel
3435
}
3536
```
3637

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+
3749
## Configure your first command bus
3850

3951
There are three different types of message bus supported by the ProophServiceBus.
4052
While they have totally different purposes, their configuration is nearly the same.
4153
As an example, we will configure a command bus.
4254
For query bus and event bus, please have a look at the [configuration reference](./configuration_reference.html).
4355

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):
4558
```yaml
46-
# app/config/config.yml
4759
prooph_service_bus:
4860
command_buses:
4961
acme_command_bus: ~
5062
```
5163
5264
That's all you need to define a (useless) command bus. Let's make it useful.
5365
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
7567
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:
8371
```yaml
84-
# app/config/services.yml
72+
# app/config/services.yml or (flex) config/packages/prooph_service_bus.yaml
8573
services:
8674
acme.command.register_user_handler:
8775
class: Acme\Command\RegisterUserHandler
88-
tags:
89-
- { name: 'prooph_service_bus.acme_command_bus.route_target' }
9076
```
91-
The bundle will try to detect the name of the command by itself.
9277

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:
9479
```yaml
95-
# app/config/services.yml
9680
services:
9781
acme.command.register_user_handler:
9882
class: Acme\Command\RegisterUserHandler
9983
tags:
100-
- { name: 'prooph_service_bus.acme_command_bus.route_target', message: 'Acme\Command\RegisterUser' }
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\ServiceBus\Plugin\InvokeStrategy\HandleCommandStrategy
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\ServiceBus\Plugin\InvokeStrategy\HandleCommandStrategy
140-
tags:
141-
- { name: 'prooph_service_bus.acme_command_bus.plugin' }
84+
- { name: 'prooph_service_bus.acme_command_bus.route_target' }
14285
```
14386

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.
14788

148-
## Access the command bus
89+
## Dispatching the command
14990

15091
Given our configuration
15192
```yaml
152-
# app/config/config.yml
93+
# app/config/config.yml or (flex) config/packages/prooph_service_bus.yaml
15394
prooph_service_bus:
15495
command_buses:
15596
acme_command_bus: ~
15697
```
98+
15799
we can access the command bus from the container using the ID `prooph_service_bus.acme_command_bus`:
100+
158101
```php
159102
<?php
160103
// …
@@ -163,9 +106,15 @@ class RegisterUserController extends Controller
163106
public function indexAction()
164107
{
165108
// …
166-
$this->get('prooph_service_bus.acme_command_bus')
109+
$this
110+
->get('prooph_service_bus.acme_command_bus')
167111
->dispatch(new RegisterUser(/* … */));
168112
// …
169113
}
170114
}
171115
```
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)?

doc/plugins.md

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
# Plugins
2+
3+
Plugins are a great way to expand a message bus with additional functionality.
4+
You can find more information about PSB plugins and a list of included plugins in the
5+
[documentation of the Service Bus](http://docs.getprooph.org/service-bus/plugins.html) itself.
6+
7+
There are two ways to attach a plugin to a bus.
8+
You can configure it using a tag at the plugin definition or directly at the bus configuration.
9+
10+
We will show you the configuration using the example of the
11+
`Prooph\ServiceBus\Plugin\InvokeStrategy\HandleCommandStrategy` – which is part of the PSB –
12+
but de facto each plugin will be attached the same way.
13+
14+
The basis for both ways is the service definition of the plugin:
15+
```yaml
16+
# app/config/services.yml
17+
services:
18+
acme.prooph.plugin.handle_command_strategy:
19+
class: Prooph\ServiceBus\Plugin\InvokeStrategy\HandleCommandStrategy
20+
```
21+
22+
and a configured message bus:
23+
```yaml
24+
# app/config/config.yml or (flex) config/packages/prooph_service_bus.yaml
25+
prooph_service_bus:
26+
command_buses:
27+
acme_command_bus: ~
28+
```
29+
The command bus is used as example again, plugins are attached to each bus the same way.
30+
31+
## Definition using tags
32+
33+
If you don't know about tags in Symfony please have a look at the
34+
[official documentation](http://symfony.com/doc/current/service_container/tags.html).
35+
36+
To attach a plugin to a specific message bus, we just need to add a tag to its service definition:
37+
```yaml
38+
# app/config/services.yml
39+
services:
40+
acme.prooph.plugin.handle_command_strategy:
41+
class: Prooph\ServiceBus\Plugin\InvokeStrategy\HandleCommandStrategy
42+
tags:
43+
- { name: 'prooph_service_bus.acme_command_bus.plugin' }
44+
```
45+
46+
The name of the tag is simple `prooph_service_bus.<name-of-the-bus>.plugin`.
47+
There are no additional attributes that can be set.
48+
49+
But sometimes you want to attach a plugin to more than one message bus, e.g. for logging.
50+
Of course you could add multiple tags, but for two use cases there are special tags:
51+
52+
- To attach the plugin to every command bus, you can use the tag `prooph_service_bus.command_bus.plugin`
53+
(resp. `prooph_service_bus.query_bus.plugin` for every query bus and `prooph_service_bus.event_bus.plugin` for every event bus).
54+
- To attach the plugin to every message bus (no matter whether it is a command bus, a query bus or an event bus)
55+
you can use the tag `prooph_service_bus.plugin`.
56+
57+
That is everything you need to know about attaching plugins to a message bus using tags.
58+
59+
Let's now have a look at the other way.
60+
61+
## Definition at the bus
62+
63+
If you are no fan of service tags, you can attach the plugin directly at the bus configuration:
64+
```yaml
65+
# app/config/config.yml or (flex) config/packages/prooph_service_bus.yaml
66+
prooph_service_bus:
67+
command_buses:
68+
acme_command_bus:
69+
plugins:
70+
- "acme.prooph.plugin.handle_command_strategy"
71+
```
72+
Again this will work the same way for query buses and event buses.
73+
74+
> **Hint:** To get autocompletion in some IDEs you can prepend the service id
75+
> with an `@` (`"@acme.prooph.plugin.handle_command_strategy"`).
76+
>
77+
> The bundle will recognize this and find your plugin anyway.
78+
79+
If you attach your plugins directly at the bus configuration, they will be at a central place.
80+
But there is no way to attach them to every message bus or every message bus of a type like when using tags.

doc/profiler.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Symfony Profiler
2+
3+
The ProophServiceBusBundle provides several plugins that help you to inspect your application.
4+
For most of them you will need access to the Symfony Profiler, so please ensure that you have installed the
5+
[WebProfilerBundle](https://packagist.org/packages/symfony/web-profiler-bundle).
6+
7+
## DataCollectorPlugin
8+
9+
The DataCollectorPlugin gathers data about the dispatched messages and shows them in an extra section within
10+
the Symfony Profiler.
11+
There is one profiler for the command- and one for the event-buses
12+
and both are automatically enabled if `kernel.debug` is `true`.
13+
14+
![Example of a timeline with a command and an event](profiler_data_collector_sections.png)
15+
16+
## PsrLoggerPlugin
17+
18+
The PsrLoggerPlugin fills your log with information about your dispatched messages.
19+
There will be one PsrLoggerPlugin automatically registered for each defined message bus.
20+
21+
You can find the logged messages either in the *Logs* section of the Symfony Profiler or directly in your log
22+
(depending on how your logging is configured).
23+
24+
![Example of a timeline with a command and an event](profiler_logs.png)
25+
26+
## StopwatchPlugin
27+
28+
The StopwatchPlugin is automatically enabled if `kernel.debug` is `true`
29+
(which is the case e.g. in the `dev` environment).
30+
It times the execution time of your command- and event-handlers.
31+
The collected data are shown within the *Performance* section of the Symfony Profiler.
32+
33+
For an example with a executed command and an executed event please have a look at the following image:
34+
35+
![Example of a timeline with a command and an event](profiler_timeline.png)
9.07 KB
Loading

doc/profiler_logs.png

97.4 KB
Loading

doc/profiler_timeline.png

24.4 KB
Loading

0 commit comments

Comments
 (0)