TYPO3 CMS  TYPO3_7-6
JsonView.php
Go to the documentation of this file.
1 <?php
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 
19 
25 class JsonView extends AbstractView
26 {
36 
42 
46  protected $reflectionService;
47 
51  protected $controllerContext;
52 
58  protected $variablesToRender = ['value'];
59 
158  protected $configuration = [];
159 
164 
168  public function injectPersistenceManager(\TYPO3\CMS\Extbase\Persistence\PersistenceManagerInterface $persistenceManager)
169  {
170  $this->persistenceManager = $persistenceManager;
171  }
172 
176  public function injectReflectionService(\TYPO3\CMS\Extbase\Reflection\ReflectionService $reflectionService)
177  {
178  $this->reflectionService = $reflectionService;
179  }
180 
190  {
191  $this->variablesToRender = $variablesToRender;
192  }
193 
198  public function setConfiguration(array $configuration)
199  {
200  $this->configuration = $configuration;
201  }
202 
211  public function render()
212  {
213  $response = $this->controllerContext->getResponse();
214  if ($response instanceof WebResponse) {
215  // @todo Ticket: #63643 This should be solved differently once request/response model is available for TSFE.
216  if (!empty($GLOBALS['TSFE']) && $GLOBALS['TSFE'] instanceof TypoScriptFrontendController) {
218  $typoScriptFrontendController = $GLOBALS['TSFE'];
219  if (empty($typoScriptFrontendController->config['config']['disableCharsetHeader'])) {
220  // If the charset header is *not* disabled in configuration,
221  // TypoScriptFrontendController will send the header later with the Content-Type which we set here.
222  $typoScriptFrontendController->setContentType('application/json');
223  } else {
224  // Although the charset header is disabled in configuration, we *must* send a Content-Type header here.
225  // Content-Type headers optionally carry charset information at the same time.
226  // Since we have the information about the charset, there is no reason to not include the charset information although disabled in TypoScript.
227  $response->setHeader('Content-Type', 'application/json; charset=' . trim($typoScriptFrontendController->metaCharset));
228  }
229  } else {
230  $response->setHeader('Content-Type', 'application/json');
231  }
232  }
233  $propertiesToRender = $this->renderArray();
234  return json_encode($propertiesToRender);
235  }
236 
244  protected function renderArray()
245  {
246  if (count($this->variablesToRender) === 1) {
247  $variableName = current($this->variablesToRender);
248  $valueToRender = isset($this->variables[$variableName]) ? $this->variables[$variableName] : null;
249  $configuration = isset($this->configuration[$variableName]) ? $this->configuration[$variableName] : [];
250  } else {
251  $valueToRender = [];
252  foreach ($this->variablesToRender as $variableName) {
253  $valueToRender[$variableName] = isset($this->variables[$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'])) {
277  continue;
278  }
279  if (isset($configuration['_exclude']) && is_array($configuration['_exclude']) && in_array($key, $configuration['_exclude'])) {
280  continue;
281  }
282  $array[$key] = $this->transformValue($element, isset($configuration[$key]) ? $configuration[$key] : []);
283  }
284  }
285  return $array;
286  } elseif (is_object($value)) {
287  return $this->transformObject($value, $configuration);
288  } else {
289  return $value;
290  }
291  }
292 
301  protected function transformObject($object, array $configuration)
302  {
303  if ($object instanceof \DateTime) {
304  return $object->format(\DateTime::ISO8601);
305  } else {
307 
308  $propertiesToRender = [];
309  foreach ($propertyNames as $propertyName) {
310  if (isset($configuration['_only']) && is_array($configuration['_only']) && !in_array($propertyName, $configuration['_only'])) {
311  continue;
312  }
313  if (isset($configuration['_exclude']) && is_array($configuration['_exclude']) && in_array($propertyName, $configuration['_exclude'])) {
314  continue;
315  }
316 
317  $propertyValue = \TYPO3\CMS\Extbase\Reflection\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  }
342 }
transformValue($value, array $configuration)
Definition: JsonView.php:268
static getProperty($subject, $propertyName, $forceDirectAccess=false)
injectPersistenceManager(\TYPO3\CMS\Extbase\Persistence\PersistenceManagerInterface $persistenceManager)
Definition: JsonView.php:168
transformObject($object, array $configuration)
Definition: JsonView.php:301
injectReflectionService(\TYPO3\CMS\Extbase\Reflection\ReflectionService $reflectionService)
Definition: JsonView.php:176
if(TYPO3_MODE==='BE') $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tsfebeuserauth.php']['frontendEditingController']['default']
setConfiguration(array $configuration)
Definition: JsonView.php:198
setVariablesToRender(array $variablesToRender)
Definition: JsonView.php:189