How to Customize a Method Behavior without Using Inheritance

How to Customize a Method Behavior without Using Inheritance

Doing something before or after a Method Call

If you want to do something right before, or directly after a method is called, you can dispatch an event respectively at the beginning or at the end of the method:

  1. class CustomMailer
  2. {
  3. // ...
  4. public function send($subject, $message)
  5. {
  6. // dispatch an event before the method
  7. $event = new BeforeSendMailEvent($subject, $message);
  8. $this->dispatcher->dispatch($event, 'mailer.pre_send');
  9. // get $subject and $message from the event, they may have been modified
  10. $subject = $event->getSubject();
  11. $message = $event->getMessage();
  12. // the real method implementation is here
  13. $returnValue = ...;
  14. // do something after the method
  15. $event = new AfterSendMailEvent($returnValue);
  16. $this->dispatcher->dispatch($event, 'mailer.post_send');
  17. return $event->getReturnValue();
  18. }
  19. }

In this example, two events are dispatched:

  1. mailer.pre_send, before the method is called,
  2. and mailer.post_send after the method is called.

Each uses a custom Event class to communicate information to the listeners of the two events. For example, BeforeSendMailEvent might look like this:

  1. // src/Event/BeforeSendMailEvent.php
  2. namespace App\Event;
  3. use Symfony\Contracts\EventDispatcher\Event;
  4. class BeforeSendMailEvent extends Event
  5. {
  6. private $subject;
  7. private $message;
  8. public function __construct($subject, $message)
  9. {
  10. $this->subject = $subject;
  11. $this->message = $message;
  12. }
  13. public function getSubject()
  14. {
  15. return $this->subject;
  16. }
  17. public function setSubject($subject)
  18. {
  19. $this->subject = $subject;
  20. }
  21. public function getMessage()
  22. {
  23. return $this->message;
  24. }
  25. public function setMessage($message)
  26. {
  27. $this->message = $message;
  28. }
  29. }

And the AfterSendMailEvent even like this:

  1. // src/Event/AfterSendMailEvent.php
  2. namespace App\Event;
  3. use Symfony\Contracts\EventDispatcher\Event;
  4. class AfterSendMailEvent extends Event
  5. {
  6. private $returnValue;
  7. public function __construct($returnValue)
  8. {
  9. $this->returnValue = $returnValue;
  10. }
  11. public function getReturnValue()
  12. {
  13. return $this->returnValue;
  14. }
  15. public function setReturnValue($returnValue)
  16. {
  17. $this->returnValue = $returnValue;
  18. }
  19. }

Both events allow you to get some information (e.g. getMessage()) and even change that information (e.g. setMessage()).

Now, you can create an event subscriber to hook into this event. For example, you could listen to the mailer.post_send event and change the method’s return value:

  1. // src/EventSubscriber/MailPostSendSubscriber.php
  2. namespace App\EventSubscriber;
  3. use App\Event\AfterSendMailEvent;
  4. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  5. class MailPostSendSubscriber implements EventSubscriberInterface
  6. {
  7. public function onMailerPostSend(AfterSendMailEvent $event)
  8. {
  9. $returnValue = $event->getReturnValue();
  10. // modify the original ``$returnValue`` value
  11. $event->setReturnValue($returnValue);
  12. }
  13. public static function getSubscribedEvents()
  14. {
  15. return [
  16. 'mailer.post_send' => 'onMailerPostSend',
  17. ];
  18. }
  19. }

That’s it! Your subscriber should be called automatically (or read more about event subscriber configuration).

This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.