EventDispatcher.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. <?php
  2. declare(strict_types=1);
  3. namespace Doctrine\Migrations;
  4. use Doctrine\Common\EventArgs;
  5. use Doctrine\Common\EventManager;
  6. use Doctrine\DBAL\Connection;
  7. use Doctrine\Migrations\Event\MigrationsEventArgs;
  8. use Doctrine\Migrations\Event\MigrationsVersionEventArgs;
  9. use Doctrine\Migrations\Metadata\MigrationPlan;
  10. use Doctrine\Migrations\Metadata\MigrationPlanList;
  11. /**
  12. * The EventDispatcher class is responsible for dispatching events internally that a user can listen for.
  13. *
  14. * @internal
  15. */
  16. final class EventDispatcher
  17. {
  18. /** @var EventManager */
  19. private $eventManager;
  20. /** @var Connection */
  21. private $connection;
  22. public function __construct(Connection $connection, EventManager $eventManager)
  23. {
  24. $this->eventManager = $eventManager;
  25. $this->connection = $connection;
  26. }
  27. public function dispatchMigrationEvent(
  28. string $eventName,
  29. MigrationPlanList $migrationsPlan,
  30. MigratorConfiguration $migratorConfiguration
  31. ): void {
  32. $event = $this->createMigrationEventArgs($migrationsPlan, $migratorConfiguration);
  33. $this->dispatchEvent($eventName, $event);
  34. }
  35. public function dispatchVersionEvent(
  36. string $eventName,
  37. MigrationPlan $plan,
  38. MigratorConfiguration $migratorConfiguration
  39. ): void {
  40. $event = $this->createMigrationsVersionEventArgs(
  41. $plan,
  42. $migratorConfiguration
  43. );
  44. $this->dispatchEvent($eventName, $event);
  45. }
  46. private function dispatchEvent(string $eventName, ?EventArgs $args = null): void
  47. {
  48. $this->eventManager->dispatchEvent($eventName, $args);
  49. }
  50. private function createMigrationEventArgs(
  51. MigrationPlanList $migrationsPlan,
  52. MigratorConfiguration $migratorConfiguration
  53. ): MigrationsEventArgs {
  54. return new MigrationsEventArgs($this->connection, $migrationsPlan, $migratorConfiguration);
  55. }
  56. private function createMigrationsVersionEventArgs(
  57. MigrationPlan $plan,
  58. MigratorConfiguration $migratorConfiguration
  59. ): MigrationsVersionEventArgs {
  60. return new MigrationsVersionEventArgs(
  61. $this->connection,
  62. $plan,
  63. $migratorConfiguration
  64. );
  65. }
  66. }