/**
  * Find the correct template and parse it
  *
  * @return string Parsed template
  */
 public function generate()
 {
     $this->strTemplate = $this->type;
     // Return output for the backend if in BE mode
     if (($output = $this->rsceGetBackendOutput()) !== null) {
         return $output;
     }
     try {
         return parent::generate();
     } catch (\Exception $exception) {
         if (TL_MODE === 'BE') {
             $template = new CustomTemplate($this->strTemplate);
             $template->setData($this->Template->getData());
             $this->Template = $template;
             return $this->Template->parse();
         }
         throw $exception;
     }
 }
 /**
  * Load the TL_CTE, FE_MOD and TL_FFL configuration and use caching if possible
  *
  * @param  bool $bypassCache
  * @return void
  */
 public static function loadConfig($bypassCache = false)
 {
     // Don't load the config in the install tool
     if (version_compare(VERSION, '4.0', '>=')) {
         try {
             if (\System::getContainer()->get('request')->get('_route') === 'contao_backend_install') {
                 return;
             }
         } catch (\Exception $exception) {
             return;
         }
     } else {
         if (\Environment::get('script') === 'contao/install.php' || \Environment::get('script') === 'install.php') {
             return;
         }
     }
     $filePaths = static::getCacheFilePaths();
     $cacheHash = md5(implode(',', array_merge(glob(TL_ROOT . '/templates/rsce_*') ?: array(), glob(TL_ROOT . '/templates/*/rsce_*') ?: array())));
     if (!$bypassCache && file_exists($filePaths['fullPath'])) {
         $fileCacheHash = null;
         include $filePaths['fullPath'];
         if ($fileCacheHash === $cacheHash) {
             // the cache file is valid and loaded
             return;
         }
     }
     // The getInstance calls are neccessary to keep the contao instance
     // stack intact and prevent an "Invalid connection resource" exception
     if (TL_MODE === 'BE') {
         \BackendUser::getInstance();
     } else {
         if (TL_MODE === 'FE') {
             \FrontendUser::getInstance();
         }
     }
     \Database::getInstance();
     $contents = array();
     $contents[] = '<?php' . "\n";
     $contents[] = '$fileCacheHash = ' . var_export($cacheHash, true) . ';' . "\n";
     $templates = \Controller::getTemplateGroup('rsce_');
     $allConfigs = array_merge(glob(TL_ROOT . '/templates/rsce_*_config.php') ?: array(), glob(TL_ROOT . '/templates/*/rsce_*_config.php') ?: array());
     $fallbackConfigPaths = array();
     $duplicateConfigs = array_filter(array_count_values(array_map(function ($configPath) {
         return basename($configPath, '_config.php');
     }, $allConfigs)), function ($count) {
         return $count > 1;
     });
     if (count($duplicateConfigs)) {
         \System::log('Duplicate Custom Elements found: ' . implode(', ', array_keys($duplicateConfigs)), __METHOD__, TL_ERROR);
     }
     foreach ($allConfigs as $configPath) {
         $templateName = basename($configPath, '_config.php');
         if (file_exists(substr($configPath, 0, -11) . '.html5')) {
             if (!isset($templates[$templateName])) {
                 $templates[$templateName] = $templateName;
             }
             if (!isset($fallbackConfigPaths[$templateName])) {
                 $fallbackConfigPaths[$templateName] = $configPath;
             }
         }
     }
     $themes = \Database::getInstance()->prepare('SELECT name, templates FROM tl_theme')->execute()->fetchAllAssoc();
     $themeNamesByTemplateDir = array();
     foreach ($themes as $theme) {
         if ($theme['templates']) {
             $themeNamesByTemplateDir[$theme['templates']] = $theme['name'];
         }
     }
     $elements = array();
     foreach ($templates as $template => $label) {
         if (substr($template, -7) === '_config') {
             continue;
         }
         $configPath = null;
         try {
             $templatePaths = CustomTemplate::getTemplates($template);
             if (!empty($templatePaths[0])) {
                 $configPath = substr($templatePaths[0], 0, -6) . '_config.php';
             }
         } catch (\Exception $e) {
             $configPath = null;
         }
         if ($configPath === null || !file_exists($configPath)) {
             if (isset($fallbackConfigPaths[$template])) {
                 $configPath = $fallbackConfigPaths[$template];
             } else {
                 continue;
             }
         }
         $config = (include $configPath);
         $element = array('config' => $config, 'label' => isset($config['label']) ? $config['label'] : array(implode(' ', array_map('ucfirst', explode('_', substr($template, 5)))), ''), 'labelPrefix' => '', 'types' => isset($config['types']) ? $config['types'] : array('content', 'module', 'form'), 'contentCategory' => isset($config['contentCategory']) ? $config['contentCategory'] : 'custom_elements', 'moduleCategory' => isset($config['moduleCategory']) ? $config['moduleCategory'] : 'custom_elements', 'template' => $template, 'path' => substr(dirname($configPath), strlen(TL_ROOT . '/')));
         if ($element['path'] && substr($element['path'], 10)) {
             if (isset($themeNamesByTemplateDir[$element['path']])) {
                 $element['labelPrefix'] = $themeNamesByTemplateDir[$element['path']] . ': ';
             } else {
                 $element['labelPrefix'] = implode(' ', array_map('ucfirst', preg_split('(\\W)', substr($element['path'], 10)))) . ': ';
             }
         }
         $elements[] = $element;
     }
     usort($elements, function ($a, $b) {
         if ($a['path'] !== $b['path']) {
             if ($a['path'] === 'templates') {
                 return -1;
             }
             if ($b['path'] === 'templates') {
                 return 1;
             }
             return strcmp($a['labelPrefix'], $b['labelPrefix']);
         }
         return strcmp($a['template'], $b['template']);
     });
     $addLabelPrefix = count(array_unique(array_map(function ($element) {
         return $element['path'];
     }, $elements))) > 1;
     foreach ($elements as $element) {
         if (in_array('content', $element['types'])) {
             $GLOBALS['TL_CTE'][$element['contentCategory']][$element['template']] = 'MadeYourDay\\Contao\\Element\\CustomElement';
             $contents[] = '$GLOBALS[\'TL_CTE\'][\'' . $element['contentCategory'] . '\'][\'' . $element['template'] . '\'] = \'MadeYourDay\\\\Contao\\\\Element\\\\CustomElement\';';
             $GLOBALS['TL_LANG']['CTE'][$element['template']] = static::getLabelTranslated($element['label']);
             $contents[] = '$GLOBALS[\'TL_LANG\'][\'CTE\'][\'' . $element['template'] . '\'] = \\MadeYourDay\\Contao\\CustomElements::getLabelTranslated(' . var_export($element['label'], true) . ');';
             if ($addLabelPrefix && $element['labelPrefix']) {
                 $GLOBALS['TL_LANG']['CTE'][$element['template']][0] = $element['labelPrefix'] . $GLOBALS['TL_LANG']['CTE'][$element['template']][0];
                 $contents[] = '$GLOBALS[\'TL_LANG\'][\'CTE\'][\'' . $element['template'] . '\'][0] = ' . var_export($element['labelPrefix'], true) . ' . $GLOBALS[\'TL_LANG\'][\'CTE\'][\'' . $element['template'] . '\'][0];';
             }
         }
         if (in_array('module', $element['types'])) {
             $GLOBALS['FE_MOD'][$element['moduleCategory']][$element['template']] = 'MadeYourDay\\Contao\\Element\\CustomElement';
             $contents[] = '$GLOBALS[\'FE_MOD\'][\'' . $element['moduleCategory'] . '\'][\'' . $element['template'] . '\'] = \'MadeYourDay\\\\Contao\\\\Element\\\\CustomElement\';';
             $GLOBALS['TL_LANG']['FMD'][$element['template']] = static::getLabelTranslated($element['label']);
             $contents[] = '$GLOBALS[\'TL_LANG\'][\'FMD\'][\'' . $element['template'] . '\'] = \\MadeYourDay\\Contao\\CustomElements::getLabelTranslated(' . var_export($element['label'], true) . ');';
             if ($addLabelPrefix && $element['labelPrefix']) {
                 $GLOBALS['TL_LANG']['FMD'][$element['template']][0] = $element['labelPrefix'] . $GLOBALS['TL_LANG']['FMD'][$element['template']][0];
                 $contents[] = '$GLOBALS[\'TL_LANG\'][\'FMD\'][\'' . $element['template'] . '\'][0] = ' . var_export($element['labelPrefix'], true) . ' . $GLOBALS[\'TL_LANG\'][\'FMD\'][\'' . $element['template'] . '\'][0];';
             }
         }
         if (in_array('form', $element['types'])) {
             $GLOBALS['TL_FFL'][$element['template']] = 'MadeYourDay\\Contao\\Form\\CustomWidget';
             $contents[] = '$GLOBALS[\'TL_FFL\'][\'' . $element['template'] . '\'] = \'MadeYourDay\\\\Contao\\\\Form\\\\CustomWidget\';';
             $GLOBALS['TL_LANG']['FFL'][$element['template']] = static::getLabelTranslated($element['label']);
             $contents[] = '$GLOBALS[\'TL_LANG\'][\'FFL\'][\'' . $element['template'] . '\'] = \\MadeYourDay\\Contao\\CustomElements::getLabelTranslated(' . var_export($element['label'], true) . ');';
             if ($addLabelPrefix && $element['labelPrefix']) {
                 $GLOBALS['TL_LANG']['FFL'][$element['template']][0] = $element['labelPrefix'] . $GLOBALS['TL_LANG']['FFL'][$element['template']][0];
                 $contents[] = '$GLOBALS[\'TL_LANG\'][\'FFL\'][\'' . $element['template'] . '\'][0] = ' . var_export($element['labelPrefix'], true) . ' . $GLOBALS[\'TL_LANG\'][\'FFL\'][\'' . $element['template'] . '\'][0];';
             }
         }
         if (!empty($element['config']['wrapper']['type'])) {
             $GLOBALS['TL_WRAPPERS'][$element['config']['wrapper']['type']][] = $element['template'];
             $contents[] = '$GLOBALS[\'TL_WRAPPERS\'][' . var_export($element['config']['wrapper']['type'], true) . '][] = ' . var_export($element['template'], true) . ';';
         }
     }
     $file = new \File($filePaths['path'], true);
     $file->write(implode("\n", $contents));
     $file->close();
     static::refreshOpcodeCache($filePaths['fullPath']);
 }