Powerful decorating
One of awesome things about Symfony2 container is that you can decorate services. In my opinion it is really awesome and powerful - you can simply change behavior of literally any component of Symfony, like EventDispatcher of HttpKernel. It's not too good to tweak them on daily basis, but sometimes you hit edge case when this trick is a life saver. In the end it's nothing more than:
- Extending class of service you want to alter.
- Making changes - adding methods or overriding exisiting ones.
- Writing new service definition.
Here's original dispatch() method definition (as of Symfony 2.8):
We cannot edit this method in place, so let's create our own class that extends CoontainerAwareEventDispatcher and overrides it:
Now the last part, service definition:
Note that you should not worry about changed service id. Symfony will do the work for you and you will get your modified dispatcher when you try to get event_dispatcher from container.
Of course this is not the only usage of service decorators. You can also use it to, for example, pass some extra services to class you extend, or replace some service definitions. It is also possible to add some public methods, but my suggestion is to do this only when really neccesary - this way you change original vendor's API which can change or lead other developers into confusion.
I hope you found above stuff useful. Happy coding!