|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file belongs to the package "TYPO3 Fluid". |
| 5 | + * See LICENSE.txt that was shipped with this package. |
| 6 | + */ |
| 7 | + |
| 8 | +namespace TYPO3Fluid\Fluid\ViewHelpers; |
| 9 | + |
| 10 | +use TYPO3Fluid\Fluid\Core\Compiler\TemplateCompiler; |
| 11 | +use TYPO3Fluid\Fluid\Core\Parser\ParsingState; |
| 12 | +use TYPO3Fluid\Fluid\Core\Parser\SyntaxTree\NodeInterface; |
| 13 | +use TYPO3Fluid\Fluid\Core\Parser\SyntaxTree\ViewHelperNode; |
| 14 | +use TYPO3Fluid\Fluid\Core\Rendering\RenderingContext; |
| 15 | +use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper; |
| 16 | +use TYPO3Fluid\Fluid\Core\ViewHelper\ArgumentDefinition; |
| 17 | + |
| 18 | +/** |
| 19 | + * ``f:argument`` allows to define requirements and type constraints to variables that |
| 20 | + * are provided to templates and partials. This can be very helpful to document how |
| 21 | + * a template or partial is supposed to be used and which input variables are required. |
| 22 | + * |
| 23 | + * These requirements are enforced during rendering of the template or partial: |
| 24 | + * If an argument is defined with this ViewHelper which isn't marked as ``optional``, |
| 25 | + * an exception will be thrown if that variable isn't present during rendering. |
| 26 | + * If a variable doesn't match the specified type and can't be converted automatically, |
| 27 | + * an exception will be thrown as well. |
| 28 | + * |
| 29 | + * Note that ``f:argument`` ViewHelpers must be used at the root level of the |
| 30 | + * template, and can't be nested into other ViewHelpers. Also, the usage of variables |
| 31 | + * in any of its arguments is not possible (e. g. you can't define an argument name |
| 32 | + * by using a variable). |
| 33 | + * |
| 34 | + * Example |
| 35 | + * ======== |
| 36 | + * |
| 37 | + * For the following partial: |
| 38 | + * |
| 39 | + * .. code-block:: xml |
| 40 | + * |
| 41 | + * <f:argument name="title" type="string" /> |
| 42 | + * <f:argument name="tags" type="string[]" optional="{true}" /> |
| 43 | + * <f:argument name="user" type="string" optional="{true}" default="admin" /> |
| 44 | + * |
| 45 | + * Title: {title}<br /> |
| 46 | + * <f:if condition="{tags}"> |
| 47 | + * Tags: {tags -> f:join(separator: ', ')}<br /> |
| 48 | + * </f:if> |
| 49 | + * User: {user} |
| 50 | + * |
| 51 | + * The following render calls will be successful: |
| 52 | + * |
| 53 | + * .. code-block:: xml |
| 54 | + * |
| 55 | + * <!-- All arguments supplied --> |
| 56 | + * <f:render partial="MyPartial" arguments="{title: 'My title', tags: {0: 'tag1', 1: 'tag2'}, user: 'me'}" /> |
| 57 | + * <!-- "user" will fall back to default value --> |
| 58 | + * <f:render partial="MyPartial" arguments="{title: 'My title', tags: {0: 'tag1', 1: 'tag2'}}" /> |
| 59 | + * <!-- "tags" will be "null", "user" will fall back to default value --> |
| 60 | + * <f:render partial="MyPartial" arguments="{title: 'My title'}" /> |
| 61 | + * |
| 62 | + * The following render calls will result in an exception: |
| 63 | + * |
| 64 | + * .. code-block:: xml |
| 65 | + * |
| 66 | + * <!-- required "title" has not been supplied --> |
| 67 | + * <f:render partial="MyPartial" /> |
| 68 | + * <!-- "user" has been supplied as array, not as string --> |
| 69 | + * <f:render partial="MyPartial" arguments="{title: 'My title', user: {firstName: 'Jane', lastName: 'Doe'}}" /> |
| 70 | + * |
| 71 | + * @api |
| 72 | + */ |
| 73 | +final class ArgumentViewHelper extends AbstractViewHelper |
| 74 | +{ |
| 75 | + /** |
| 76 | + * No need to add escaping nodes since the ViewHelper doesn't output anything |
| 77 | + */ |
| 78 | + protected $escapeOutput = false; |
| 79 | + |
| 80 | + public function initializeArguments(): void |
| 81 | + { |
| 82 | + $this->registerArgument('name', 'string', 'name of the template argument', true); |
| 83 | + $this->registerArgument('type', 'string', 'type of the template argument', true); |
| 84 | + $this->registerArgument('description', 'string', 'description of the template argument'); |
| 85 | + $this->registerArgument('optional', 'boolean', 'true if the defined argument should be optional', false, false); |
| 86 | + $this->registerArgument('default', 'mixed', 'default value for optional argument'); |
| 87 | + } |
| 88 | + |
| 89 | + public function render(): string |
| 90 | + { |
| 91 | + return ''; |
| 92 | + } |
| 93 | + |
| 94 | + public function compile($argumentsName, $closureName, &$initializationPhpCode, ViewHelperNode $node, TemplateCompiler $compiler): string |
| 95 | + { |
| 96 | + return '\'\''; |
| 97 | + } |
| 98 | + |
| 99 | + public static function nodeInitializedEvent(ViewHelperNode $node, array $arguments, ParsingState $parsingState): void |
| 100 | + { |
| 101 | + // Create static values of supplied arguments. A new empty rendering context is used here |
| 102 | + // because argument definitions shouldn't be dependent on any variables in the template. |
| 103 | + // Any variables that are used anyway (e. g. in default values) will be interpreted as "null" |
| 104 | + $emptyRenderingContext = new RenderingContext(); |
| 105 | + $evaluatedArguments = array_map( |
| 106 | + static fn(NodeInterface $node): mixed => $node->evaluate($emptyRenderingContext), |
| 107 | + $arguments, |
| 108 | + ); |
| 109 | + $argumentName = (string)$evaluatedArguments['name']; |
| 110 | + |
| 111 | + // Make sure that arguments are not nested into other ViewHelpers as this might create confusion |
| 112 | + if ($parsingState->hasNodeTypeInStack(ViewHelperNode::class)) { |
| 113 | + throw new \TYPO3Fluid\Fluid\Core\Parser\Exception(sprintf( |
| 114 | + 'Template argument "%s" needs to be defined at the root level of the template, not within a ViewHelper.', |
| 115 | + $argumentName, |
| 116 | + ), 1744908510); |
| 117 | + } |
| 118 | + |
| 119 | + // Make sure that this argument hasn't already been defined in the template |
| 120 | + $argumentDefinitions = $parsingState->getArgumentDefinitions(); |
| 121 | + if (isset($argumentDefinitions[$argumentName])) { |
| 122 | + throw new \TYPO3Fluid\Fluid\Core\Parser\Exception(sprintf( |
| 123 | + 'Template argument "%s" has been defined multiple times.', |
| 124 | + $argumentName, |
| 125 | + ), 1744908509); |
| 126 | + } |
| 127 | + |
| 128 | + // Create argument definition to be interpreted later during rendering |
| 129 | + // This will also be written to the cache by the TemplateCompiler |
| 130 | + $argumentDefinitions[$argumentName] = new ArgumentDefinition( |
| 131 | + $argumentName, |
| 132 | + (string)$evaluatedArguments['type'], |
| 133 | + array_key_exists('description', $evaluatedArguments) ? (string)$evaluatedArguments['description'] : '', |
| 134 | + array_key_exists('optional', $evaluatedArguments) ? !$evaluatedArguments['optional'] : false, |
| 135 | + array_key_exists('default', $evaluatedArguments) ? $evaluatedArguments['default'] : null, |
| 136 | + ); |
| 137 | + $parsingState->setArgumentDefinitions($argumentDefinitions); |
| 138 | + } |
| 139 | +} |
0 commit comments