1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
namespace TYPO3Fluid\Fluid\Core\ViewHelper; |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file belongs to the package "TYPO3 Fluid". |
7
|
|
|
* See LICENSE.txt that was shipped with this package. |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
use Closure; |
11
|
|
|
use TYPO3Fluid\Fluid\Component\AbstractComponent; |
12
|
|
|
use TYPO3Fluid\Fluid\Component\Argument\ArgumentCollection; |
13
|
|
|
use TYPO3Fluid\Fluid\Component\Argument\ArgumentDefinition; |
14
|
|
|
use TYPO3Fluid\Fluid\Component\ComponentInterface; |
15
|
|
|
use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* The abstract base class for all view helpers. |
19
|
|
|
*/ |
20
|
|
|
abstract class AbstractViewHelper extends AbstractComponent |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var RenderingContextInterface |
24
|
|
|
*/ |
25
|
|
|
protected $renderingContext; |
26
|
|
|
|
27
|
|
|
protected $escapeOutput = true; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Execute via Component API implementation. |
31
|
|
|
* |
32
|
|
|
* @param RenderingContextInterface $renderingContext |
33
|
|
|
* @return mixed |
34
|
|
|
*/ |
35
|
|
|
public function evaluate(RenderingContextInterface $renderingContext) |
36
|
|
|
{ |
37
|
|
|
$this->renderingContext = $renderingContext; |
38
|
|
|
$this->getArguments()->setRenderingContext($renderingContext); |
39
|
|
|
return $this->callRenderMethod(); |
|
|
|
|
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function onOpen(RenderingContextInterface $renderingContext): ComponentInterface |
43
|
|
|
{ |
44
|
|
|
$this->getArguments()->setRenderingContext($renderingContext); |
45
|
|
|
return parent::onOpen($renderingContext); |
|
|
|
|
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function getArguments(): ArgumentCollection |
49
|
|
|
{ |
50
|
|
|
if ($this->arguments === null) { |
51
|
|
|
$this->arguments = new ArgumentCollection(); |
52
|
|
|
$this->initializeArguments(); |
53
|
|
|
} |
54
|
|
|
return $this->arguments; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Register a new argument. Call this method from your ViewHelper subclass |
59
|
|
|
* inside the initializeArguments() method. |
60
|
|
|
* |
61
|
|
|
* @param string $name Name of the argument |
62
|
|
|
* @param string $type Type of the argument |
63
|
|
|
* @param string $description Description of the argument |
64
|
|
|
* @param boolean $required If TRUE, argument is required. Defaults to FALSE. |
65
|
|
|
* @param mixed $defaultValue Default value of argument |
66
|
|
|
* @return AbstractViewHelper $this, to allow chaining. |
67
|
|
|
* @throws Exception |
68
|
|
|
*/ |
69
|
|
|
protected function registerArgument(string $name, string $type, string $description, bool $required = false, $defaultValue = null): self |
70
|
|
|
{ |
71
|
|
|
$this->getArguments()->addDefinition(new ArgumentDefinition($name, $type, $description, $required, $defaultValue)); |
72
|
|
|
return $this; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Overrides a registered argument. Call this method from your ViewHelper subclass |
77
|
|
|
* inside the initializeArguments() method if you want to override a previously registered argument. |
78
|
|
|
* |
79
|
|
|
* @param string $name Name of the argument |
80
|
|
|
* @param string $type Type of the argument |
81
|
|
|
* @param string $description Description of the argument |
82
|
|
|
* @param boolean $required If TRUE, argument is required. Defaults to FALSE. |
83
|
|
|
* @param mixed $defaultValue Default value of argument |
84
|
|
|
* @return AbstractViewHelper $this, to allow chaining. |
85
|
|
|
* @throws Exception |
86
|
|
|
* @see registerArgument() |
87
|
|
|
* @deprecated Will be removed in Fluid 4.0 |
88
|
|
|
*/ |
89
|
|
|
protected function overrideArgument(string $name, string $type, string $description, bool $required = false, $defaultValue = null): self |
90
|
|
|
{ |
91
|
|
|
$this->getArguments()->addDefinition(new ArgumentDefinition($name, $type, $description, $required, $defaultValue)); |
92
|
|
|
return $this; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Call the render() method and handle errors. |
97
|
|
|
* |
98
|
|
|
* @return mixed the rendered ViewHelper |
99
|
|
|
* @throws Exception |
100
|
|
|
* @deprecated Will be removed and no longer called in Fluid 4.0 |
101
|
|
|
*/ |
102
|
|
|
protected function callRenderMethod() |
103
|
|
|
{ |
104
|
|
|
if (method_exists($this, 'render')) { |
105
|
|
|
return call_user_func([$this, 'render']); |
106
|
|
|
} |
107
|
|
|
if (method_exists($this, 'renderStatic')) { |
108
|
|
|
// Method is safe to call - will not recurse through ViewHelperInvoker via the default |
109
|
|
|
// implementation of renderStatic() on this class. |
110
|
|
|
return call_user_func_array([static::class, 'renderStatic'], [$this->arguments->getArrayCopy(), $this->buildRenderChildrenClosure(), $this->arguments->getRenderingContext()]); |
|
|
|
|
111
|
|
|
} |
112
|
|
|
return $this->renderChildren(); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Helper method which triggers the rendering of everything between the |
117
|
|
|
* opening and the closing tag. |
118
|
|
|
* |
119
|
|
|
* @return mixed The finally rendered child nodes. |
120
|
|
|
*/ |
121
|
|
|
protected function renderChildren() |
122
|
|
|
{ |
123
|
|
|
return $this->evaluateChildren($this->renderingContext); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Helper which is mostly needed when calling renderStatic() from within |
128
|
|
|
* render(). |
129
|
|
|
* |
130
|
|
|
* No public API yet. |
131
|
|
|
* |
132
|
|
|
* @deprecated Will be removed and not called in Fluid 4.0 |
133
|
|
|
* @return Closure |
134
|
|
|
*/ |
135
|
|
|
protected function buildRenderChildrenClosure() |
136
|
|
|
{ |
137
|
|
|
$self = clone $this; |
138
|
|
|
$renderChildrenClosure = function () use ($self) { |
139
|
|
|
return $self->renderChildren(); |
140
|
|
|
}; |
141
|
|
|
return $renderChildrenClosure; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Initialize all arguments. You need to override this method and call |
146
|
|
|
* $this->registerArgument(...) inside this method, to register all your arguments. |
147
|
|
|
* |
148
|
|
|
* @return void |
149
|
|
|
*/ |
150
|
|
|
protected function initializeArguments() |
151
|
|
|
{ |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
public function allowUndeclaredArgument(string $argumentName): bool |
155
|
|
|
{ |
156
|
|
|
return false; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
protected function hasArgument(string $argumentName): bool |
160
|
|
|
{ |
161
|
|
|
return $this->getArguments()->getRaw($argumentName) !== null; |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
|
This method has been deprecated. The supplier of the class has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.