/**
  * Insert module and return generated content.
  *
  * @param PageModuleWrapper $pageModuleWrapper
  * @return string
  */
 protected function insertModule(PageModuleWrapper $pageModuleWrapper)
 {
     $this->app->logger->debug(($pageModuleWrapper->getEnabled() ? 'Inserting' : 'Skipping') . ' module "' . $pageModuleWrapper->getName() . '" of type "' . $pageModuleWrapper->getClassName() . '" with target "' . $pageModuleWrapper->getTarget() . '"');
     /** @var PreModuleEvent $event */
     $event = $this->app->dispatcher->dispatch(GeneratorEvents::PRE_MODULE, new PreModuleEvent($pageModuleWrapper));
     if ($event->getContent() === null) {
         // TODO: how do we handle $event->isEnabled() ?
         /** @var \Curry\Module\AbstractModule $module */
         $module = $pageModuleWrapper->createObject();
         $template = null;
         if ($event->getTemplate() !== null) {
             $this->app->twig->loadTemplate($event->getTemplate());
         } else {
             if ($module->getDefaultTemplate()) {
                 $template = $this->app->loadTemplateString($module->getDefaultTemplate());
             }
         }
         if ($template && $template->getEnvironment()) {
             $twig = $template->getEnvironment();
             $twig->addGlobal('module', array('Id' => $pageModuleWrapper->getPageModuleId(), 'ClassName' => $pageModuleWrapper->getClassName(), 'Name' => $pageModuleWrapper->getName(), 'ModuleDataId' => $pageModuleWrapper->getModuleDataId(), 'Target' => $pageModuleWrapper->getTarget()));
         }
         $content = (string) $module->showFront($template);
     } else {
         $content = $event->getContent();
     }
     /** @var PostModuleEvent $postEvent */
     $postEvent = $this->app->dispatcher->dispatch(GeneratorEvents::POST_MODULE, new PostModuleEvent($pageModuleWrapper, $content, $event->getExtras()));
     return $postEvent->getContent();
 }