예제 #1
0
 /**
  * Convert a single ViewHelperNode into its cached representation. If the ViewHelper implements the "Compilable" facet,
  * the ViewHelper itself is asked for its cached PHP code representation. If not, a ViewHelper is built and then invoked.
  *
  * @param ViewHelperNode $node
  * @return array
  * @see convert()
  */
 protected function convertViewHelperNode(ViewHelperNode $node)
 {
     $initializationPhpCode = '// Rendering ViewHelper ' . $node->getViewHelperClassName() . chr(10);
     // Build up $arguments array
     $argumentsVariableName = $this->variableName('arguments');
     $initializationPhpCode .= sprintf('%s = array();', $argumentsVariableName) . chr(10);
     $alreadyBuiltArguments = array();
     foreach ($node->getArguments() as $argumentName => $argumentValue) {
         if ($argumentValue instanceof NodeInterface) {
             $converted = $this->convert($argumentValue);
         } else {
             $converted = array('initialization' => '', 'execution' => $argumentValue);
         }
         $initializationPhpCode .= $converted['initialization'];
         $initializationPhpCode .= sprintf('%s[\'%s\'] = %s;', $argumentsVariableName, $argumentName, $converted['execution']) . chr(10);
         $alreadyBuiltArguments[$argumentName] = TRUE;
     }
     $arguments = $node->getArgumentDefinitions();
     foreach ($arguments as $argumentName => $argumentDefinition) {
         if (!isset($alreadyBuiltArguments[$argumentName])) {
             $initializationPhpCode .= sprintf('%s[\'%s\'] = %s;', $argumentsVariableName, $argumentName, var_export($argumentDefinition->getDefaultValue(), TRUE)) . chr(10);
         }
     }
     // Build up closure which renders the child nodes
     $renderChildrenClosureVariableName = $this->variableName('renderChildrenClosure');
     $initializationPhpCode .= sprintf('%s = %s;', $renderChildrenClosureVariableName, $this->templateCompiler->wrapChildNodesInClosure($node)) . chr(10);
     $viewHelperInitializationPhpCode = '';
     $convertedViewHelperExecutionCode = $node->getUninitializedViewHelper()->compile($argumentsVariableName, $renderChildrenClosureVariableName, $viewHelperInitializationPhpCode, $node, $this->templateCompiler);
     $initializationPhpCode .= $viewHelperInitializationPhpCode;
     $initializationArray = array('initialization' => $initializationPhpCode, 'execution' => $convertedViewHelperExecutionCode);
     return $initializationArray;
 }