From 444e8374a0718d10324706cee8ec1911f3bf2ac6 Mon Sep 17 00:00:00 2001
From: Benni Mack <benni@typo3.org>
Date: Thu, 26 Jul 2018 20:33:06 +0200
Subject: [PATCH] [TASK] Deprecate TSFE->initTemplate

The method only does one line of code, and is now instantiated if needed.

Resolves: #85666
Releases: master
Change-Id: Id5df0ecb3f887743ea1167829c9cca69aef501e4
Reviewed-on: https://review.typo3.org/57693
Reviewed-by: Benni Mack <benni@typo3.org>
Tested-by: Benni Mack <benni@typo3.org>
Tested-by: TYPO3com <no-reply@typo3.com>
Reviewed-by: Wouter Wolters <typo3@wouterwolters.nl>
Reviewed-by: Susanne Moog <susanne.moog@typo3.org>
Tested-by: Susanne Moog <susanne.moog@typo3.org>
Reviewed-by: Andreas Fernandez <a.fernandez@scripting-base.de>
Tested-by: Andreas Fernandez <a.fernandez@scripting-base.de>
---
 ...oScriptFrontendController-initTemplate.rst | 33 +++++++++++++++++++
 .../TypoScriptFrontendController.php          | 10 ++++++
 .../PrepareTypoScriptFrontendRendering.php    |  4 ---
 .../Typolink/AbstractTypolinkBuilder.php      |  3 +-
 .../Php/MethodCallMatcher.php                 |  7 ++++
 .../Classes/Service/RedirectService.php       |  1 -
 6 files changed, 52 insertions(+), 6 deletions(-)
 create mode 100644 typo3/sysext/core/Documentation/Changelog/master/Deprecation-85666-TypoScriptFrontendController-initTemplate.rst

diff --git a/typo3/sysext/core/Documentation/Changelog/master/Deprecation-85666-TypoScriptFrontendController-initTemplate.rst b/typo3/sysext/core/Documentation/Changelog/master/Deprecation-85666-TypoScriptFrontendController-initTemplate.rst
new file mode 100644
index 000000000000..2975510f8c44
--- /dev/null
+++ b/typo3/sysext/core/Documentation/Changelog/master/Deprecation-85666-TypoScriptFrontendController-initTemplate.rst
@@ -0,0 +1,33 @@
+.. include:: ../../Includes.txt
+
+================================================================
+Deprecation: #85666 - TypoScriptFrontendController->initTemplate
+================================================================
+
+See :issue:`85666`
+
+Description
+===========
+
+The method :php:`TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController->initTemplate()` has been marked as
+deprecated.
+
+
+Impact
+======
+
+Calling the method directly will trigger a deprecation message.
+
+
+Affected Installations
+======================
+
+TYPO3 installations with custom extensions calling this public method directly.
+
+
+Migration
+=========
+
+The method call can simply get removed, the TemplateService in instantiated by TSFE on demand.
+
+.. index:: Frontend, PHP-API, FullyScanned, ext:frontend
diff --git a/typo3/sysext/frontend/Classes/Controller/TypoScriptFrontendController.php b/typo3/sysext/frontend/Classes/Controller/TypoScriptFrontendController.php
index 2a98a13614d5..3558dd80f59c 100644
--- a/typo3/sysext/frontend/Classes/Controller/TypoScriptFrontendController.php
+++ b/typo3/sysext/frontend/Classes/Controller/TypoScriptFrontendController.php
@@ -2237,9 +2237,11 @@ class TypoScriptFrontendController implements LoggerAwareInterface
 
     /**
      * Initialize the TypoScript template parser
+     * @deprecated since TYPO3 v9.4 will be removed in TYPO3 v10.0. Either instantiate $TSFE->tmpl yourself, if really necessary.
      */
     public function initTemplate()
     {
+        trigger_error('TSFE->initTemplate() will be removed in TYPO3 v10.0. Instantiating TemplateService is done implicitly on usage within TSFE directly.', E_USER_DEPRECATED);
         $this->tmpl = GeneralUtility::makeInstance(TemplateService::class, $this->context);
     }
 
@@ -2262,6 +2264,10 @@ class TypoScriptFrontendController implements LoggerAwareInterface
             return;
         }
 
