[TASK] Streamline phpdoc annotations in EXT:fluid
[Packages/TYPO3.CMS.git] / typo3 / sysext / fluid / Classes / ViewHelpers / Debug / RenderViewHelper.php
1 <?php
2 namespace TYPO3\CMS\Fluid\ViewHelpers\Debug;
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
17 use TYPO3\CMS\Core\Core\Environment;
18 use TYPO3\CMS\Core\Utility\PathUtility;
19 use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
20
21 /**
22 * Debuggable version of f:render - performs the same
23 * rendering operation but wraps the output with HTML
24 * that can be inspected with the admin panel in FE.
25 *
26 * Replaces `f:render` when the admin panel decides
27 * (see ViewHelperResolver class). Also possible to use
28 * explicitly by using `f:debug.render` instead of the
29 * normal `f:render` statement.
30 */
31 class RenderViewHelper extends AbstractViewHelper
32 {
33
34 /**
35 * @var bool
36 */
37 protected $escapeOutput = false;
38
39 /**
40 * Initializes additional arguments available for this view helper.
41 */
42 public function initializeArguments()
43 {
44 parent::initializeArguments();
45 $this->registerArgument('debug', 'boolean', 'If true, the admin panel shows debug information if activated,', false, true);
46 $this->registerArgument('section', 'string', 'Section to render - combine with partial to render section in partial', false, null);
47 $this->registerArgument('partial', 'string', 'Partial to render, with or without section', false, null);
48 $this->registerArgument('arguments', 'array', 'Array of variables to be transferred. Use {_all} for all variables', false, []);
49 $this->registerArgument('optional', 'boolean', 'If TRUE, considers the *section* optional. Partial never is.', false, false);
50 $this->registerArgument('default', 'mixed', 'Value (usually string) to be displayed if the section or partial does not exist', false, null);
51 $this->registerArgument('contentAs', 'string', 'If used, renders the child content and adds it as a template variable with this name for use in the partial/section', false, null);
52 }
53
54 /**
55 * Renders the content.
56 *
57 * @return string
58 */
59 public function render()
60 {
61 $isDebug = $this->arguments['debug'];
62 $section = $this->arguments['section'];
63 $partial = $this->arguments['partial'];
64 $arguments = (array)$this->arguments['arguments'];
65 $optional = (boolean)$this->arguments['optional'];
66 $contentAs = $this->arguments['contentAs'];
67 $tagContent = $this->renderChildren();
68
69 if ($contentAs !== null) {
70 $arguments[$contentAs] = $tagContent;
71 }
72
73 $content = '';
74 if ($partial !== null) {
75 $content = $this->viewHelperVariableContainer->getView()->renderPartial($partial, $section, $arguments, $optional);
76 } elseif ($section !== null) {
77 $content = $this->viewHelperVariableContainer->getView()->renderSection($section, $arguments, $optional);
78 }
79 // Replace empty content with default value. If default is
80 // not set, NULL is returned and cast to a new, empty string
81 // outside of this ViewHelper.
82 if ($content === '') {
83 $content = $this->arguments['default'] ?? $tagContent;
84 }
85
86 // if debug is disabled, return content
87 if (!$isDebug) {
88 return $content;
89 }
90
91 $cssRules = [];
92 $cssRules[] = 'display: block';
93 $cssRules[] = 'background-color: #fff';
94 $cssRules[] = 'padding: 5px';
95 $cssRules[] = 'border: 1px solid #f00';
96 $cssRules[] = 'color: #000';
97 $cssRules[] = 'overflow: hidden';
98 $cssWrapper = implode(';', $cssRules);
99 $cssRules[] = 'font-size: 11px';
100 $cssRules[] = 'font-family: Monospace';
101 $cssTitle = implode(';', $cssRules);
102
103 $debugInfo = [];
104 if (isset($this->arguments['partial'])) {
105 $path = $this->renderingContext->getTemplatePaths()->getPartialPathAndFilename($partial);
106 $path = str_replace(
107 [
108 Environment::getBackendPath() . '/ext/',
109 Environment::getExtensionsPath() . '/',
110 Environment::getFrameworkBasePath() . '/'
111 ],
112 'EXT:',
113 $path
114 );
115 $path = PathUtility::stripPathSitePrefix($path);
116 $debugInfo['Partial'] = 'Partial: ' . $path;
117 }
118 if (isset($this->arguments['section'])) {
119 $debugInfo['Section'] = 'Section: ' . htmlspecialchars($section);
120 }
121
122 $debugContent = sprintf(
123 '<strong>%s</strong>',
124 implode('<br />', $debugInfo)
125 );
126
127 return sprintf(
128 '<div class="t3js-debug-template" title="%s" style="%s"><span style="%s">%s</span>%s</div>',
129 htmlspecialchars(implode('/', array_keys($debugInfo))),
130 $cssTitle,
131 $cssWrapper,
132 $debugContent,
133 $content
134 );
135 }
136 }