vendor/contao-bootstrap/templates/src/EventListener/ThemeConfigurationListener.php line 36

Open in your IDE?
  1. <?php
  2. /**
  3.  * Contao Bootstrap templates.
  4.  *
  5.  * @package    contao-bootstrap
  6.  * @subpackage Templates
  7.  * @author     David Molineus <david.molineus@netzmacht.de>
  8.  * @copyright  2014-2018 netzmacht David Molineus. All rights reserved.
  9.  * @license    https://github.com/contao-bootstrap/templates/blob/master/LICENSE LGPL 3.0-or-later
  10.  * @filesource
  11.  */
  12. declare(strict_types=1);
  13. namespace ContaoBootstrap\Templates\EventListener;
  14. use Contao\Environment;
  15. use Contao\FilesModel;
  16. use Contao\ThemeModel;
  17. use ContaoBootstrap\Core\Environment\ThemeContext;
  18. use ContaoBootstrap\Core\Message\Command\BuildContextConfig;
  19. /**
  20.  * Class GravatarConfigurationListener parses the gravatar configuration defined in the theme.
  21.  */
  22. final class ThemeConfigurationListener
  23. {
  24.     /**
  25.      * Build the context config.
  26.      *
  27.      * @param BuildContextConfig $command The command.
  28.      *
  29.      * @return void
  30.      */
  31.     public function onBuildContextConfig(BuildContextConfig $command): void
  32.     {
  33.         $context $command->getContext();
  34.         if (!$context instanceof ThemeContext) {
  35.             return;
  36.         }
  37.         $theme ThemeModel::findByPk($context->getThemeId());
  38.         if (!$theme) {
  39.             return;
  40.         }
  41.         $templateConfig = [];
  42.         $templateConfig $this->buildGravatarConfig($theme$templateConfig);
  43.         $templateConfig $this->buildTemplateMappingConfig($theme$templateConfig);
  44.         if (count($templateConfig) > 0) {
  45.             $config $command->getConfig()->merge(
  46.                 [
  47.                     'templates' => $templateConfig
  48.                 ]
  49.             );
  50.             $command->setConfig($config);
  51.         }
  52.     }
  53.     /**
  54.      * Build the gravatar config.
  55.      *
  56.      * @param ThemeModel $theme  The theme model.
  57.      * @param array      $config The theme config.
  58.      *
  59.      * @return array
  60.      */
  61.     private function buildGravatarConfig(ThemeModel $theme, array $config): array
  62.     {
  63.         $file FilesModel::findByPk($theme->bs_gravatar_default);
  64.         if ($file) {
  65.             $config['gravatar']['default'] = Environment::get('base') . $file->path;
  66.         }
  67.         if ($theme->bs_gravatar_size) {
  68.             $config['gravatar']['size'] = (int) $theme->bs_gravatar_size;
  69.         }
  70.         return $config;
  71.     }
  72.     /**
  73.      * Build the gravatar config.
  74.      *
  75.      * @param ThemeModel $theme  The theme model.
  76.      * @param array      $config The theme config.
  77.      *
  78.      * @return array
  79.      */
  80.     private function buildTemplateMappingConfig(ThemeModel $theme, array $config): array
  81.     {
  82.         if ($theme->bs_templates_disable_mapping) {
  83.             $config['auto_mapping'] = false;
  84.         }
  85.         return $config;
  86.     }
  87. }