vendor/sylius/resource-bundle/src/Bundle/Controller/EventDispatcher.php line 71

Open in your IDE?
  1. <?php
  2. /*
  3. * This file is part of the Sylius package.
  4. *
  5. * (c) Paweł Jędrzejewski
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. declare(strict_types=1);
  11. namespace Sylius\Bundle\ResourceBundle\Controller;
  12. use Sylius\Bundle\ResourceBundle\Event\ResourceControllerEvent;
  13. use Sylius\Component\Resource\Model\ResourceInterface;
  14. use Symfony\Contracts\EventDispatcher\EventDispatcherInterface as SymfonyEventDispatcherInterface;
  15. final class EventDispatcher implements EventDispatcherInterface
  16. {
  17. private SymfonyEventDispatcherInterface $eventDispatcher;
  18. public function __construct(SymfonyEventDispatcherInterface $eventDispatcher)
  19. {
  20. $this->eventDispatcher = $eventDispatcher;
  21. }
  22. public function dispatch(
  23. string $eventName,
  24. RequestConfiguration $requestConfiguration,
  25. ResourceInterface $resource,
  26. ): ResourceControllerEvent {
  27. $eventName = $requestConfiguration->getEvent() ?: $eventName;
  28. $metadata = $requestConfiguration->getMetadata();
  29. $event = new ResourceControllerEvent($resource);
  30. $this->eventDispatcher->dispatch($event, sprintf('%s.%s.%s', $metadata->getApplicationName(), $metadata->getName(), $eventName));
  31. return $event;
  32. }
  33. public function dispatchMultiple(
  34. string $eventName,
  35. RequestConfiguration $requestConfiguration,
  36. $resources,
  37. ): ResourceControllerEvent {
  38. $eventName = $requestConfiguration->getEvent() ?: $eventName;
  39. $metadata = $requestConfiguration->getMetadata();
  40. $event = new ResourceControllerEvent($resources);
  41. $this->eventDispatcher->dispatch($event, sprintf('%s.%s.%s', $metadata->getApplicationName(), $metadata->getName(), $eventName));
  42. return $event;
  43. }
  44. public function dispatchPreEvent(
  45. string $eventName,
  46. RequestConfiguration $requestConfiguration,
  47. ResourceInterface $resource,
  48. ): ResourceControllerEvent {
  49. $eventName = $requestConfiguration->getEvent() ?: $eventName;
  50. $metadata = $requestConfiguration->getMetadata();
  51. $event = new ResourceControllerEvent($resource);
  52. $this->eventDispatcher->dispatch($event, sprintf('%s.%s.pre_%s', $metadata->getApplicationName(), $metadata->getName(), $eventName));
  53. return $event;
  54. }
  55. public function dispatchPostEvent(
  56. string $eventName,
  57. RequestConfiguration $requestConfiguration,
  58. ResourceInterface $resource,
  59. ): ResourceControllerEvent {
  60. $eventName = $requestConfiguration->getEvent() ?: $eventName;
  61. $metadata = $requestConfiguration->getMetadata();
  62. $event = new ResourceControllerEvent($resource);
  63. $this->eventDispatcher->dispatch($event, sprintf('%s.%s.post_%s', $metadata->getApplicationName(), $metadata->getName(), $eventName));
  64. return $event;
  65. }
  66. public function dispatchInitializeEvent(
  67. string $eventName,
  68. RequestConfiguration $requestConfiguration,
  69. ResourceInterface $resource,
  70. ): ResourceControllerEvent {
  71. $eventName = $requestConfiguration->getEvent() ?: $eventName;
  72. $metadata = $requestConfiguration->getMetadata();
  73. $event = new ResourceControllerEvent($resource);
  74. $this->eventDispatcher->dispatch(
  75. $event,
  76. sprintf('%s.%s.initialize_%s', $metadata->getApplicationName(), $metadata->getName(), $eventName),
  77. );
  78. return $event;
  79. }
  80. }