|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
namespace TYPO3Fluid\Fluid\ViewHelpers; |
|
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 TYPO3Fluid\Fluid\Component\ComponentInterface; |
|
11
|
|
|
use TYPO3Fluid\Fluid\Component\Error\ChildNotFoundException; |
|
12
|
|
|
use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface; |
|
13
|
|
|
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Renders an Atom. Usually not used directly; instead, Atoms should |
|
17
|
|
|
* be registered as a namespace and will masquerace as this ViewHelper. |
|
18
|
|
|
*/ |
|
19
|
|
|
class AtomViewHelper extends AbstractViewHelper |
|
20
|
|
|
{ |
|
21
|
|
|
protected $escapeOutput = false; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @return void |
|
25
|
|
|
*/ |
|
26
|
|
|
public function initializeArguments() |
|
27
|
|
|
{ |
|
28
|
|
|
parent::initializeArguments(); |
|
29
|
|
|
$this->registerArgument('atom', 'mixed', 'Atom name or instance', true); |
|
30
|
|
|
$this->registerArgument('file', 'mixed', 'Atom file name, overrides "atom" if both are provided'); |
|
31
|
|
|
$this->registerArgument('section', 'string', 'Optional name or dotted-name path to section to render from inside Atom'); |
|
32
|
|
|
$this->registerArgument('optional', 'boolean', 'If Atom is not found and optional is true, does not throw exception error', false, false); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
public function evaluate(RenderingContextInterface $renderingContext) |
|
36
|
|
|
{ |
|
37
|
|
|
$arguments = $this->getArguments()->setRenderingContext($renderingContext)->getArrayCopy(); |
|
38
|
|
|
$optional = (boolean) ($arguments['optional'] ?? false); |
|
39
|
|
|
$default = $arguments['default'] ?? null; |
|
40
|
|
|
|
|
41
|
|
|
try { |
|
42
|
|
|
if (isset($arguments['file'])) { |
|
43
|
|
|
$component = $renderingContext->getTemplateParser()->parseFile($arguments['file']); |
|
44
|
|
|
} elseif (isset($arguments['atom'])) { |
|
45
|
|
|
$component = $renderingContext->getViewHelperResolver()->resolveAtom(...explode(':', $arguments['atom'])); |
|
|
|
|
|
|
46
|
|
|
} else { |
|
47
|
|
|
return $this->evaluateChildren($renderingContext); |
|
48
|
|
|
} |
|
49
|
|
|
} catch (ChildNotFoundException $exception) { |
|
50
|
|
|
if ($optional) { |
|
51
|
|
|
return $default; |
|
52
|
|
|
} |
|
53
|
|
|
throw $exception; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
if (!empty($arguments['section'])) { |
|
57
|
|
|
$component = $component->getNamedChild($arguments['section']); |
|
58
|
|
|
} |
|
59
|
|
|
$component->getArguments()->assignAll($arguments); |
|
|
|
|
|
|
60
|
|
|
return $component->evaluate($renderingContext); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
public function allowUndeclaredArgument(string $argumentName): bool |
|
64
|
|
|
{ |
|
65
|
|
|
return true; |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
|
This check looks for function calls that miss required arguments.