+        if (!($this->tmpl instanceof TemplateService)) {
+            $this->tmpl = GeneralUtility::makeInstance(TemplateService::class, $this->context);
+        }
+
         $pageSectionCacheContent = $this->tmpl->getCurrentPageData();
         if (!is_array($pageSectionCacheContent)) {
             // Nothing in the cache, we acquire an "exclusive lock" for the key now.
@@ -2477,6 +2483,10 @@ class TypoScriptFrontendController implements LoggerAwareInterface
      */
     public function getConfigArray()
     {
+        if (!($this->tmpl instanceof TemplateService)) {
+            $this->tmpl = GeneralUtility::makeInstance(TemplateService::class, $this->context);
+        }
+
         // If config is not set by the cache (which would be a major mistake somewhere) OR if INTincScripts-include-scripts have been registered, then we must parse the template in order to get it
         if (empty($this->config) || is_array($this->config['INTincScript']) || $this->forceTemplateParsing) {
             $timeTracker = $this->getTimeTracker();
diff --git a/typo3/sysext/frontend/Classes/Middleware/PrepareTypoScriptFrontendRendering.php b/typo3/sysext/frontend/Classes/Middleware/PrepareTypoScriptFrontendRendering.php
index ac875ea632dc..dd57e6ce2530 100644
--- a/typo3/sysext/frontend/Classes/Middleware/PrepareTypoScriptFrontendRendering.php
+++ b/typo3/sysext/frontend/Classes/Middleware/PrepareTypoScriptFrontendRendering.php
@@ -56,10 +56,6 @@ class PrepareTypoScriptFrontendRendering implements MiddlewareInterface
      */
     public function process(ServerRequestInterface $request, PsrRequestHandlerInterface $handler): ResponseInterface
     {
-        // Starts the template
-        $this->timeTracker->push('Start Template');
-        $this->controller->initTemplate();
-        $this->timeTracker->pull();
         // Get from cache
         $this->timeTracker->push('Get Page from cache');
         // Locks may be acquired here
diff --git a/typo3/sysext/frontend/Classes/Typolink/AbstractTypolinkBuilder.php b/typo3/sysext/frontend/Classes/Typolink/AbstractTypolinkBuilder.php
index 6129469e1699..635290d551b6 100644
--- a/typo3/sysext/frontend/Classes/Typolink/AbstractTypolinkBuilder.php
+++ b/typo3/sysext/frontend/Classes/Typolink/AbstractTypolinkBuilder.php
@@ -16,6 +16,7 @@ namespace TYPO3\CMS\Frontend\Typolink;
  */
 
 use TYPO3\CMS\Core\Service\DependencyOrderingService;
+use TYPO3\CMS\Core\TypoScript\TemplateService;
 use TYPO3\CMS\Core\Utility\GeneralUtility;
 use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;
 use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;
@@ -200,7 +201,7 @@ abstract class AbstractTypolinkBuilder
                     (int)GeneralUtility::_GP('type')
             );
             $GLOBALS['TSFE']->sys_page = GeneralUtility::makeInstance(PageRepository::class);
-            $GLOBALS['TSFE']->initTemplate();
+            $GLOBALS['TSFE']->tmpl = GeneralUtility::makeInstance(TemplateService::class);
         }
         return $GLOBALS['TSFE'];
     }
diff --git a/typo3/sysext/install/Configuration/ExtensionScanner/Php/MethodCallMatcher.php b/typo3/sysext/install/Configuration/ExtensionScanner/Php/MethodCallMatcher.php
index 7f014b77ccba..4b88e9588a4e 100644
--- a/typo3/sysext/install/Configuration/ExtensionScanner/Php/MethodCallMatcher.php
+++ b/typo3/sysext/install/Configuration/ExtensionScanner/Php/MethodCallMatcher.php
@@ -2487,4 +2487,11 @@ return [
             'Deprecation-85554-PageRepository-checkWorkspaceAccess.rst',
         ],
     ],
+    'TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController->initTemplate' => [
+        'numberOfMandatoryArguments' => 0,
+        'maximumNumberOfArguments' => 0,
+        'restFiles' => [
+            'Deprecation-85666-TypoScriptFrontendController-initTemplate.rst',
+        ],
+    ],
 ];
diff --git a/typo3/sysext/redirects/Classes/Service/RedirectService.php b/typo3/sysext/redirects/Classes/Service/RedirectService.php
index 9593c42f13a7..3472fde447d6 100644
--- a/typo3/sysext/redirects/Classes/Service/RedirectService.php
+++ b/typo3/sysext/redirects/Classes/Service/RedirectService.php
@@ -247,7 +247,6 @@ class RedirectService implements LoggerAwareInterface
             GeneralUtility::_GP('type')
         );
         $GLOBALS['TSFE']->fetch_the_id();
-        $GLOBALS['TSFE']->initTemplate();
         $GLOBALS['TSFE']->getConfigArray();
         $GLOBALS['TSFE']->settingLanguage();
         $GLOBALS['TSFE']->settingLocale();
-- 
GitLab