TYPO3  7.6
TypoScriptReferenceLoader.php
Go to the documentation of this file.
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  */
19 
24 {
28  protected $xmlDoc;
29 
33  public function __construct()
34  {
35  $GLOBALS['LANG']->includeLLFile('EXT:t3editor/Resources/Private/Language/locallang.xlf');
36  }
37 
47  {
48  $parsedBody = $request->getParsedBody();
49  $queryParams = $request->getQueryParams();
50 
51  // Load the TSref XML information:
52  $this->loadFile(\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::extPath('t3editor') . 'Resources/Private/tsref.xml');
53  $fetch = isset($parsedBody['fetch']) ? $parsedBody['fetch'] : $queryParams['fetch'];
54  $content = null;
55 
56  switch ($fetch) {
57  case 'types':
58  $response->getBody()->write(json_encode($this->getTypes()));
59  break;
60  case 'description':
61  $typeId = isset($parsedBody['typeId']) ? $parsedBody['typeId'] : $queryParams['typeId'];
62  $parameterName = isset($parsedBody['parameterName']) ? $parsedBody['parameterName'] : $queryParams['parameterName'];
63  $response = $this->getDescription($typeId, $parameterName);
64  $response->withHeader('Content-Type', 'text/html; charset=utf8');
65  break;
66  }
67  return $response;
68  }
69 
76  protected function loadFile($filepath)
77  {
78  $this->xmlDoc = new \DOMDocument('1.0', 'utf-8');
79  $this->xmlDoc->load($filepath);
80  // @TODO: oliver@typo3.org: I guess this is not required here
81  $this->xmlDoc->saveXML();
82  }
83 
89  protected function getTypes()
90  {
91  $types = $this->xmlDoc->getElementsByTagName('type');
92  $typeArr = array();
93  foreach ($types as $type) {
94  $typeId = $type->getAttribute('id');
95  $typeName = $type->getAttribute('name');
96  if (!$typeName) {
97  $typeName = $typeId;
98  }
99  $properties = $type->getElementsByTagName('property');
100  $propArr = array();
101  foreach ($properties as $property) {
102  $p = array();
103  $p['name'] = $property->getAttribute('name');
104  $p['type'] = $property->getAttribute('type');
105  $propArr[$property->getAttribute('name')] = $p;
106  }
107  $typeArr[$typeId] = array();
108  $typeArr[$typeId]['properties'] = $propArr;
109  $typeArr[$typeId]['name'] = $typeName;
110  if ($type->hasAttribute('extends')) {
111  $typeArr[$typeId]['extends'] = $type->getAttribute('extends');
112  }
113  }
114  return $typeArr;
115  }
116 
124  protected function getDescription($typeId, $parameterName = '')
125  {
127  $response = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Http\Response::class);
128  if (!$typeId) {
129  $response->getBody()->write($GLOBALS['LANG']->getLL('typeIDMissing'));
130  $response = $response->withStatus(500);
131  return $response;
132  }
133  // getElementById does only work with schema
134  $type = $this->getType($typeId);
135  // Retrieve propertyDescription
136  if ($parameterName) {
137  $properties = $type->getElementsByTagName('property');
138  foreach ($properties as $propery) {
139  $propName = $propery->getAttribute('name');
140  if ($propName == $parameterName) {
141  $descriptions = $propery->getElementsByTagName('description');
142  if ($descriptions->length) {
143  $description = $descriptions->item(0)->textContent;
144  $description = htmlspecialchars($description);
145  $description = nl2br($description);
146  $response->getBody()->write($description);
147  break;
148  }
149  }
150  }
151  }
152  return $response;
153  }
154 
161  protected function getType($typeId)
162  {
163  $types = $this->xmlDoc->getElementsByTagName('type');
164  foreach ($types as $type) {
165  if ($type->getAttribute('id') == $typeId) {
166  return $type;
167  }
168  }
169  }
170 }