From 74f46b023a2243773e8d4dac6f79aab0dbcb3837 Mon Sep 17 00:00:00 2001
From: Benni Mack <benni@typo3.org>
Date: Fri, 25 May 2018 12:23:01 +0200
Subject: [PATCH] [TASK] Cleanup t3editor controllers and docs

EXT:t3editor has some AJAX controllers which can be simplified:
- Unused properties are removed
- Unrelated "LANG" calls can be streamlined, resulting in a lowered
  memory footprint
- Rootline resolving is done directly via the existing class
- Remove obsolete usage of PhpFrontend in phpDoc in T3editor base class

Resolves: #85081
Releases: master
Change-Id: Ia5669d161c59881e509d9b7e881cc555ef2b44c1
Reviewed-on: https://review.typo3.org/57051
Reviewed-by: Andreas Fernandez <a.fernandez@scripting-base.de>
Tested-by: Andreas Fernandez <a.fernandez@scripting-base.de>
Tested-by: TYPO3com <no-reply@typo3.com>
Reviewed-by: Wouter Wolters <typo3@wouterwolters.nl>
Tested-by: Wouter Wolters <typo3@wouterwolters.nl>
---
 .../Controller/CodeCompletionController.php   | 66 ++++++++-----------
 .../TypoScriptReferenceController.php         |  8 ---
 typo3/sysext/t3editor/Classes/T3editor.php    |  2 +-
 3 files changed, 27 insertions(+), 49 deletions(-)

diff --git a/typo3/sysext/t3editor/Classes/Controller/CodeCompletionController.php b/typo3/sysext/t3editor/Classes/Controller/CodeCompletionController.php
index a9e9980d8af1..b10bf7c0efc0 100644
--- a/typo3/sysext/t3editor/Classes/Controller/CodeCompletionController.php
+++ b/typo3/sysext/t3editor/Classes/Controller/CodeCompletionController.php
@@ -17,56 +17,36 @@ use Psr\Http\Message\ResponseInterface;
 use Psr\Http\Message\ServerRequestInterface;
 use TYPO3\CMS\Core\Http\HtmlResponse;
 use TYPO3\CMS\Core\Http\JsonResponse;
