]> source.dussan.org Git - nextcloud-server.git/commitdiff
move core script loading from static template to event listener
authorRobin Appelman <robin@icewind.nl>
Mon, 14 Aug 2023 15:45:53 +0000 (17:45 +0200)
committerRobin Appelman <robin@icewind.nl>
Thu, 17 Aug 2023 08:57:56 +0000 (10:57 +0200)
Signed-off-by: Robin Appelman <robin@icewind.nl>
core/Listener/BeforeTemplateRenderedListener.php
lib/private/legacy/OC_Template.php

index 4b658cb550a1712284c0202a7e4d2d51f36f302c..b3ab7c429e8bcba5b42bf25992d22ce75d3fa563 100644 (file)
@@ -25,12 +25,18 @@ declare(strict_types=1);
  */
 namespace OC\Core\Listener;
 
+use OCP\AppFramework\Http\Events\BeforeLoginTemplateRenderedEvent;
 use OCP\AppFramework\Http\Events\BeforeTemplateRenderedEvent;
 use OCP\AppFramework\Http\TemplateResponse;
 use OCP\EventDispatcher\Event;
 use OCP\EventDispatcher\IEventListener;
+use OCP\IConfig;
+use OCP\Util;
 
 class BeforeTemplateRenderedListener implements IEventListener {
+       public function __construct(private IConfig $config) {
+       }
+
        public function handle(Event $event): void {
                if (!($event instanceof BeforeTemplateRenderedEvent)) {
                        return;
@@ -38,7 +44,29 @@ class BeforeTemplateRenderedListener implements IEventListener {
 
                if ($event->getResponse()->getRenderAs() === TemplateResponse::RENDER_AS_USER) {
                        // Making sure to inject just after core
-                       \OCP\Util::addScript('core', 'unsupported-browser-redirect');
+                       Util::addScript('core', 'unsupported-browser-redirect');
+               }
+
+               \OC_Util::addStyle('server', null, true);
+
+               if ($event instanceof BeforeTemplateRenderedEvent) {
+
+                       // include common nextcloud webpack bundle
+                       Util::addScript('core', 'common');
+                       Util::addScript('core', 'main');
+                       Util::addTranslations('core');
+
+                       if ($event->getResponse()->getRenderAs() !== TemplateResponse::RENDER_AS_ERROR) {
+                               Util::addScript('core', 'files_fileinfo');
+                               Util::addScript('core', 'files_client');
+                               Util::addScript('core', 'merged-template-prepend');
+
+
+                               // If installed and background job is set to ajax, add dedicated script
+                               if ($this->config->getAppValue('core', 'backgroundjobs_mode', 'ajax') == 'ajax') {
+                                       Util::addScript('core', 'backgroundjobs');
+                               }
+                       }
                }
        }
 }
index 70477ea6a06277303e85de892f0d168089faeac3..3d27475590f2e3b4d67c1736e580de3f92dcb85c 100644 (file)
@@ -59,8 +59,6 @@ class OC_Template extends \OC\Template\Base {
        /** @var string */
        protected $app; // app id
 
-       protected static $initTemplateEngineFirstRun = true;
-
        /**
         * Constructor
         *
@@ -73,9 +71,6 @@ class OC_Template extends \OC\Template\Base {
         * @param bool $registerCall = true
         */
        public function __construct($app, $name, $renderAs = TemplateResponse::RENDER_AS_BLANK, $registerCall = true) {
-               // Read the selected theme from the config file
-               self::initTemplateEngine($renderAs);
-
                $theme = OC_Util::getTheme();
 
                $requestToken = (OC::$server->getSession() && $registerCall) ? \OCP\Util::callRegister() : '';
@@ -95,40 +90,6 @@ class OC_Template extends \OC\Template\Base {
                parent::__construct($template, $requestToken, $l10n, $themeDefaults);
        }
 
-       /**
-        * @param string $renderAs
-        */
-       public static function initTemplateEngine($renderAs) {
-               if (self::$initTemplateEngineFirstRun) {
-                       // apps that started before the template initialization can load their own scripts/styles
-                       // so to make sure this scripts/styles here are loaded first we put all core scripts first
-                       // check lib/public/Util.php
-                       OC_Util::addStyle('server', null, true);
-
-                       // include common nextcloud webpack bundle
-                       Util::addScript('core', 'common');
-                       Util::addScript('core', 'main');
-                       Util::addTranslations('core');
-
-                       if (\OC::$server->getSystemConfig()->getValue('installed', false) && !\OCP\Util::needUpgrade()) {
-                               Util::addScript('core', 'files_fileinfo');
-                               Util::addScript('core', 'files_client');
-                               Util::addScript('core', 'merged-template-prepend');
-                       }
-
-                       // If installed and background job is set to ajax, add dedicated script
-                       if (\OC::$server->getSystemConfig()->getValue('installed', false)
-                               && $renderAs !== TemplateResponse::RENDER_AS_ERROR
-                               && !\OCP\Util::needUpgrade()) {
-                               if (\OC::$server->getConfig()->getAppValue('core', 'backgroundjobs_mode', 'ajax') == 'ajax') {
-                                       Util::addScript('core', 'backgroundjobs');
-                               }
-                       }
-
-                       self::$initTemplateEngineFirstRun = false;
-               }
-       }
-
 
        /**
         * find the template with the given name