index.rst 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. Introduction
  2. ============
  3. The Doctrine Event Manager is a simple event system used by the various Doctrine projects. It was originally built
  4. for the DBAL and ORM but over time other projects adopted it and now it is available as a standalone library.
  5. Installation
  6. ============
  7. The library can easily be installed with composer.
  8. .. code-block:: sh
  9. $ composer require doctrine/event-manager
  10. Setup
  11. =====
  12. The event system is controlled by the ``Doctrine\Common\EventManager`` class.
  13. .. code-block:: php
  14. use Doctrine\Common\EventManager;
  15. $eventManager = new EventManager();
  16. Listeners
  17. =========
  18. Now you are ready to listen for events. Here is an example of a custom event listener named ``TestEvent``.
  19. .. code-block:: php
  20. use Doctrine\Common\EventArgs;
  21. use Doctrine\Common\EventManager;
  22. final class TestEvent
  23. {
  24. public const preFoo = 'preFoo';
  25. public const postFoo = 'postFoo';
  26. /** @var EventManager */
  27. private $eventManager;
  28. /** @var bool */
  29. public $preFooInvoked = false;
  30. /** @var bool */
  31. public $postFooInvoked = false;
  32. public function __construct(EventManager $eventManager)
  33. {
  34. $eventManager->addEventListener([self::preFoo, self::postFoo], $this);
  35. }
  36. public function preFoo(EventArgs $eventArgs) : void
  37. {
  38. $this->preFooInvoked = true;
  39. }
  40. public function postFoo(EventArgs $eventArgs) : void
  41. {
  42. $this->postFooInvoked = true;
  43. }
  44. }
  45. // Create a new instance
  46. $testEvent = new TestEvent($eventManager);
  47. Dispatching Events
  48. ==================
  49. Now you can dispatch events with the ``dispatchEvent()`` method.
  50. .. code-block:: php
  51. $eventManager->dispatchEvent(TestEvent::preFoo);
  52. $eventManager->dispatchEvent(TestEvent::postFoo);
  53. Removing Event Listeners
  54. ========================
  55. You can easily remove a listener with the ``removeEventListener()`` method.
  56. .. code-block:: php
  57. $eventManager->removeEventListener([TestEvent::preFoo, TestEvent::postFoo], $testEvent);
  58. Event Subscribers
  59. =================
  60. The Doctrine event system also has a simple concept of event subscribers. We can define a simple ``TestEventSubscriber`` class which implements the ``Doctrine\Common\EventSubscriber`` interface with a ``getSubscribedEvents()`` method which returns an array of events it should be subscribed to.
  61. .. code-block:: php
  62. use Doctrine\Common\EventSubscriber;
  63. final class TestEventSubscriber implements EventSubscriber
  64. {
  65. /** @var bool */
  66. public $preFooInvoked = false;
  67. public function preFoo() : void
  68. {
  69. $this->preFooInvoked = true;
  70. }
  71. public function getSubscribedEvents() : array
  72. {
  73. return [TestEvent::preFoo];
  74. }
  75. }
  76. $eventSubscriber = new TestEventSubscriber();
  77. $eventManager->addEventSubscriber($eventSubscriber);
  78. .. note::
  79. The array returned by the ``getSubscribedEvents()`` method is a simple array with the values being the event names. The subscriber must have a method that is named exactly like the event.
  80. Now when you dispatch an event, any event subscribers will be notified of that event.
  81. .. code-block:: php
  82. $eventManager->dispatchEvent(TestEvent::preFoo);
  83. Now you can check the ``preFooInvoked`` property to see if the event subscriber was notified of the event:
  84. .. code-block:: php
  85. if ($eventSubscriber->preFooInvoked) {
  86. // the preFoo method was invoked
  87. }