41b4194bc667d80bc5d007d4c5d330810c21734c
[Packages/TYPO3.CMS.git] / typo3 / sysext / fluid / Classes / ViewHelpers / Info / AbstractInfoViewHelper.php
1 <?php
2 namespace TYPO3\CMS\Fluid\ViewHelpers\Info;
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 TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper;
17 use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;
18 use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
19
20 /**
21 * Base class for information retrieval ViewHelpers
22 */
23 abstract class AbstractInfoViewHelper extends AbstractViewHelper
24 {
25 /**
26 * Initialize/register arguments
27 *
28 * @return void
29 */
30 public function initializeArguments()
31 {
32 $this->registerArgument('as', 'string', 'Optional template variable to assign - if not used, ViewHelper returns the info array directly');
33 }
34
35 /**
36 * @return string|array
37 */
38 public function render()
39 {
40 return static::renderStatic($this->arguments, $this->buildRenderChildrenClosure(), $this->renderingContext);
41 }
42
43 /**
44 * @return array
45 */
46 protected static function getData()
47 {
48 return [];
49 }
50
51 /**
52 * @return TypoScriptFrontendController
53 */
54 protected static function getTypoScriptFrontendController()
55 {
56 return $GLOBALS['TSFE'];
57 }
58
59 /**
60 * @param array $arguments
61 * @param \Closure $renderChildrenClosure
62 * @param RenderingContextInterface $renderingContext
63 * @return string|array
64 */
65 public static function renderStatic(
66 array $arguments,
67 \Closure $renderChildrenClosure,
68 RenderingContextInterface $renderingContext)
69 {
70 $data = static::getTypoScriptFrontendController()->cObj->data;
71 if (empty($arguments['as'])) {
72 return $data;
73 } else {
74 $provider = $renderingContext->getVariableProvider();
75 $provider->add($arguments['as'], $data);
76 $content = $renderChildrenClosure();
77 $provider->remove($arguments['as']);
78 return $content;
79 }
80 }
81 }