|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types = 1); |
| 4 | + |
| 5 | +namespace Infinityloop\ObserverComponent; |
| 6 | + |
| 7 | +final class EventMapper |
| 8 | +{ |
| 9 | + use Nette\SmartObject; |
| 10 | + |
| 11 | + private \Nette\Application\Application $application; |
| 12 | + private \Nette\Caching\IStorage $storage; |
| 13 | + private ?\Nette\Caching\Cache $eventMap = null; |
| 14 | + private bool $debugMode; |
| 15 | + |
| 16 | + public function __construct( |
| 17 | + \Nette\Application\Application $application, |
| 18 | + \Nette\Caching\IStorage $storage, |
| 19 | + bool $debugMode = true |
| 20 | + ) |
| 21 | + { |
| 22 | + $this->application = $application; |
| 23 | + $this->storage = $storage; |
| 24 | + $this->debugMode = $debugMode; |
| 25 | + } |
| 26 | + |
| 27 | + public function registerObserver(IObserverComponent $component) : void |
| 28 | + { |
| 29 | + $componentPath = $component->lookupPath(\Nette\Application\IPresenter::class); |
| 30 | + \assert(\is_string($componentPath)); |
| 31 | + |
| 32 | + if (!$this->debugMode && $this->isComponentRegistered($componentPath)) { |
| 33 | + return; |
| 34 | + } |
| 35 | + |
| 36 | + foreach ($component::getObservedEvents() as $eventName) { |
| 37 | + \assert(\is_string($eventName) && \class_exists($eventName)); |
| 38 | + |
| 39 | + $observerList = $this->getObserverList($eventName); |
| 40 | + |
| 41 | + if (\in_array($componentPath, $observerList, true)) { |
| 42 | + continue; |
| 43 | + } |
| 44 | + |
| 45 | + $observerList[] = $componentPath; |
| 46 | + $this->getEventMap()->save($eventName, $observerList); |
| 47 | + |
| 48 | + $registeredComponents = $this->getEventMap()->load('components') ?? []; |
| 49 | + $registeredComponents[] = $componentPath; |
| 50 | + $this->getEventMap()->save('components', $registeredComponents); |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + public function dispatchEvent(IEvent $event) : void |
| 55 | + { |
| 56 | + $presenter = $this->application->getPresenter(); |
| 57 | + \assert($presenter instanceof \Nette\Application\UI\Control); |
| 58 | + |
| 59 | + foreach ($this->getObserverList(\get_class($event)) as $observerPath) { |
| 60 | + \assert(\is_string($observerPath)); |
| 61 | + |
| 62 | + $observer = $presenter->getComponent($observerPath); |
| 63 | + \assert($observer instanceof IObserverComponent); |
| 64 | + |
| 65 | + $observer->observableUpdated($event); |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + private function getObserverList(string $eventName) : array |
| 70 | + { |
| 71 | + return $this->getEventMap()->load($eventName) |
| 72 | + ?? []; |
| 73 | + } |
| 74 | + |
| 75 | + private function isComponentRegistered(string $componentPath) : bool |
| 76 | + { |
| 77 | + $registeredComponents = $this->getEventMap()->load('components') ?? []; |
| 78 | + |
| 79 | + return \in_array($componentPath, $registeredComponents, true); |
| 80 | + } |
| 81 | + |
| 82 | + private function getEventMap() : \Nette\Caching\Cache |
| 83 | + { |
| 84 | + if (!$this->eventMap instanceof \Nette\Caching\Cache) { |
| 85 | + $applicationPath = \ltrim($this->application->getPresenter()->getAction(true), ':'); |
| 86 | + $this->eventMap = new \Nette\Caching\Cache($this->storage, 'eventMapper-' . $applicationPath); |
| 87 | + } |
| 88 | + |
| 89 | + return $this->eventMap; |
| 90 | + } |
| 91 | +} |
0 commit comments