Processor.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\View\Element\UiComponent;
  7. use Magento\Framework\View\Element\UiComponentInterface;
  8. /**
  9. * Class Processor
  10. */
  11. class Processor implements PoolInterface, SubjectInterface
  12. {
  13. /**
  14. * @var UiComponentInterface[]
  15. */
  16. protected $components = [];
  17. /**
  18. * Array of observers
  19. *
  20. * [
  21. * 'component_type1' => ObserverInterface[],
  22. * 'component_type2' => ObserverInterface[],
  23. * ]
  24. *
  25. * @var array
  26. */
  27. protected $observers = [];
  28. /**
  29. * @inheritDoc
  30. */
  31. public function register(UiComponentInterface $component)
  32. {
  33. $this->components[] = $component;
  34. }
  35. /**
  36. * @inheritDoc
  37. */
  38. public function getComponents()
  39. {
  40. return $this->components;
  41. }
  42. /**
  43. * @inheritDoc
  44. */
  45. public function attach($type, ObserverInterface $observer)
  46. {
  47. $this->observers[$type][] = $observer;
  48. }
  49. /**
  50. * @inheritDoc
  51. */
  52. public function detach($type, ObserverInterface $observer)
  53. {
  54. if (!isset($this->observers[$type])) {
  55. return;
  56. }
  57. $key = array_search($observer, $this->observers[$type], true);
  58. if ($key !== false) {
  59. unset($this->observers[$type][$key]);
  60. }
  61. }
  62. /**
  63. * @inheritDoc
  64. */
  65. public function notify($type)
  66. {
  67. $componentType = $this->normalizeType($type);
  68. if (!isset($this->observers[$componentType])) {
  69. return;
  70. }
  71. /** @var UiComponentInterface $component */
  72. foreach ($this->getComponents() as $component) {
  73. if ($component->getComponentName() != $type) {
  74. continue;
  75. }
  76. /** @var ObserverInterface $observer */
  77. foreach ($this->observers[$componentType] as $observer) {
  78. $observer->update($component);
  79. }
  80. }
  81. }
  82. /**
  83. * Normalize type to component type
  84. *
  85. * @param string $type
  86. * @return string
  87. */
  88. protected function normalizeType($type)
  89. {
  90. $componentType = (strpos($type, '.') !== false) ? substr($type, 0, strpos($type, '.')) : $type;
  91. return $componentType;
  92. }
  93. }