1
|
|
|
<?php |
2
|
|
|
namespace TYPO3Fluid\Fluid\Core\ViewHelper\Traits; |
3
|
|
|
|
4
|
|
|
use TYPO3Fluid\Fluid\Core\Compiler\TemplateCompiler; |
5
|
|
|
use TYPO3Fluid\Fluid\Core\Compiler\ViewHelperCompiler; |
6
|
|
|
use TYPO3Fluid\Fluid\Core\Exception; |
7
|
|
|
use TYPO3Fluid\Fluid\Core\Parser\SyntaxTree\ViewHelperNode; |
8
|
|
|
use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class CompilableWithContentArgumentAndRenderStatic |
12
|
|
|
* |
13
|
|
|
* Provides default methods for rendering and compiling |
14
|
|
|
* any ViewHelper that conforms to the `renderStatic` |
15
|
|
|
* method pattern but has the added common use case that |
16
|
|
|
* an argument value must be checked and used instead of |
17
|
|
|
* the normal render children closure, if that named |
18
|
|
|
* argument is specified and not empty. |
19
|
|
|
*/ |
20
|
|
|
trait CompileWithContentArgumentAndRenderStatic |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* Name of variable that contains the value to use |
24
|
|
|
* instead of render children closure, if specified. |
25
|
|
|
* If no name is provided here, the first variable |
26
|
|
|
* registered in `initializeArguments` of the ViewHelper |
27
|
|
|
* will be used. |
28
|
|
|
* |
29
|
|
|
* Note: it is significantly better practice to define |
30
|
|
|
* this property in your ViewHelper class and so fix it |
31
|
|
|
* to one particular argument instead of resolving, |
32
|
|
|
* especially when your ViewHelper is called multiple |
33
|
|
|
* times within an uncompiled template! |
34
|
|
|
* |
35
|
|
|
* @var string |
36
|
|
|
*/ |
37
|
|
|
protected $contentArgumentName; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param string $argumentsName |
41
|
|
|
* @param string $closureName |
42
|
|
|
* @param string $initializationPhpCode |
43
|
|
|
* @param ViewHelperNode $node |
44
|
|
|
* @param TemplateCompiler $compiler |
45
|
|
|
* @return string |
46
|
|
|
*/ |
47
|
|
View Code Duplication |
public function compile( |
|
|
|
|
48
|
|
|
$argumentsName, |
49
|
|
|
$closureName, |
50
|
|
|
&$initializationPhpCode, |
51
|
|
|
ViewHelperNode $node, |
|
|
|
|
52
|
|
|
TemplateCompiler $compiler |
|
|
|
|
53
|
|
|
) { |
54
|
|
|
list ($initialization, $execution) = ViewHelperCompiler::getInstance()->compileWithCallToStaticMethod( |
55
|
|
|
$this, |
56
|
|
|
$argumentsName, |
57
|
|
|
$closureName, |
58
|
|
|
ViewHelperCompiler::RENDER_STATIC, |
59
|
|
|
static::class |
60
|
|
|
); |
61
|
|
|
$initializationPhpCode .= $initialization; |
62
|
|
|
return $execution; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Helper which is mostly needed when calling renderStatic() from within |
67
|
|
|
* render(). |
68
|
|
|
* |
69
|
|
|
* No public API yet. |
70
|
|
|
* |
71
|
|
|
* @return \Closure |
72
|
|
|
*/ |
73
|
|
|
protected function buildRenderChildrenClosure() |
74
|
|
|
{ |
75
|
|
|
$argumentName = $this->resolveContentArgumentName(); |
76
|
|
|
$arguments = $this->arguments; |
|
|
|
|
77
|
|
|
if (!empty($argumentName) && isset($arguments[$argumentName])) { |
78
|
|
|
$renderChildrenClosure = function () use ($arguments, $argumentName) { |
79
|
|
|
return $arguments[$argumentName]; |
80
|
|
|
}; |
81
|
|
|
} else { |
82
|
|
|
$self = clone $this; |
83
|
|
|
$renderChildrenClosure = function () use ($self) { |
84
|
|
|
return $self->renderChildren(); |
|
|
|
|
85
|
|
|
}; |
86
|
|
|
} |
87
|
|
|
return $renderChildrenClosure; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @return string |
92
|
|
|
*/ |
93
|
|
|
protected function resolveContentArgumentName() |
94
|
|
|
{ |
95
|
|
|
if (empty($this->contentArgumentName)) { |
96
|
|
|
$registeredArguments = call_user_func_array([$this, 'prepareArguments'], []); |
97
|
|
|
foreach ($registeredArguments as $registeredArgument) { |
98
|
|
|
if (!$registeredArgument->isRequired()) { |
99
|
|
|
$this->contentArgumentName = $registeredArgument->getName(); |
100
|
|
|
return $this->contentArgumentName; |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
throw new Exception( |
104
|
|
|
'Attempting to compile %s failed. Chosen compile method requires that ViewHelper has ' . |
105
|
|
|
'at least one registered and optional argument' |
106
|
|
|
); |
107
|
|
|
} |
108
|
|
|
return $this->contentArgumentName; |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.