[BUGFIX] Fix several typos in php comments
[Packages/TYPO3.CMS.git] / typo3 / sysext / t3editor / Classes / Controller / CodeCompletionController.php
1 <?php
2 namespace TYPO3\CMS\T3editor\Controller;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16 use Psr\Http\Message\ResponseInterface;
17 use Psr\Http\Message\ServerRequestInterface;
18 use TYPO3\CMS\Core\Http\HtmlResponse;
19 use TYPO3\CMS\Core\Http\JsonResponse;
20 use TYPO3\CMS\Core\Localization\LanguageService;
21 use TYPO3\CMS\Core\TypoScript\ExtendedTemplateService;
22 use TYPO3\CMS\Core\Utility\GeneralUtility;
23 use TYPO3\CMS\Core\Utility\RootlineUtility;
24
25 /**
26 * Code completion for t3editor
27 * @internal This is a specific Backend Controller implementation and is not considered part of the Public TYPO3 API.
28 */
29 class CodeCompletionController
30 {
31 /**
32 * Loads all templates up to a given page id (walking the rootline) and
33 * cleans parts that are not required for the t3editor codecompletion.
34 *
35 * @param ServerRequestInterface $request
36 * @return ResponseInterface
37 */
38 public function loadCompletions(ServerRequestInterface $request): ResponseInterface
39 {
40 // Check whether access is granted (only admin have access to sys_template records):
41 if (!$GLOBALS['BE_USER']->isAdmin()) {
42 return new HtmlResponse($this->getLanguageService()->sL('LLL:EXT:t3editor/Resources/Private/Language/locallang.xlf:noPermission'), 500);
43 }
44 $pageId = (int)($request->getParsedBody()['pageId'] ?? $request->getQueryParams()['pageId']);
45 // Check whether there is a pageId given:
46 if (!$pageId) {
47 return new HtmlResponse($this->getLanguageService()->sL('LLL:EXT:t3editor/Resources/Private/Language/locallang.xlf:pageIDInteger'), 500);
48 }
49 // Fetch the templates
50 return (new JsonResponse())->setPayload($this->getMergedTemplates($pageId));
51 }
52
53 /**
54 * Gets merged templates by walking the rootline to a given page id.
55 *
56 * @todo oliver@typo3.org: Refactor this method and comment what's going on there
57 * @param int $pageId
58 * @return array Setup part of merged template records
59 */
60 protected function getMergedTemplates($pageId)
61 {
62 $tsParser = GeneralUtility::makeInstance(ExtendedTemplateService::class);
63 // Gets the rootLine
64 $rootLine = GeneralUtility::makeInstance(RootlineUtility::class, $pageId)->get();
65 // This generates the constants/config + hierarchy info for the template.
66 $tsParser->runThroughTemplates($rootLine);
67 // ts-setup & ts-constants of the currently edited template should not be included
68 // therefor we have to delete the last template from the stack
69 array_pop($tsParser->config);
70 array_pop($tsParser->constants);
71 $tsParser->linkObjects = true;
72 $tsParser->ext_regLinenumbers = false;
73 $tsParser->generateConfig();
74 $result = $this->treeWalkCleanup($tsParser->setup);
75 return $result;
76 }
77
78 /**
79 * Walks through a tree of TypoScript configuration and cleans it up.
80 *
81 * @TODO oliver@typo3.org: Define and comment why this is necessary and exactly happens below
82 * @param array $treeBranch TypoScript configuration or sub branch of it
83 * @return array Cleaned TypoScript branch
84 */
85 private function treeWalkCleanup(array $treeBranch)
86 {
87 $cleanedTreeBranch = [];
88 foreach ($treeBranch as $key => $value) {
89 $dotCount = substr_count($key, '.');
90 //type definition or value-assignment
91 if ($dotCount === 0) {
92 if ($value != '') {
93 if (strlen($value) > 20) {
94 $value = substr($value, 0, 20);
95 }
96 if (!isset($cleanedTreeBranch[$key])) {
97 $cleanedTreeBranch[$key] = [];
98 }
99 $cleanedTreeBranch[$key]['v'] = $value;
100 }
101 } elseif ($dotCount == 1) {
102 // subtree (definition of properties)
103 $subBranch = $this->treeWalkCleanup($value);
104 if ($subBranch) {
105 $key = str_replace('.', '', $key);
106 if (!isset($cleanedTreeBranch[$key])) {
107 $cleanedTreeBranch[$key] = [];
108 }
109 $cleanedTreeBranch[$key]['c'] = $subBranch;
110 }
111 }
112 }
113 return $cleanedTreeBranch;
114 }
115
116 /**
117 * @return LanguageService
118 */
119 protected function getLanguageService(): LanguageService
120 {
121 return $GLOBALS['LANG'];
122 }
123 }