TYPO3 CMS  TYPO3_8-7
AbstractViewHelper.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 
20 
26 abstract class AbstractViewHelper extends \TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper implements \TYPO3Fluid\Fluid\Core\ViewHelper\ViewHelperInterface
27 {
34  protected $controllerContext;
35 
42 
46  protected $objectManager;
47 
51  public function setRenderingContext(\TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface $renderingContext)
52  {
53  parent::setRenderingContext($renderingContext);
54  if ($renderingContext instanceof \TYPO3\CMS\Fluid\Core\Rendering\RenderingContext) {
55  $this->controllerContext = $renderingContext->getControllerContext();
56  }
57  }
58 
62  public function injectObjectManager(\TYPO3\CMS\Extbase\Object\ObjectManagerInterface $objectManager)
63  {
64  $this->objectManager = $objectManager;
65  }
66 
72  public function injectReflectionService(\TYPO3\CMS\Extbase\Reflection\ReflectionService $reflectionService)
73  {
74  $this->reflectionService = $reflectionService;
75  }
76 
83  protected function callRenderMethod()
84  {
85  $renderMethodParameters = [];
86  foreach ($this->argumentDefinitions as $argumentName => $argumentDefinition) {
87  if ($argumentDefinition instanceof \TYPO3\CMS\Fluid\Core\ViewHelper\ArgumentDefinition && $argumentDefinition->isMethodParameter()) {
88  $renderMethodParameters[$argumentName] = $this->arguments[$argumentName];
89  }
90  }
91 
92  try {
93  return call_user_func_array([$this, 'render'], $renderMethodParameters);
94  } catch (Exception $exception) {
95  if (GeneralUtility::getApplicationContext()->isProduction()) {
96  $this->getLogger()->error('A Fluid ViewHelper Exception was captured: ' . $exception->getMessage() . ' (' . $exception->getCode() . ')', ['exception' => $exception]);
97  return '';
98  }
99  throw $exception;
100  }
101  }
102 
106  protected function getLogger()
107  {
108  return GeneralUtility::makeInstance(LogManager::class)->getLogger(__CLASS__);
109  }
110 
116  protected function registerRenderMethodArguments()
117  {
118  $methodParameters = $this->reflectionService->getMethodParameters(get_class($this), 'render');
119  if (count($methodParameters) === 0) {
120  return;
121  }
122 
123  $methodTags = $this->reflectionService->getMethodTagsValues(get_class($this), 'render');
124 
125  $paramAnnotations = [];
126  if (isset($methodTags['param'])) {
127  $paramAnnotations = $methodTags['param'];
128  }
129 
130  $i = 0;
131  foreach ($methodParameters as $parameterName => $parameterInfo) {
132  $dataType = null;
133  if (isset($parameterInfo['type'])) {
134  $dataType = isset($parameterInfo['array']) && (bool)$parameterInfo['array'] ? 'array' : $parameterInfo['type'];
135  } else {
136  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);
137  }
138 
139  $description = '';
140  if (isset($paramAnnotations[$i])) {
141  $explodedAnnotation = explode(' ', $paramAnnotations[$i]);
142  array_shift($explodedAnnotation);
143  array_shift($explodedAnnotation);
144  $description = implode(' ', $explodedAnnotation);
145  }
146  $defaultValue = null;
147  if (isset($parameterInfo['defaultValue'])) {
148  $defaultValue = $parameterInfo['defaultValue'];
149  }
150  $this->argumentDefinitions[$parameterName] = new ArgumentDefinition($parameterName, $dataType, $description, ($parameterInfo['optional'] === false), $defaultValue, true);
151  $i++;
152  }
153  }
154 
159  public function prepareArguments()
160  {
161  if (method_exists($this, 'registerRenderMethodArguments')) {
163  }
164  return parent::prepareArguments();
165  }
166 }
setRenderingContext(\TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface $renderingContext)
injectReflectionService(\TYPO3\CMS\Extbase\Reflection\ReflectionService $reflectionService)
static makeInstance($className,... $constructorArguments)
injectObjectManager(\TYPO3\CMS\Extbase\Object\ObjectManagerInterface $objectManager)