Open tristanlins opened 10 years ago
Brainstorming result:
class SystemSubscriber implements EventSubscriberInterface { /** * Handle a load language file event. * * @param LoadLanguageFileEvent $event The event. * * @return void */ public function handleLoadLanguageFile(LoadLanguageFileEvent $event) { $hash = $event->getSemaphoreHash(); if (LoadLanguageFileEvent::$cameFromHook[$hash] === false) { throw new \InvalidConditionException(); } if (LoadLanguageFileEvent::$cameFromHook[$hash] === null) { LoadLanguageFileEvent::$cameFromHook[$hash] = false; \System::loadLanguageFile( $event->getFileName(), $event->getLanguage(), $event->isCacheIgnored() ); unset(LoadLanguageFileEvent::$cameFromHook[$hash]); } } } class TristanSubscriber { public function handleLoadLanguageFile(LoadLanguageFileEvent $event) { // fire twice. } } $eventDispatcher->addSubscriber(new SystemSubscriber(), PHP_INT_MAX); class SystemHookHandler { public function loadLanguageFile($fileName, $language = null, $ignoreCache = false) { $hash = LoadLanguageFileEvent::generateSemaphoreHash($fileName, $language, $ignoreCache); if (LoadLanguageFileEvent::$cameFromHook[$hash] === true) { throw new \InvalidConditionException(); } if (LoadLanguageFileEvent::$cameFromHook[$hash]=== null) { LoadLanguageFileEvent::$cameFromHook[$hash] = true; $event->setCameFromHook(true); $eventDispatcher->dispatch($event); unset(LoadLanguageFileEvent::$cameFromHook[$hash]); } } } $GLOBALS['TL_HOOKS']['loadLanguageFile'][] = array('SystemHookHandler', 'loadLanguageFile'); class LoadLanguageFileEvent { protected $fileName; protected $cameFromHook; public function getSemaphoreHash() { return self::generateSemaphoreHash($this->get....(), ...); } public static generateSemaphoreHash($filename, ...) { return md5($filename . ...); } } Via plain call: 1. $this->loadLanguageFile(); 2. SystemHookHandler::loadLanguageFile(); => cameFromHook == true 3. dispatcher->dispatch() 4. SystemSubscriber::handleLoadLanguageFile(); == NO-OP 5. done Via event: 1. dispatcher->dispatch() => cameFromHook == false 2. SystemSubscriber::LoadLanguageFileEvent(); ==> call \System::loadLanguageFile(); 2.1. SystemHookHandler::loadLanguageFile(); => cameFromHook == true 2.2. dispatcher->dispatch() 2.3. SystemSubscriber::handleLoadLanguageFile(); == NO-OP 3. SystemSubscriber::LoadLanguageFileEvent(); -> stop event
Brainstorming result: