TYPO3 CMS  TYPO3_7-6
CompileWithContentArgumentAndRenderStatic.php
Go to the documentation of this file.
1 <?php
3 
4 /*
5  * This file is part of the TYPO3 CMS project.
6  *
7  * It is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU General Public License, either version 2
9  * of the License, or any later version.
10  *
11  * For the full copyright and license information, please read the
12  * LICENSE.txt file that was distributed with this source code.
13  *
14  * The TYPO3 project - inspiring people to share!
15  */
16 
20 
32 {
33 
49  protected $contentArgumentName;
50 
54  abstract protected function buildRenderChildrenClosure();
55 
59  abstract public function prepareArguments();
60 
68  public function render()
69  {
70  $argumentName = $this->resolveContentArgumentName();
71  $arguments = $this->arguments;
72  if (!empty($argumentName) && isset($arguments[$argumentName])) {
73  $renderChildrenClosure = function () use ($arguments, $argumentName) {
74  return $arguments[$argumentName];
75  };
76  } else {
77  $renderChildrenClosure = $this->buildRenderChildrenClosure();
78  }
79  return static::renderStatic(
80  $arguments,
81  $renderChildrenClosure,
82  $this->renderingContext
83  );
84  }
85 
94  public function compile(
95  $argumentsName,
96  $closureName,
97  &$initializationPhpCode,
98  AbstractNode $node,
99  TemplateCompiler $compiler
100  ) {
101  $contentArgumentName = $this->resolveContentArgumentName();
102  return sprintf(
103  '%s::renderStatic(%s, isset(%s[\'%s\']) ? function() use (%s) { return %s[\'%s\']; } : %s, $renderingContext)',
104  static::class,
105  $argumentsName,
106  $argumentsName,
107  $contentArgumentName,
108  $argumentsName,
109  $argumentsName,
110  $contentArgumentName,
111  $closureName
112  );
113  }
114 
119  protected function resolveContentArgumentName()
120  {
121  if (empty($this->contentArgumentName)) {
122  foreach ($this->prepareArguments() as $registeredArgument) {
123  if (!$registeredArgument->isRequired()) {
124  return $registeredArgument->getName();
125  }
126  }
127  throw new Exception(
128  'Attempting to compile %s failed. Chosen compile method requires that ViewHelper has ' .
129  'at least one registered and optional argument'
130  );
131  }
132  return $this->contentArgumentName;
133  }
134 }
compile( $argumentsName, $closureName, &$initializationPhpCode, AbstractNode $node, TemplateCompiler $compiler)