/**
  * @param $responses
  * @param $eventsToListen
  * @param bool $processOriginal
  */
 protected function parseAndRunEventListeners(&$responses, $eventsToListen, $processOriginal = TRUE)
 {
     if ($processOriginal) {
         foreach ($responses['original'] as $response) {
             $this->hijaxEventDispatcher->parseAndRunEventListeners($response['response']);
         }
     }
     if ($eventsToListen && is_array($eventsToListen)) {
         foreach ($eventsToListen as $listenerId => $eventNames) {
             $shouldProcess = FALSE;
             foreach ($eventNames as $eventName) {
                 if ($this->hijaxEventDispatcher->hasPendingEventWithName($eventName, $listenerId)) {
                     $shouldProcess = TRUE;
                     break;
                 }
             }
             if ($shouldProcess) {
                 /* @var $listener \EssentialDots\ExtbaseHijax\Event\Listener */
                 $listener = $this->listenerFactory->findById($listenerId);
                 if ($listener) {
                     $configuration = $listener->getConfiguration();
                     $bootstrap = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Extbase\\Core\\Bootstrap');
                     $bootstrap->cObj = $listener->getCObj();
                     $bootstrap->initialize($configuration);
                     /* @var $request \TYPO3\CMS\Extbase\Mvc\Web\Request */
                     $request = $listener->getRequest();
                     $request->setDispatched(false);
                     $this->setPreventMarkupUpdateOnAjaxLoad(false);
                     /* @var $response \TYPO3\CMS\Extbase\Mvc\Web\Response */
                     $response = $this->objectManager->get('TYPO3\\CMS\\Extbase\\Mvc\\Web\\Response');
                     /* @var $dispatcher \EssentialDots\ExtbaseHijax\MVC\Dispatcher */
                     $dispatcher = $this->objectManager->get('EssentialDots\\ExtbaseHijax\\MVC\\Dispatcher');
                     try {
                         $dispatcher->dispatch($request, $response, $listener);
                         $this->parseHeaders($response);
                         $content = $response->getContent();
                         $this->serviceContent->processIntScripts($content);
                         $this->serviceContent->processAbsRefPrefix($content, $configuration['settings']['absRefPrefix']);
                         $responses['affected'][] = array('id' => $listenerId, 'format' => $request->getFormat(), 'response' => $content, 'preventMarkupUpdate' => $this->getPreventMarkupUpdateOnAjaxLoad());
                     } catch (\EssentialDots\ExtbaseHijax\MVC\Exception\StopProcessingAction $exception) {
                         $this->parseHeaders($response);
                     }
                 } else {
                     // TODO: log error message
                 }
             }
         }
     }
 }
 /**
  * @param array $match
  * @return string
  */
 protected function parseAndRunEventListenersCallback($match)
 {
     $matchesListenerDef = array();
     preg_match('/(?P<listenerId>[a-zA-Z0-9_-]*)\\((?P<eventNames>.*)\\);/msU', $match['listenerDefinition'], $matchesListenerDef);
     $elementId = $match['elementId'];
     $listenerId = $matchesListenerDef['listenerId'];
     $eventNames = $this->convertCSVToArray($matchesListenerDef['eventNames']);
     $shouldProcess = FALSE;
     foreach ($eventNames as $eventName) {
         if ($this->hasPendingEventWithName($eventName, $listenerId)) {
             $shouldProcess = TRUE;
             break;
         }
     }
     if ($shouldProcess) {
         /* @var $listener \EssentialDots\ExtbaseHijax\Event\Listener */
         $listener = $this->listenerFactory->findById($listenerId);
         if ($listener) {
             /* @var $bootstrap \TYPO3\CMS\Extbase\Core\Bootstrap */
             $bootstrap = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Extbase\\Core\\Bootstrap');
             $bootstrap->cObj = $listener->getCObj();
             $bootstrap->initialize($listener->getConfiguration());
             $request = $listener->getRequest();
             $request->setDispatched(false);
             /* @var $response \TYPO3\CMS\Extbase\Mvc\Web\Response */
             $response = $this->objectManager->get('TYPO3\\CMS\\Extbase\\Mvc\\Web\\Response');
             $dispatcher = $this->objectManager->get('TYPO3\\CMS\\Extbase\\Mvc\\Dispatcher');
             $dispatcher->dispatch($request, $response);
             $result = $response->getContent();
         } else {
             // TODO: log error message
             $result = $match[0];
         }
     } else {
         $result = $match[0];
     }
     return $result;
 }
 /**
  * Dispatches a request to a controller and initializes the security framework.
  *
  * @param \TYPO3\CMS\Extbase\Mvc\RequestInterface $request The request to dispatch
  * @param \TYPO3\CMS\Extbase\Mvc\ResponseInterface $response The response, to be modified by the controller
  * @param \EssentialDots\ExtbaseHijax\Event\Listener $listener Listener
  * @return void
  */
 public function dispatch(\TYPO3\CMS\Extbase\Mvc\RequestInterface $request, \TYPO3\CMS\Extbase\Mvc\ResponseInterface $response, \EssentialDots\ExtbaseHijax\Event\Listener $listener = NULL)
 {
     /* @var $request \TYPO3\CMS\Extbase\Mvc\Request */
     $this->currentRequest = $request;
     array_push($this->requestsStack, $this->currentRequest);
     if (defined('TYPO3_cliMode') && TYPO3_cliMode === TRUE) {
         parent::dispatch($request, $response);
     } else {
         array_push($this->listenersStack, $this->currentListener);
         if ($listener) {
             $this->currentListener = $listener;
         } else {
             $this->currentListener = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('EssentialDots\\ExtbaseHijax\\Event\\Listener', $request);
         }
         if (!$this->serviceContent->getExecuteExtbasePlugins()) {
             $this->listenerFactory->persist($this->currentListener);
             $this->serviceContent->setCurrentListener($this->currentListener);
         } else {
             $this->hijaxEventDispatcher->startContentElement();
             try {
                 parent::dispatch($request, $response);
             } catch (\TYPO3\CMS\Extbase\Mvc\Controller\Exception\RequiredArgumentMissingException $requiredArgumentMissingException) {
                 try {
                     // this happens with simple reload on pages where some argument is required
                     $configuration = $this->configurationManager->getConfiguration(\TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface::CONFIGURATION_TYPE_FRAMEWORK);
                     $defaultControllerName = current(array_keys($configuration['controllerConfiguration']));
                     $allowedControllerActions = array();
                     foreach ($configuration['controllerConfiguration'] as $controllerName => $controllerActions) {
                         $allowedControllerActions[$controllerName] = $controllerActions['actions'];
                     }
                     $defaultActionName = is_array($allowedControllerActions[$request->getControllerName()]) ? current($allowedControllerActions[$request->getControllerName()]) : '';
                     // try to run the current controller with the default action
                     $request->setDispatched(false);
                     $request->setControllerActionName($defaultActionName);
                     parent::dispatch($request, $response);
                 } catch (\TYPO3\CMS\Extbase\Mvc\Controller\Exception\RequiredArgumentMissingException $requiredArgumentMissingException) {
                     if ($defaultControllerName != $request->getControllerName()) {
                         $request->setControllerName($defaultControllerName);
                         $defaultActionName = is_array($allowedControllerActions[$defaultControllerName]) ? current($allowedControllerActions[$defaultControllerName]) : '';
                         // try to run the default plugin controller with the default action
                         $request->setDispatched(false);
                         $request->setControllerActionName($defaultActionName);
                         parent::dispatch($request, $response);
                     }
                 }
             }
             if ($this->hijaxEventDispatcher->getIsHijaxElement()) {
                 $this->listenerFactory->persist($this->currentListener);
             }
             if (($this->ajaxDispatcher->getIsActive() || $this->hijaxEventDispatcher->getIsHijaxElement()) && !$this->ajaxDispatcher->getPreventMarkupUpdateOnAjaxLoad()) {
                 $currentListeners = $this->hijaxEventDispatcher->getListeners('', TRUE);
                 $signature = $this->getCurrentListener()->getId() . '(' . $this->convertArrayToCSV(array_keys($currentListeners)) . '); ';
                 $content = $response->getContent();
                 $content = '<!-- ###EVENT_LISTENER_' . self::$id . '### START ' . $signature . ' -->' . $content . '<!-- ###EVENT_LISTENER_' . self::$id . '### END -->';
                 if (\TYPO3\CMS\Core\Utility\GeneralUtility::_GP('eID') && \TYPO3\CMS\Core\Utility\GeneralUtility::_GP('eID') != 'extbase_hijax_dispatcher') {
                     $this->hijaxEventDispatcher->replaceXMLCommentsWithDivs($content, 'html');
                 }
                 $response->setContent($content);
                 $this->extensionConfiguration->setNextElementId(++self::$id);
             }
             $this->hijaxEventDispatcher->endContentElement();
         }
         $this->currentListener = array_pop($this->listenersStack);
     }
     $this->currentRequest = array_pop($this->requestsStack);
 }