7a58c7375b0f1b5e2eead38bf54c79381c67e6c8
[Packages/TYPO3.CMS.git] / typo3 / sysext / t3editor / Classes / TypoScriptReferenceLoader.php
1 <?php
2 namespace TYPO3\CMS\T3editor;
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\JsonResponse;
19 use TYPO3\CMS\Core\Utility\GeneralUtility;
20
21 /**
22 * Loads TSref information from a XML file an responds to an AJAX call.
23 */
24 class TypoScriptReferenceLoader
25 {
26 /**
27 * @var \DOMDocument
28 */
29 protected $xmlDoc;
30
31 /**
32 * Default constructor
33 */
34 public function __construct()
35 {
36 $GLOBALS['LANG']->includeLLFile('EXT:t3editor/Resources/Private/Language/locallang.xlf');
37 }
38
39 /**
40 * General processor for AJAX requests.
41 * Called by AjaxRequestHandler
42 *
43 * @param ServerRequestInterface $request
44 * @return ResponseInterface
45 */
46 public function processAjaxRequest(ServerRequestInterface $request): ResponseInterface
47 {
48 // Load the TSref XML information:
49 $this->loadFile(GeneralUtility::getFileAbsFileName('EXT:t3editor/Resources/Private/tsref.xml'));
50 return GeneralUtility::makeInstance(JsonResponse::class)->setPayload($this->getTypes());
51 }
52
53 /**
54 * Load XML file
55 *
56 * @param string $filepath
57 */
58 protected function loadFile($filepath)
59 {
60 // Disables the functionality to allow external entities to be loaded when parsing the XML, must be kept
61 $previousValueOfEntityLoader = libxml_disable_entity_loader();
62 $this->xmlDoc = new \DOMDocument('1.0', 'utf-8');
63 $this->xmlDoc->loadXML(file_get_contents($filepath));
64 libxml_disable_entity_loader($previousValueOfEntityLoader);
65 // @TODO: oliver@typo3.org: I guess this is not required here
66 $this->xmlDoc->saveXML();
67 }
68
69 /**
70 * Get types from XML
71 *
72 * @return array
73 */
74 protected function getTypes() : array
75 {
76 $types = $this->xmlDoc->getElementsByTagName('type');
77 $typeArr = [];
78 foreach ($types as $type) {
79 $typeId = $type->getAttribute('id');
80 $typeName = $type->getAttribute('name');
81 if (!$typeName) {
82 $typeName = $typeId;
83 }
84 $properties = $type->getElementsByTagName('property');
85 $propArr = [];
86 foreach ($properties as $property) {
87 $p = [];
88 $p['name'] = $property->getAttribute('name');
89 $p['type'] = $property->getAttribute('type');
90 $propArr[$property->getAttribute('name')] = $p;
91 }
92 $typeArr[$typeId] = [];
93 $typeArr[$typeId]['properties'] = $propArr;
94 $typeArr[$typeId]['name'] = $typeName;
95 if ($type->hasAttribute('extends')) {
96 $typeArr[$typeId]['extends'] = $type->getAttribute('extends');
97 }
98 }
99 return $typeArr;
100 }
101 }