[TASK] Update php-cs-fixer to 2.5.0
[Packages/TYPO3.CMS.git] / typo3 / sysext / fluid / Classes / Core / ViewHelper / AbstractViewHelper.php
1 <?php
2 namespace TYPO3\CMS\Fluid\Core\ViewHelper;
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 Psr\Log\LoggerInterface;
18 use TYPO3\CMS\Core\Log\LogManager;
19 use TYPO3\CMS\Core\Utility\GeneralUtility;
20 use TYPO3\CMS\Extbase\Reflection\ReflectionService;
21
22 /**
23 * The abstract base class for all view helpers.
24 *
25 * @api
26 */
27 abstract class AbstractViewHelper extends \TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper implements \TYPO3Fluid\Fluid\Core\ViewHelper\ViewHelperInterface
28 {
29 /**
30 * Controller Context to use
31 *
32 * @var \TYPO3\CMS\Extbase\Mvc\Controller\ControllerContext
33 * @api
34 */
35 protected $controllerContext;
36
37 /**
38 * @var \TYPO3\CMS\Extbase\Object\ObjectManagerInterface
39 */
40 protected $objectManager;
41
42 /**
43 * @param \TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface $renderingContext
44 */
45 public function setRenderingContext(\TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface $renderingContext)
46 {
47 parent::setRenderingContext($renderingContext);
48 if ($renderingContext instanceof \TYPO3\CMS\Fluid\Core\Rendering\RenderingContext) {
49 $this->controllerContext = $renderingContext->getControllerContext();
50 }
51 }
52
53 /**
54 * @param \TYPO3\CMS\Extbase\Object\ObjectManagerInterface $objectManager
55 */
56 public function injectObjectManager(\TYPO3\CMS\Extbase\Object\ObjectManagerInterface $objectManager)
57 {
58 $this->objectManager = $objectManager;
59 }
60
61 /**
62 * Call the render() method and handle errors.
63 *
64 * @return string the rendered ViewHelper
65 * @throws Exception
66 */
67 protected function callRenderMethod()
68 {
69 $renderMethodParameters = [];
70 if ($this->hasRenderMethodArguments()) {
71 foreach ($this->argumentDefinitions as $argumentName => $argumentDefinition) {
72 if ($argumentDefinition instanceof \TYPO3\CMS\Fluid\Core\ViewHelper\ArgumentDefinition && $argumentDefinition->isMethodParameter()) {
73 $renderMethodParameters[$argumentName] = $this->arguments[$argumentName];
74 }
75 }
76 }
77
78 try {
79 return call_user_func_array([$this, 'render'], $renderMethodParameters);
80 } catch (Exception $exception) {
81 if (GeneralUtility::getApplicationContext()->isProduction()) {
82 $this->getLogger()->error('A Fluid ViewHelper Exception was captured: ' . $exception->getMessage() . ' (' . $exception->getCode() . ')', ['exception' => $exception]);
83 return '';
84 }
85 throw $exception;
86 }
87 }
88
89 /**
90 * @return LoggerInterface
91 */
92 protected function getLogger()
93 {
94 return GeneralUtility::makeInstance(LogManager::class)->getLogger(__CLASS__);
95 }
96
97 /**
98 * @deprecated since TYPO3 v9, will be removed in TYPO3 v10; intentionally not deprecation logged (logged once above)
99 * @return bool
100 */
101 protected function hasRenderMethodArguments()
102 {
103 return (new \ReflectionMethod($this, 'render'))->getNumberOfParameters() > 0;
104 }
105
106 /**
107 * Register method arguments for "render" by analysing the doc comment above.
108 *
109 * @deprecated since TYPO3 v9, will be removed in TYPO3 v10; logged from this location but not elsewhere in class.
110 * @throws \TYPO3Fluid\Fluid\Core\Parser\Exception
111 */
112 protected function registerRenderMethodArguments()
113 {
114 GeneralUtility::deprecationLog(sprintf('Render method argument support is deprecated (used on class "%s"), switch to initializeArguments and registerArgument.', get_class($this)));
115
116 $reflectionService = $this->getReflectionService();
117 $methodParameters = $reflectionService->getMethodParameters(get_class($this), 'render');
118 $methodTags = $reflectionService->getMethodTagsValues(get_class($this), 'render');
119
120 $paramAnnotations = [];
121 if (isset($methodTags['param'])) {
122 $paramAnnotations = $methodTags['param'];
123 }
124
125 $i = 0;
126 foreach ($methodParameters as $parameterName => $parameterInfo) {
127 $dataType = null;
128 if (isset($parameterInfo['type'])) {
129 $dataType = isset($parameterInfo['array']) && (bool)$parameterInfo['array'] ? 'array' : $parameterInfo['type'];
130 } else {
131 throw new \TYPO3\CMS\Fluid\Core\Exception('Could not determine type of argument "' . $parameterName . '" of the render-method in ViewHelper "' . get_class($this) . '". Either the methods docComment is invalid or some PHP optimizer strips off comments.', 1242292003);
132 }
133
134 $description = '';
135 if (isset($paramAnnotations[$i])) {
136 $explodedAnnotation = explode(' ', $paramAnnotations[$i]);
137 array_shift($explodedAnnotation);
138 array_shift($explodedAnnotation);
139 $description = implode(' ', $explodedAnnotation);
140 }
141 $defaultValue = null;
142 if (isset($parameterInfo['defaultValue'])) {
143 $defaultValue = $parameterInfo['defaultValue'];
144 }
145 $this->argumentDefinitions[$parameterName] = new ArgumentDefinition($parameterName, $dataType, $description, ($parameterInfo['optional'] === false), $defaultValue, true);
146 $i++;
147 }
148 }
149
150 /**
151 * @deprecated since TYPO3 v9, will be removed in TYPO3 v10; intentionally not deprecation logged (logged once above)
152 * @return ReflectionService
153 */
154 protected function getReflectionService()
155 {
156 return $this->objectManager->get(ReflectionService::class);
157 }
158
159 /**
160 * @return \TYPO3Fluid\Fluid\Core\ViewHelper\ArgumentDefinition[]
161 * @throws \TYPO3Fluid\Fluid\Core\Parser\Exception
162 */
163 public function prepareArguments()
164 {
165 if ($this->hasRenderMethodArguments() && method_exists($this, 'registerRenderMethodArguments')) {
166 $this->registerRenderMethodArguments();
167 }
168 return parent::prepareArguments();
169 }
170 }