InvokerDefault.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. /**
  3. * Default event invoker
  4. *
  5. * Copyright © Magento, Inc. All rights reserved.
  6. * See COPYING.txt for license details.
  7. */
  8. namespace Magento\Framework\Event\Invoker;
  9. use Magento\Framework\Event\Observer;
  10. class InvokerDefault implements \Magento\Framework\Event\InvokerInterface
  11. {
  12. /**
  13. * Observer model factory
  14. *
  15. * @var \Magento\Framework\Event\ObserverFactory
  16. */
  17. protected $_observerFactory;
  18. /**
  19. * Application state
  20. *
  21. * @var \Magento\Framework\App\State
  22. */
  23. protected $_appState;
  24. /**
  25. * @param \Magento\Framework\Event\ObserverFactory $observerFactory
  26. * @param \Magento\Framework\App\State $appState
  27. */
  28. public function __construct(
  29. \Magento\Framework\Event\ObserverFactory $observerFactory,
  30. \Magento\Framework\App\State $appState
  31. ) {
  32. $this->_observerFactory = $observerFactory;
  33. $this->_appState = $appState;
  34. }
  35. /**
  36. * Dispatch event
  37. *
  38. * @param array $configuration
  39. * @param Observer $observer
  40. * @return void
  41. */
  42. public function dispatch(array $configuration, Observer $observer)
  43. {
  44. /** Check whether event observer is disabled */
  45. if (isset($configuration['disabled']) && true === $configuration['disabled']) {
  46. return;
  47. }
  48. if (isset($configuration['shared']) && false === $configuration['shared']) {
  49. $object = $this->_observerFactory->create($configuration['instance']);
  50. } else {
  51. $object = $this->_observerFactory->get($configuration['instance']);
  52. }
  53. $this->_callObserverMethod($object, $observer);
  54. }
  55. /**
  56. * @param \Magento\Framework\Event\ObserverInterface $object
  57. * @param Observer $observer
  58. * @return $this
  59. * @throws \LogicException
  60. */
  61. protected function _callObserverMethod($object, $observer)
  62. {
  63. if ($object instanceof \Magento\Framework\Event\ObserverInterface) {
  64. $object->execute($observer);
  65. } elseif ($this->_appState->getMode() == \Magento\Framework\App\State::MODE_DEVELOPER) {
  66. throw new \LogicException(
  67. sprintf(
  68. 'Observer "%s" must implement interface "%s"',
  69. get_class($object),
  70. \Magento\Framework\Event\ObserverInterface::class
  71. )
  72. );
  73. }
  74. return $this;
  75. }
  76. }