events.rst 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. Migrations Events
  2. =================
  3. The Doctrine Migrations library emits a series of events during the migration process.
  4. - ``onMigrationsMigrating``: dispatched immediately before starting to execute versions. This does not fire if
  5. there are no versions to be executed.
  6. - ``onMigrationsVersionExecuting``: dispatched before a single version executes.
  7. - ``onMigrationsVersionExecuted``: dispatched after a single version executes.
  8. - ``onMigrationsVersionSkipped``: dispatched when a single version is skipped.
  9. - ``onMigrationsMigrated``: dispatched when all versions have been executed.
  10. All of these events are emitted via the DBAL connection's event manager. Here's an example event subscriber that
  11. listens for all possible migrations events.
  12. .. code-block:: php
  13. <?php
  14. use Doctrine\Common\EventSubscriber;
  15. use Doctrine\Migrations\Event\MigrationsEventArgs;
  16. use Doctrine\Migrations\Event\MigrationsVersionEventArgs;
  17. use Doctrine\Migrations\Events;
  18. class MigrationsListener implements EventSubscriber
  19. {
  20. public function getSubscribedEvents() : array
  21. {
  22. return [
  23. Events::onMigrationsMigrating,
  24. Events::onMigrationsMigrated,
  25. Events::onMigrationsVersionExecuting,
  26. Events::onMigrationsVersionExecuted,
  27. Events::onMigrationsVersionSkipped,
  28. ];
  29. }
  30. public function onMigrationsMigrating(MigrationsEventArgs $args) : void
  31. {
  32. // ...
  33. }
  34. public function onMigrationsMigrated(MigrationsEventArgs $args) : void
  35. {
  36. // ...
  37. }
  38. public function onMigrationsVersionExecuting(MigrationsVersionEventArgs $args) : void
  39. {
  40. // ...
  41. }
  42. public function onMigrationsVersionExecuted(MigrationsVersionEventArgs $args) : void
  43. {
  44. // ...
  45. }
  46. public function onMigrationsVersionSkipped(MigrationsVersionEventArgs $args) : void
  47. {
  48. // ...
  49. }
  50. }
  51. To add an event subscriber to a connections event manager, use the ``Connection::getEventManager()`` method
  52. and the ``EventManager::addEventSubscriber()`` method:
  53. This might go in the ``cli-config.php`` file or somewhere in a frameworks container or dependency injection configuration.
  54. .. code-block:: php
  55. <?php
  56. use Doctrine\DBAL\DriverManager;
  57. $connection = DriverManager::getConnection([
  58. // ...
  59. ]);
  60. $connection->getEventManager()->addEventSubscriber(new MigrationsListener());
  61. // rest of the cli set up...
  62. :ref:`Next Chapter: Version Numbers <version-numbers>`