‪TYPO3CMS  9.5
JsonView.php
Go to the documentation of this file.
1 <?php
2 declare(strict_types = 1);
3 
5 
6 /*
7  * This file is part of the TYPO3 CMS project.
8  *
9  * It is free software; you can redistribute it and/or modify it under
10  * the terms of the GNU General Public License, either version 2
11  * of the License, or any later version.
12  *
13  * For the full copyright and license information, please read the
14  * LICENSE.txt file that was distributed with this source code.
15  *
16  * The TYPO3 project - inspiring people to share!
17  */
18 
19 use ‪TYPO3\CMS\Extbase\Mvc\Web\Response as WebResponse;
24 
29 {
39 
45 
49  protected ‪$reflectionService;
50 
54  protected ‪$controllerContext;
55 
61  protected ‪$variablesToRender = ['value'];
62 
161  protected ‪$configuration = [];
162 
166  protected ‪$persistenceManager;
167 
173  {
174  $this->persistenceManager = ‪$persistenceManager;
175  }
176 
182  {
183  $this->reflectionService = ‪$reflectionService;
184  }
185 
192  public function ‪setVariablesToRender(array ‪$variablesToRender): void
193  {
194  $this->variablesToRender = ‪$variablesToRender;
195  }
196 
200  public function ‪setConfiguration(array ‪$configuration): void
201  {
202  $this->configuration = ‪$configuration;
203  }
204 
212  public function ‪render(): string
213  {
214  $response = $this->controllerContext->getResponse();
215  if ($response instanceof WebResponse) {
216  // @todo Ticket: #63643 This should be solved differently once request/response model is available for TSFE.
217  if (!empty(‪$GLOBALS['TSFE']) && ‪$GLOBALS['TSFE'] instanceof TypoScriptFrontendController) {
219  $typoScriptFrontendController = ‪$GLOBALS['TSFE'];
220  if (empty($typoScriptFrontendController->config['config']['disableCharsetHeader'])) {
221  // If the charset header is *not* disabled in configuration,
222  // TypoScriptFrontendController will send the header later with the Content-Type which we set here.
223  $typoScriptFrontendController->setContentType('application/json');
224  } else {
225  // Although the charset header is disabled in configuration, we *must* send a Content-Type header here.
226  // Content-Type headers optionally carry charset information at the same time.
227  // Since we have the information about the charset, there is no reason to not include the charset information although disabled in TypoScript.
228  $response->setHeader('Content-Type', 'application/json; charset=' . trim($typoScriptFrontendController->metaCharset));
229  }
230  } else {
231  $response->setHeader('Content-Type', 'application/json');
232  }
233  }
234  $propertiesToRender = $this->‪renderArray();
235  return json_encode($propertiesToRender, JSON_UNESCAPED_UNICODE);
236  }
237 
244  protected function ‪renderArray()
245  {
246  if (count($this->variablesToRender) === 1) {
247  $variableName = current($this->variablesToRender);
248  $valueToRender = $this->variables[$variableName] ?? null;
249  ‪$configuration = $this->configuration[$variableName] ?? [];
250  } else {
251  $valueToRender = [];
252  foreach ($this->variablesToRender as $variableName) {
253  $valueToRender[$variableName] = $this->variables[$variableName] ?? null;
254  }
256  }
257  return $this->‪transformValue($valueToRender, ‪$configuration);
258  }
259 
268  protected function ‪transformValue($value, array ‪$configuration)
269  {
270  if (is_array($value) || $value instanceof \ArrayAccess) {
271  $array = [];
272  foreach ($value as $key => $element) {
273  if (isset(‪$configuration['_descendAll']) && is_array(‪$configuration['_descendAll'])) {
274  $array[$key] = $this->‪transformValue($element, ‪$configuration['_descendAll']);
275  } else {
276  if (isset(‪$configuration['_only']) && is_array(‪$configuration['_only']) && !in_array($key, ‪$configuration['_only'], true)) {
277  continue;
278  }
279  if (isset(‪$configuration['_exclude']) && is_array(‪$configuration['_exclude']) && in_array($key, ‪$configuration['_exclude'], true)) {
280  continue;
281  }
282  $array[$key] = $this->‪transformValue($element, ‪$configuration[$key] ?? []);
283  }
284  }
285  return $array;
286  }
287  if (is_object($value)) {
288  return $this->‪transformObject($value, ‪$configuration);
289  }
290  return $value;
291  }
292 
301  protected function ‪transformObject(object $object, array ‪$configuration)
302  {
303  if ($object instanceof \DateTime) {
304  return $object->format(\DateTime::ATOM);
305  }
306  $propertyNames = ‪ObjectAccess::getGettablePropertyNames($object);
307 
308  $propertiesToRender = [];
309  foreach ($propertyNames as $propertyName) {
310  if (isset(‪$configuration['_only']) && is_array(‪$configuration['_only']) && !in_array($propertyName, ‪$configuration['_only'], true)) {
311  continue;
312  }
313  if (isset(‪$configuration['_exclude']) && is_array(‪$configuration['_exclude']) && in_array($propertyName, ‪$configuration['_exclude'], true)) {
314  continue;
315  }
316 
317  $propertyValue = ‪ObjectAccess::getProperty($object, $propertyName);
318 
319  if (!is_array($propertyValue) && !is_object($propertyValue)) {
320  $propertiesToRender[$propertyName] = $propertyValue;
321  } elseif (isset(‪$configuration['_descend']) && array_key_exists($propertyName, ‪$configuration['_descend'])) {
322  $propertiesToRender[$propertyName] = $this->‪transformValue($propertyValue, ‪$configuration['_descend'][$propertyName]);
323  }
324  }
325  if (isset(‪$configuration['_exposeObjectIdentifier']) && ‪$configuration['_exposeObjectIdentifier'] === true) {
326  if (isset(‪$configuration['_exposedObjectIdentifierKey']) && strlen(‪$configuration['_exposedObjectIdentifierKey']) > 0) {
327  $identityKey = ‪$configuration['_exposedObjectIdentifierKey'];
328  } else {
329  $identityKey = '__identity';
330  }
331  $propertiesToRender[$identityKey] = $this->persistenceManager->getIdentifierByObject($object);
332  }
333  if (isset(‪$configuration['_exposeClassName']) && (‪$configuration['_exposeClassName'] === self::EXPOSE_CLASSNAME_FULLY_QUALIFIED || ‪$configuration['_exposeClassName'] === self::EXPOSE_CLASSNAME_UNQUALIFIED)) {
334  $className = get_class($object);
335  $classNameParts = explode('\\', $className);
336  $propertiesToRender['__class'] = (‪$configuration['_exposeClassName'] === self::EXPOSE_CLASSNAME_FULLY_QUALIFIED ? $className : array_pop($classNameParts));
337  }
338 
339  return $propertiesToRender;
340  }
341 }
‪TYPO3\CMS\Extbase\Mvc\View\JsonView\$persistenceManager
‪PersistenceManagerInterface $persistenceManager
Definition: JsonView.php:161
‪TYPO3\CMS\Extbase\Mvc\View\JsonView\$variablesToRender
‪string[] $variablesToRender
Definition: JsonView.php:58
‪TYPO3\CMS\Extbase\Persistence\PersistenceManagerInterface
Definition: PersistenceManagerInterface.php:21
‪TYPO3\CMS\Extbase\Mvc\View\JsonView\$configuration
‪array $configuration
Definition: JsonView.php:157
‪TYPO3\CMS\Extbase\Mvc\View\JsonView\$reflectionService
‪ReflectionService $reflectionService
Definition: JsonView.php:48
‪TYPO3\CMS\Extbase\Mvc\View\JsonView\injectPersistenceManager
‪injectPersistenceManager(PersistenceManagerInterface $persistenceManager)
Definition: JsonView.php:167
‪TYPO3\CMS\Extbase\Reflection\ObjectAccess
Definition: ObjectAccess.php:29
‪TYPO3\CMS\Extbase\Reflection\ReflectionService
Definition: ReflectionService.php:27
‪TYPO3\CMS\Extbase\Mvc\View\JsonView\transformObject
‪array string transformObject(object $object, array $configuration)
Definition: JsonView.php:296
‪TYPO3\CMS\Extbase\Mvc\View\JsonView\injectReflectionService
‪injectReflectionService(ReflectionService $reflectionService)
Definition: JsonView.php:176
‪TYPO3\CMS\Extbase\Mvc\View\JsonView\EXPOSE_CLASSNAME_FULLY_QUALIFIED
‪const EXPOSE_CLASSNAME_FULLY_QUALIFIED
Definition: JsonView.php:38
‪TYPO3\CMS\Extbase\Mvc\Web\Response
Definition: Response.php:25
‪TYPO3\CMS\Extbase\Mvc\View\JsonView\setConfiguration
‪setConfiguration(array $configuration)
Definition: JsonView.php:195
‪TYPO3\CMS\Extbase\Reflection\ObjectAccess\getProperty
‪static mixed getProperty($subject, $propertyName, $forceDirectAccess=false)
Definition: ObjectAccess.php:54
‪TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController
Definition: TypoScriptFrontendController.php:97
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:5
‪TYPO3\CMS\Extbase\Mvc\View\JsonView\renderArray
‪mixed renderArray()
Definition: JsonView.php:239
‪TYPO3\CMS\Extbase\Mvc\View\JsonView\$controllerContext
‪TYPO3 CMS Extbase Mvc Controller ControllerContext $controllerContext
Definition: JsonView.php:52
‪TYPO3\CMS\Extbase\Mvc\View\JsonView\EXPOSE_CLASSNAME_UNQUALIFIED
‪const EXPOSE_CLASSNAME_UNQUALIFIED
Definition: JsonView.php:44
‪TYPO3\CMS\Extbase\Mvc\View
Definition: AbstractView.php:2
‪TYPO3\CMS\Extbase\Mvc\View\JsonView\setVariablesToRender
‪setVariablesToRender(array $variablesToRender)
Definition: JsonView.php:187
‪TYPO3\CMS\Extbase\Mvc\View\JsonView\render
‪string render()
Definition: JsonView.php:207
‪TYPO3\CMS\Extbase\Mvc\View\JsonView\transformValue
‪mixed transformValue($value, array $configuration)
Definition: JsonView.php:263
‪TYPO3\CMS\Extbase\Mvc\View\JsonView
Definition: JsonView.php:29
‪TYPO3\CMS\Extbase\Mvc\View\AbstractView
Definition: AbstractView.php:21
‪TYPO3\CMS\Extbase\Reflection\ObjectAccess\getGettablePropertyNames
‪static array getGettablePropertyNames($object)
Definition: ObjectAccess.php:227