TYPO3CMS  8
 All Classes Namespaces Files Functions Variables Pages
AbstractViewHelper.php
Go to the documentation of this file.
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;
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 
52  public function setRenderingContext(\TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface $renderingContext)
53  {
54  parent::setRenderingContext($renderingContext);
55  if ($renderingContext instanceof \TYPO3\CMS\Fluid\Core\Rendering\RenderingContext) {
56  $this->controllerContext = $renderingContext->getControllerContext();
57  }
58  }
59 
63  public function injectObjectManager(\TYPO3\CMS\Extbase\Object\ObjectManagerInterface $objectManager)
64  {
65  $this->objectManager = $objectManager;
66  }
67 
73  public function injectReflectionService(\TYPO3\CMS\Extbase\Reflection\ReflectionService $reflectionService)
74  {
75  $this->reflectionService = $reflectionService;
76  }
77 
84  protected function callRenderMethod()
85  {
86  $renderMethodParameters = [];
87  foreach ($this->argumentDefinitions as $argumentName => $argumentDefinition) {
88  if ($argumentDefinition instanceof \TYPO3\CMS\Fluid\Core\ViewHelper\ArgumentDefinition && $argumentDefinition->isMethodParameter()) {
89  $renderMethodParameters[$argumentName] = $this->arguments[$argumentName];
90  }
91  }
92 
93  try {
94  return call_user_func_array([$this, 'render'], $renderMethodParameters);
95  } catch (Exception $exception) {
96  if (GeneralUtility::getApplicationContext()->isProduction()) {
97  $this->getLogger()->error('A Fluid ViewHelper Exception was captured: ' . $exception->getMessage() . ' (' . $exception->getCode() . ')', ['exception' => $exception]);
98  return '';
99  } else {
100  throw $exception;
101  }
102  }
103  }
104 
108  protected function getLogger()
109  {
110  return GeneralUtility::makeInstance(LogManager::class)->getLogger(__CLASS__);
111  }
112 
119  protected function registerRenderMethodArguments()
120  {
121  $methodParameters = $this->reflectionService->getMethodParameters(get_class($this), 'render');
122  if (count($methodParameters) === 0) {
123  return;
124  }
125 
126  $methodTags = $this->reflectionService->getMethodTagsValues(get_class($this), 'render');
127 
128  $paramAnnotations = [];
129  if (isset($methodTags['param'])) {
130  $paramAnnotations = $methodTags['param'];
131  }
132 
133  $i = 0;
134  foreach ($methodParameters as $parameterName => $parameterInfo) {
135  $dataType = null;
136  if (isset($parameterInfo['type'])) {
137  $dataType = isset($parameterInfo['array']) && (bool)$parameterInfo['array'] ? 'array' : $parameterInfo['type'];
138  } else {
139  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);
140  }
141 
142  $description = '';
143  if (isset($paramAnnotations[$i])) {
144  $explodedAnnotation = explode(' ', $paramAnnotations[$i]);
145  array_shift($explodedAnnotation);
146  array_shift($explodedAnnotation);
147  $description = implode(' ', $explodedAnnotation);
148  }
149  $defaultValue = null;
150  if (isset($parameterInfo['defaultValue'])) {
151  $defaultValue = $parameterInfo['defaultValue'];
152  }
153  $this->argumentDefinitions[$parameterName] = new ArgumentDefinition($parameterName, $dataType, $description, ($parameterInfo['optional'] === false), $defaultValue, true);
154  $i++;
155  }
156  }
157 
162  public function prepareArguments()
163  {
164  if (method_exists($this, 'registerRenderMethodArguments')) {
166  }
167  return parent::prepareArguments();
168  }
169 }
setRenderingContext(\TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface $renderingContext)
injectObjectManager(\TYPO3\CMS\Extbase\Object\ObjectManagerInterface $objectManager)
injectReflectionService(\TYPO3\CMS\Extbase\Reflection\ReflectionService $reflectionService)
static makeInstance($className,...$constructorArguments)