[BUGFIX] Fix several typos in php comments
[Packages/TYPO3.CMS.git] / typo3 / sysext / t3editor / Classes / Controller / TypoScriptReferenceController.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\JsonResponse;
19 use TYPO3\CMS\Core\Utility\GeneralUtility;
20
21 /**
22 * Loads TSref information from a XML file and responds to an AJAX call.
23 * @internal This is a specific Backend Controller implementation and is not considered part of the Public TYPO3 API.
24 */
25 class TypoScriptReferenceController
26 {
27 /**
28 * @var \DOMDocument
29 */
30 protected $xmlDoc;
31
32 /**
33 * Load TypoScript reference
34 *
35 * @param ServerRequestInterface $request
36 * @return ResponseInterface
37 */
38 public function loadReference(ServerRequestInterface $request): ResponseInterface
39 {
40 // Load the TSref XML information:
41 $this->loadFile(GeneralUtility::getFileAbsFileName('EXT:t3editor/Resources/Private/tsref.xml'));
42 return (new JsonResponse())->setPayload($this->getTypes());
43 }
44
45 /**
46 * Load XML file
47 *
48 * @param string $filepath
49 */
50 protected function loadFile($filepath)
51 {
52 // Disables the functionality to allow external entities to be loaded when parsing the XML, must be kept
53 $previousValueOfEntityLoader = libxml_disable_entity_loader();
54 $this->xmlDoc = new \DOMDocument('1.0', 'utf-8');
55 $this->xmlDoc->loadXML(file_get_contents($filepath));
56 libxml_disable_entity_loader($previousValueOfEntityLoader);
57 // @TODO: oliver@typo3.org: I guess this is not required here
58 $this->xmlDoc->saveXML();
59 }
60
61 /**
62 * Get types from XML
63 *
64 * @return array
65 */
66 protected function getTypes(): array
67 {
68 $types = $this->xmlDoc->getElementsByTagName('type');
69 $typeArr = [];
70 foreach ($types as $type) {
71 $typeId = $type->getAttribute('id');
72 $typeName = $type->getAttribute('name');
73 if (!$typeName) {
74 $typeName = $typeId;
75 }
76 $properties = $type->getElementsByTagName('property');
77 $propArr = [];
78 foreach ($properties as $property) {
79 $p = [];
80 $p['name'] = $property->getAttribute('name');
81 $p['type'] = $property->getAttribute('type');
82 $propArr[$property->getAttribute('name')] = $p;
83 }
84 $typeArr[$typeId] = [];
85 $typeArr[$typeId]['properties'] = $propArr;
86 $typeArr[$typeId]['name'] = $typeName;
87 if ($type->hasAttribute('extends')) {
88 $typeArr[$typeId]['extends'] = $type->getAttribute('extends');
89 }
90 }
91 return $typeArr;
92 }
93 }