vendor/contao/core-bundle/src/Resources/contao/classes/BackendTemplate.php line 158

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of Contao.
  4.  *
  5.  * (c) Leo Feyer
  6.  *
  7.  * @license LGPL-3.0-or-later
  8.  */
  9. namespace Contao;
  10. use Symfony\Component\HttpFoundation\Response;
  11. /**
  12.  * Provide methods to handle back end templates.
  13.  *
  14.  * @property string $ua
  15.  * @property array  $javascripts
  16.  * @property array  $stylesheets
  17.  * @property string $mootools
  18.  * @property string $attributes
  19.  * @property string $badgeTitle
  20.  *
  21.  * @author Leo Feyer <https://github.com/leofeyer>
  22.  */
  23. class BackendTemplate extends Template
  24. {
  25.     use BackendTemplateTrait;
  26.     /**
  27.      * Add a hook to modify the template output
  28.      *
  29.      * @return string
  30.      */
  31.     public function parse()
  32.     {
  33.         $strBuffer parent::parse();
  34.         // HOOK: add custom parse filters
  35.         if (isset($GLOBALS['TL_HOOKS']['parseBackendTemplate']) && \is_array($GLOBALS['TL_HOOKS']['parseBackendTemplate']))
  36.         {
  37.             foreach ($GLOBALS['TL_HOOKS']['parseBackendTemplate'] as $callback)
  38.             {
  39.                 $this->import($callback[0]);
  40.                 $strBuffer $this->{$callback[0]}->{$callback[1]}($strBuffer$this->strTemplate);
  41.             }
  42.         }
  43.         return $strBuffer;
  44.     }
  45.     /**
  46.      * Return a response object
  47.      *
  48.      * @return Response The response object
  49.      */
  50.     public function getResponse()
  51.     {
  52.         $response parent::getResponse();
  53.         $response->headers->set('Cache-Control''no-cache, no-store');
  54.         return $response->setPrivate();
  55.     }
  56.     /**
  57.      * Compile the template
  58.      *
  59.      * @internal Do not call this method in your code. It will be made private in Contao 5.0.
  60.      */
  61.     protected function compile()
  62.     {
  63.         // User agent class (see #3074 and #6277)
  64.         $this->ua Environment::get('agent')->class;
  65.         if (Config::get('fullscreen'))
  66.         {
  67.             $this->ua .= ' fullscreen';
  68.         }
  69.         $this->addBackendConfig();
  70.         // Style sheets
  71.         if (!empty($GLOBALS['TL_CSS']) && \is_array($GLOBALS['TL_CSS']))
  72.         {
  73.             $strStyleSheets '';
  74.             $objCombiner = new Combiner();
  75.             foreach (array_unique($GLOBALS['TL_CSS']) as $stylesheet)
  76.             {
  77.                 $options StringUtil::resolveFlaggedUrl($stylesheet);
  78.                 if ($options->static)
  79.                 {
  80.                     $objCombiner->add($stylesheet$options->mtime$options->media);
  81.                 }
  82.                 else
  83.                 {
  84.                     $strStyleSheets .= Template::generateStyleTag($this->addStaticUrlTo($stylesheet), $options->media$options->mtime);
  85.                 }
  86.             }
  87.             if ($objCombiner->hasEntries())
  88.             {
  89.                 $strStyleSheets Template::generateStyleTag($objCombiner->getCombinedFile(), 'all') . $strStyleSheets;
  90.             }
  91.             $this->stylesheets .= $strStyleSheets;
  92.         }
  93.         // JavaScripts
  94.         if (!empty($GLOBALS['TL_JAVASCRIPT']) && \is_array($GLOBALS['TL_JAVASCRIPT']))
  95.         {
  96.             $objCombiner = new Combiner();
  97.             $objCombinerAsync = new Combiner();
  98.             $strJavaScripts '';
  99.             foreach (array_unique($GLOBALS['TL_JAVASCRIPT']) as $javascript)
  100.             {
  101.                 $options StringUtil::resolveFlaggedUrl($javascript);
  102.                 if ($options->static)
  103.                 {
  104.                     $options->async $objCombinerAsync->add($javascript$options->mtime) : $objCombiner->add($javascript$options->mtime);
  105.                 }
  106.                 else
  107.                 {
  108.                     $strJavaScripts .= Template::generateScriptTag($this->addStaticUrlTo($javascript), $options->async$options->mtime);
  109.                 }
  110.             }
  111.             if ($objCombiner->hasEntries())
  112.             {
  113.                 $strJavaScripts Template::generateScriptTag($objCombiner->getCombinedFile()) . $strJavaScripts;
  114.             }
  115.             if ($objCombinerAsync->hasEntries())
  116.             {
  117.                 $strJavaScripts Template::generateScriptTag($objCombinerAsync->getCombinedFile(), true) . $strJavaScripts;
  118.             }
  119.             $this->javascripts .= $strJavaScripts;
  120.         }
  121.         // MooTools scripts (added at the page bottom)
  122.         if (!empty($GLOBALS['TL_MOOTOOLS']) && \is_array($GLOBALS['TL_MOOTOOLS']))
  123.         {
  124.             $strMootools '';
  125.             foreach (array_unique($GLOBALS['TL_MOOTOOLS']) as $script)
  126.             {
  127.                 $strMootools .= $script;
  128.             }
  129.             $this->mootools .= $strMootools;
  130.         }
  131.         $strBuffer $this->parse();
  132.         $strBuffer = static::replaceOldBePaths($strBuffer);
  133.         // HOOK: add custom output filter
  134.         if (isset($GLOBALS['TL_HOOKS']['outputBackendTemplate']) && \is_array($GLOBALS['TL_HOOKS']['outputBackendTemplate']))
  135.         {
  136.             foreach ($GLOBALS['TL_HOOKS']['outputBackendTemplate'] as $callback)
  137.             {
  138.                 $this->import($callback[0]);
  139.                 $strBuffer $this->{$callback[0]}->{$callback[1]}($strBuffer$this->strTemplate);
  140.             }
  141.         }
  142.         $this->strBuffer $strBuffer;
  143.         parent::compile();
  144.     }
  145.     /**
  146.      * Add the contao.backend configuration
  147.      */
  148.     private function addBackendConfig(): void
  149.     {
  150.         $container System::getContainer();
  151.         if (!$container->hasParameter('contao.backend'))
  152.         {
  153.             return;
  154.         }
  155.         $backendConfig $container->getParameter('contao.backend');
  156.         if (!empty($backendConfig['attributes']) && \is_array($backendConfig['attributes']))
  157.         {
  158.             $this->attributes ' ' implode(' 'array_map(
  159.                 static function ($v$k) { return sprintf('data-%s="%s"'$k$v); },
  160.                 $backendConfig['attributes'],
  161.                 array_keys($backendConfig['attributes'])
  162.             ));
  163.         }
  164.         if (!empty($backendConfig['custom_css']) && \is_array($backendConfig['custom_css']))
  165.         {
  166.             if (!\is_array($GLOBALS['TL_CSS']))
  167.             {
  168.                 $GLOBALS['TL_CSS'] = array();
  169.             }
  170.             $GLOBALS['TL_CSS'] = array_merge($GLOBALS['TL_CSS'], $backendConfig['custom_css']);
  171.         }
  172.         if (!empty($backendConfig['custom_js']) && \is_array($backendConfig['custom_js']))
  173.         {
  174.             if (!\is_array($GLOBALS['TL_JAVASCRIPT']))
  175.             {
  176.                 $GLOBALS['TL_JAVASCRIPT'] = array();
  177.             }
  178.             $GLOBALS['TL_JAVASCRIPT'] = array_merge($GLOBALS['TL_JAVASCRIPT'], $backendConfig['custom_js']);
  179.         }
  180.         $this->badgeTitle $backendConfig['badge_title'];
  181.     }
  182. }
  183. class_alias(BackendTemplate::class, 'BackendTemplate');