Example #1
0
 /**
  * Compiles the ExpressionNode, returning an array with
  * exactly two keys which contain strings:
  *
  * - "initialization" which contains variable initializations
  * - "execution" which contains the execution (that uses the variables)
  *
  * The expression and matches can be read from the local
  * instance - and the RenderingContext and other APIs
  * can be accessed via the TemplateCompiler.
  *
  * @param TemplateCompiler $templateCompiler
  * @return array
  */
 public function compile(TemplateCompiler $templateCompiler)
 {
     $handlerClass = get_class($this);
     $expressionVariable = $templateCompiler->variableName('string');
     $matchesVariable = $templateCompiler->variableName('array');
     $initializationPhpCode = sprintf('// Rendering %s node' . chr(10), $handlerClass);
     $initializationPhpCode .= sprintf('%s = \'%s\';', $expressionVariable, $this->getExpression()) . chr(10);
     $initializationPhpCode .= sprintf('%s = %s;', $matchesVariable, var_export($this->getMatches(), true)) . chr(10);
     return ['initialization' => $initializationPhpCode, 'execution' => sprintf('\\%s::evaluateExpression($renderingContext, %s, %s)', $handlerClass, $expressionVariable, $matchesVariable)];
 }
 /**
  * This ViewHelper is used a *lot* because it is used by the escape interceptor.
  * Therefore we render it to raw PHP code during compilation
  *
  * @param string $argumentsName
  * @param string $closureName
  * @param string $initializationPhpCode
  * @param ViewHelperNode $node
  * @param TemplateCompiler $compiler
  * @return string
  */
 public function compile($argumentsName, $closureName, &$initializationPhpCode, ViewHelperNode $node, TemplateCompiler $compiler)
 {
     $valueVariableName = $compiler->variableName('value');
     $initializationPhpCode .= sprintf('%1$s = (%2$s[\'value\'] !== NULL ? %2$s[\'value\'] : %3$s());', $valueVariableName, $argumentsName, $closureName) . chr(10);
     return sprintf('!is_string(%1$s) && !(is_object(%1$s) && method_exists(%1$s, \'__toString\')) ? %1$s : htmlspecialchars(%1$s, (%2$s[\'keepQuotes\'] ? ENT_NOQUOTES : ENT_QUOTES), %2$s[\'encoding\'], %2$s[\'doubleEncode\'])', $valueVariableName, $argumentsName);
 }
Example #3
0
    /**
     * Compiles the ExpressionNode, returning an array with
     * exactly two keys which contain strings:
     *
     * - "initialization" which contains variable initializations
     * - "execution" which contains the execution (that uses the variables)
     *
     * The expression and matches can be read from the local
     * instance - and the RenderingContext and other APIs
     * can be accessed via the TemplateCompiler.
     *
     * @param TemplateCompiler $templateCompiler
     * @return string
     */
    public function compile(TemplateCompiler $templateCompiler)
    {
        $parts = preg_split('/([\\?:])/s', $this->getExpression());
        $parts = array_map([__CLASS__, 'trimPart'], $parts);
        list($check, $then, $else) = $parts;
        $matchesVariable = $templateCompiler->variableName('array');
        $initializationPhpCode = '// Rendering TernaryExpression node' . chr(10);
        $initializationPhpCode .= sprintf('%s = %s;', $matchesVariable, var_export($this->getMatches(), true)) . chr(10);
        $parser = new BooleanParser();
        $compiledExpression = $parser->compile($check);
        $functionName = $templateCompiler->variableName('ternaryExpression');
        $initializationPhpCode .= sprintf('%s = function($context, $renderingContext) {
				if (%s::convertToBoolean(' . $compiledExpression . ', $renderingContext) === TRUE) {
					return %s::getTemplateVariableOrValueItself(%s, $renderingContext);
				} else {
					return %s::getTemplateVariableOrValueItself(%s, $renderingContext);
				}
			};' . chr(10), $functionName, BooleanNode::class, static::class, var_export($then, true), static::class, var_export($else, true));
        return ['initialization' => $initializationPhpCode, 'execution' => sprintf('%s(%s::gatherContext($renderingContext, %s[1]), $renderingContext)', $functionName, static::class, $matchesVariable)];
    }
Example #4
0
 /**
  * @test
  */
 public function testVariableNameDelegatesToNodeConverter()
 {
     $instance = new TemplateCompiler();
     $nodeConverter = $this->getMock(NodeConverter::class, array('variableName'), array($instance));
     $nodeConverter->expects($this->once())->method('variableName')->willReturnArgument(0);
     $instance->setNodeConverter($nodeConverter);
     $this->assertEquals('foobar', $instance->variableName('foobar'));
 }
Example #5
0
 /**
  * Compiles the node structure to a native switch
  * statement which evaluates closures for each
  * case comparison and renders child node closures
  * only when value matches.
  *
  * @param string $argumentsName
  * @param string $closureName
  * @param string $initializationPhpCode
  * @param ViewHelperNode $node
  * @param TemplateCompiler $compiler
  * @return string
  */
 public function compile($argumentsName, $closureName, &$initializationPhpCode, ViewHelperNode $node, TemplateCompiler $compiler)
 {
     $phpCode = 'call_user_func_array(function($arguments) use ($renderingContext, $self) {' . PHP_EOL . 'switch ($arguments[\'expression\']) {' . PHP_EOL;
     $expressionEvaluationClosureName = $compiler->variableName('switchExpressionClosure');
     foreach ($node->getChildNodes() as $childNode) {
         if ($this->isDefaultCaseNode($childNode)) {
             $childrenClosure = $compiler->wrapChildNodesInClosure($childNode);
             $phpCode .= sprintf('default: return call_user_func(%s);', $childrenClosure) . PHP_EOL;
         } elseif ($this->isCaseNode($childNode)) {
             $valueClosure = $compiler->wrapViewHelperNodeArgumentEvaluationInClosure($childNode, 'value');
             $childrenClosure = $compiler->wrapChildNodesInClosure($childNode);
             $phpCode .= sprintf('case call_user_func(%s): return call_user_func(%s);', $valueClosure, $childrenClosure, $compiler->getNodeConverter()->convert($childNode)) . PHP_EOL;
         }
     }
     $phpCode .= '}' . PHP_EOL;
     $phpCode .= sprintf('}, array(%s))', $argumentsName);
     return $phpCode;
 }