+use TYPO3\CMS\Core\Localization\LanguageService;
+use TYPO3\CMS\Core\TypoScript\ExtendedTemplateService;
+use TYPO3\CMS\Core\Utility\GeneralUtility;
+use TYPO3\CMS\Core\Utility\RootlineUtility;
 
 /**
  * Code completion for t3editor
  */
 class CodeCompletionController
 {
-    /**
-     * @var \TYPO3\CMS\Core\Http\AjaxRequestHandler
-     */
-    protected $ajaxObj;
-
-    /**
-     * Default constructor
-     */
-    public function __construct()
-    {
-        $GLOBALS['LANG']->includeLLFile('EXT:t3editor/Resources/Private/Language/locallang.xlf');
-    }
-
-    /**
-     * General processor for AJAX requests.
-     * Called by AjaxRequestHandler
-     *
-     * @param ServerRequestInterface $request
-     * @return ResponseInterface
-     */
-    public function loadCompletions(ServerRequestInterface $request): ResponseInterface
-    {
-        $pageId = (int)($request->getParsedBody()['pageId'] ?? $request->getQueryParams()['pageId']);
-        return $this->loadTemplates($pageId);
-    }
-
     /**
      * Loads all templates up to a given page id (walking the rootline) and
      * cleans parts that are not required for the t3editor codecompletion.
      *
-     * @param int $pageId ID of the page
+     * @param ServerRequestInterface $request
      * @return ResponseInterface
      */
-    protected function loadTemplates($pageId): ResponseInterface
+    public function loadCompletions(ServerRequestInterface $request): ResponseInterface
     {
         // Check whether access is granted (only admin have access to sys_template records):
-        if ($GLOBALS['BE_USER']->isAdmin()) {
-            // Check whether there is a pageId given:
-            if ($pageId) {
-                return (new JsonResponse())->setPayload($this->getMergedTemplates($pageId));
-            }
-            return new HtmlResponse($GLOBALS['LANG']->getLL('pageIDInteger'), 500);
+        if (!$GLOBALS['BE_USER']->isAdmin()) {
+            return new HtmlResponse($this->getLanguageService()->sL('LLL:EXT:t3editor/Resources/Private/Language/locallang.xlf:noPermission'), 500);
         }
-        return new HtmlResponse($GLOBALS['LANG']->getLL('noPermission'), 500);
+        $pageId = (int)($request->getParsedBody()['pageId'] ?? $request->getQueryParams()['pageId']);
+        // Check whether there is a pageId given:
+        if (!$pageId) {
+            return new HtmlResponse($this->getLanguageService()->sL('LLL:EXT:t3editor/Resources/Private/Language/locallang.xlf:pageIDInteger'), 500);
+        }
+        // Fetch the templates
+        return (new JsonResponse())->setPayload($this->getMergedTemplates($pageId));
     }
 
     /**
@@ -78,12 +58,10 @@ class CodeCompletionController
      */
     protected function getMergedTemplates($pageId)
     {
-        /** @var $tsParser \TYPO3\CMS\Core\TypoScript\ExtendedTemplateService */
-        $tsParser = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Core\TypoScript\ExtendedTemplateService::class);
+        $tsParser = GeneralUtility::makeInstance(ExtendedTemplateService::class);
         $tsParser->init();
         // Gets the rootLine
-        $page = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Frontend\Page\PageRepository::class);
-        $rootLine = $page->getRootLine($pageId);
+        $rootLine = GeneralUtility::makeInstance(RootlineUtility::class, $pageId)->get();
         // This generates the constants/config + hierarchy info for the template.
         $tsParser->runThroughTemplates($rootLine);
         // ts-setup & ts-constants of the currently edited template should not be included
@@ -110,7 +88,7 @@ class CodeCompletionController
         foreach ($treeBranch as $key => $value) {
             $dotCount = substr_count($key, '.');
             //type definition or value-assignment
-            if ($dotCount == 0) {
+            if ($dotCount === 0) {
                 if ($value != '') {
                     if (strlen($value) > 20) {
                         $value = substr($value, 0, 20);
@@ -134,4 +112,12 @@ class CodeCompletionController
         }
         return $cleanedTreeBranch;
     }
+
+    /**
+     * @return LanguageService
+     */
+    protected function getLanguageService(): LanguageService
+    {
+        return $GLOBALS['LANG'];
+    }
 }
diff --git a/typo3/sysext/t3editor/Classes/Controller/TypoScriptReferenceController.php b/typo3/sysext/t3editor/Classes/Controller/TypoScriptReferenceController.php
index 74837b72aacb..860e61c7b2df 100644
--- a/typo3/sysext/t3editor/Classes/Controller/TypoScriptReferenceController.php
+++ b/typo3/sysext/t3editor/Classes/Controller/TypoScriptReferenceController.php
@@ -28,14 +28,6 @@ class TypoScriptReferenceController
      */
     protected $xmlDoc;
 
-    /**
-     * Default constructor
-     */
-    public function __construct()
-    {
-        $GLOBALS['LANG']->includeLLFile('EXT:t3editor/Resources/Private/Language/locallang.xlf');
-    }
-
     /**
      * Load TypoScript reference
      *
diff --git a/typo3/sysext/t3editor/Classes/T3editor.php b/typo3/sysext/t3editor/Classes/T3editor.php
index 8faf3e0a6393..172bdae8f4db 100644
--- a/typo3/sysext/t3editor/Classes/T3editor.php
+++ b/typo3/sysext/t3editor/Classes/T3editor.php
@@ -143,7 +143,7 @@ class T3editor implements SingletonInterface
     }
 
     /**
-     * @return PhpFrontend
+     * @return FrontendInterface
      * @throws \TYPO3\CMS\Core\Cache\Exception\NoSuchCacheException
      * @throws \InvalidArgumentException
      */
-- 
GitLab