123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Framework\View\Element\UiComponent;
- use Magento\Framework\View\Element\UiComponentInterface;
- /**
- * Class Processor
- */
- class Processor implements PoolInterface, SubjectInterface
- {
- /**
- * @var UiComponentInterface[]
- */
- protected $components = [];
- /**
- * Array of observers
- *
- * [
- * 'component_type1' => ObserverInterface[],
- * 'component_type2' => ObserverInterface[],
- * ]
- *
- * @var array
- */
- protected $observers = [];
- /**
- * @inheritDoc
- */
- public function register(UiComponentInterface $component)
- {
- $this->components[] = $component;
- }
- /**
- * @inheritDoc
- */
- public function getComponents()
- {
- return $this->components;
- }
- /**
- * @inheritDoc
- */
- public function attach($type, ObserverInterface $observer)
- {
- $this->observers[$type][] = $observer;
- }
- /**
- * @inheritDoc
- */
- public function detach($type, ObserverInterface $observer)
- {
- if (!isset($this->observers[$type])) {
- return;
- }
- $key = array_search($observer, $this->observers[$type], true);
- if ($key !== false) {
- unset($this->observers[$type][$key]);
- }
- }
- /**
- * @inheritDoc
- */
- public function notify($type)
- {
- $componentType = $this->normalizeType($type);
- if (!isset($this->observers[$componentType])) {
- return;
- }
- /** @var UiComponentInterface $component */
- foreach ($this->getComponents() as $component) {
- if ($component->getComponentName() != $type) {
- continue;
- }
- /** @var ObserverInterface $observer */
- foreach ($this->observers[$componentType] as $observer) {
- $observer->update($component);
- }
- }
- }
- /**
- * Normalize type to component type
- *
- * @param string $type
- * @return string
- */
- protected function normalizeType($type)
- {
- $componentType = (strpos($type, '.') !== false) ? substr($type, 0, strpos($type, '.')) : $type;
- return $componentType;
- }
- }
|