-
-
Notifications
You must be signed in to change notification settings - Fork 186
/
AbstractViewHelper.php
263 lines (237 loc) · 9 KB
/
AbstractViewHelper.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
<?php
namespace Neos\FluidAdaptor\Core\ViewHelper;
/*
* This file is part of the Neos.FluidAdaptor package.
*
* (c) Contributors of the Neos Project - www.neos.io
*
* This package is Open Source Software. For the full copyright and license
* information, please view the LICENSE file which was distributed with this
* source code.
*/
use Neos\FluidAdaptor\Core\Rendering\FlowAwareRenderingContextInterface;
use Neos\Flow\Annotations as Flow;
use Neos\Flow\Log\SystemLoggerInterface;
use Neos\Flow\Mvc\Controller\ControllerContext;
use Neos\Flow\ObjectManagement\ObjectManagerInterface;
use Neos\Flow\Reflection\ReflectionService;
use Psr\Log\LoggerInterface;
use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper as FluidAbstractViewHelper;
/**
* The abstract base class for all view helpers.
*
* @api
*/
abstract class AbstractViewHelper extends FluidAbstractViewHelper
{
/**
* Controller Context to use
*
* @var ControllerContext
* @api
*/
protected $controllerContext;
/**
* @var ObjectManagerInterface
*/
protected $objectManager;
/**
* @var SystemLoggerInterface
* @deprecated
* @see logger
*/
protected $systemLogger;
/**
* @var LoggerInterface
*/
protected $logger;
/**
* @param RenderingContextInterface $renderingContext
* @return void
*/
public function setRenderingContext(RenderingContextInterface $renderingContext)
{
$this->renderingContext = $renderingContext;
$this->templateVariableContainer = $renderingContext->getVariableProvider();
$this->viewHelperVariableContainer = $renderingContext->getViewHelperVariableContainer();
if ($renderingContext instanceof FlowAwareRenderingContextInterface) {
$this->controllerContext = $renderingContext->getControllerContext();
}
}
/**
* @param ObjectManagerInterface $objectManager
*/
public function injectObjectManager(ObjectManagerInterface $objectManager)
{
$this->objectManager = $objectManager;
}
/**
* @param SystemLoggerInterface $systemLogger
* @return void
*/
public function injectSystemLogger(SystemLoggerInterface $systemLogger)
{
$this->systemLogger = $systemLogger;
}
/**
* Injects the (system) logger based on PSR-3.
*
* @param LoggerInterface $logger
* @return void
*/
public function injectLogger(LoggerInterface $logger)
{
$this->logger = $logger;
}
/**
* @return boolean
*/
public function isEscapingInterceptorEnabled()
{
return $this->isChildrenEscapingEnabled();
}
/**
* Call the render() method and handle errors.
*
* @return string the rendered ViewHelper
* @throws Exception
*/
protected function callRenderMethod()
{
$renderMethodParameters = [];
foreach ($this->argumentDefinitions as $argumentName => $argumentDefinition) {
if ($argumentDefinition instanceof ArgumentDefinition && $argumentDefinition->isMethodParameter()) {
$renderMethodParameters[] = $this->arguments[$argumentName];
}
}
try {
return $this->render(...$renderMethodParameters);
} catch (Exception $exception) {
if (!$this->objectManager->getContext()->isProduction()) {
throw $exception;
}
$this->logger->error(
'A Fluid ViewHelper Exception was captured: ' . $exception->getMessage() . ' (' . $exception->getCode() . ')',
['exception' => $exception]
);
return '';
}
}
/**
* @return \TYPO3Fluid\Fluid\Core\ViewHelper\ArgumentDefinition[]
* @throws \TYPO3Fluid\Fluid\Core\Parser\Exception
*/
public function prepareArguments()
{
if (method_exists($this, 'registerRenderMethodArguments')) {
$this->registerRenderMethodArguments();
}
return parent::prepareArguments();
}
/**
* Register a new argument. Call this method from your ViewHelper subclass
* inside the initializeArguments() method.
*
* @param string $name Name of the argument
* @param string $type Type of the argument
* @param string $description Description of the argument
* @param boolean $required If true, argument is required. Defaults to false.
* @param mixed $defaultValue Default value of argument
* @param boolean|null $escape Can be toggled to TRUE to force escaping of variables and inline syntax passed as argument value.
* @return FluidAbstractViewHelper $this, to allow chaining.
* @throws Exception
* @api
*/
protected function registerArgument($name, $type, $description, $required = false, $defaultValue = null, $escape = null)
{
if (array_key_exists($name, $this->argumentDefinitions)) {
throw new Exception('Argument "' . $name . '" has already been defined, thus it should not be defined again.', 1253036401);
}
return parent::registerArgument($name, $type, $description, $required, $defaultValue, $escape);
}
/**
* Overrides a registered argument. Call this method from your ViewHelper subclass
* inside the initializeArguments() method if you want to override a previously registered argument.
*
* @see registerArgument()
*
* @param string $name Name of the argument
* @param string $type Type of the argument
* @param string $description Description of the argument
* @param boolean $required If true, argument is required. Defaults to false.
* @param mixed $defaultValue Default value of argument
* @param boolean|null $escape Can be toggled to TRUE to force escaping of variables and inline syntax passed as argument value.
* @return FluidAbstractViewHelper $this, to allow chaining.
* @throws Exception
* @api
*/
protected function overrideArgument($name, $type, $description, $required = false, $defaultValue = null, $escape = null)
{
if (!array_key_exists($name, $this->argumentDefinitions)) {
throw new Exception('Argument "' . $name . '" has not been defined, thus it can\'t be overridden.', 1279212461);
}
return parent::overrideArgument($name, $type, $description, $required, $defaultValue, $escape);
}
/**
* Registers render method arguments
*
* @return void
* @deprecated Render method should no longer expect arguments, instead all arguments should be registered in "initializeArguments"
*/
protected function registerRenderMethodArguments()
{
foreach (static::getRenderMethodArgumentDefinitions($this->objectManager) as $argumentName => $definition) {
$this->argumentDefinitions[$argumentName] = new ArgumentDefinition($definition[0], $definition[1], $definition[2], $definition[3], $definition[4], $definition[5]);
}
}
/**
* @param ObjectManagerInterface $objectManager
* @return ArgumentDefinition[]
* @throws \Neos\FluidAdaptor\Core\Exception
* @Flow\CompileStatic
*/
public static function getRenderMethodArgumentDefinitions(ObjectManagerInterface $objectManager)
{
$methodArgumentDefinitions = [];
$reflectionService = $objectManager->get(ReflectionService::class);
$methodParameters = $reflectionService->getMethodParameters(static::class, 'render');
if (count($methodParameters) === 0) {
return $methodArgumentDefinitions;
}
$methodTags = $reflectionService->getMethodTagsValues(static::class, 'render');
$paramAnnotations = [];
if (isset($methodTags['param'])) {
$paramAnnotations = $methodTags['param'];
}
$i = 0;
foreach ($methodParameters as $parameterName => $parameterInfo) {
$dataType = null;
$dataType = 'mixed';
if (isset($parameterInfo['type']) && strpos($parameterInfo['type'], '|') === false) {
$dataType = isset($parameterInfo['array']) && (bool)$parameterInfo['array'] ? 'array' : $parameterInfo['type'];
}
$description = '';
if (isset($paramAnnotations[$i])) {
$explodedAnnotation = explode(' ', $paramAnnotations[$i]);
array_shift($explodedAnnotation);
array_shift($explodedAnnotation);
$description = implode(' ', $explodedAnnotation);
}
$defaultValue = null;
if (isset($parameterInfo['defaultValue'])) {
$defaultValue = $parameterInfo['defaultValue'];
}
$methodArgumentDefinitions[$parameterName] = [
$parameterName,
$dataType,
$description,
($parameterInfo['optional'] === false),
$defaultValue,
true
];
$i++;
}
return $methodArgumentDefinitions;
}
}