Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Trigger (some) of the events when the hook is triggered #2

Open
tristanlins opened this issue Feb 20, 2014 · 0 comments
Open

Trigger (some) of the events when the hook is triggered #2

tristanlins opened this issue Feb 20, 2014 · 0 comments

Comments

@tristanlins
Copy link
Contributor

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant